├── .github └── workflows │ └── codeql-analysis.yml ├── .gitignore ├── 30daysOFcode_C# ├── day 00.cs ├── day 01.cs ├── day 02.cs ├── day 03.cs ├── day 04.cs ├── day 05.cs ├── day 06.cs ├── day 07.cs ├── day 08.cs ├── day 09.cs ├── day 10.cs ├── day 11.cs ├── day 12.cs ├── day 13.cs ├── day 14.cs ├── day 15.cs ├── day 16.cs ├── day 17.cs ├── day 18.cs ├── day 19.cs ├── day 20.cs ├── day 21.cs ├── day 22.cs ├── day 23.cs ├── day 24.cs ├── day 25.cs ├── day 26.cs ├── day 28.cs └── day 29.cs ├── 30daysOFcode_C++ ├── day 00.cpp ├── day 01.cpp ├── day 02.cpp ├── day 03.cpp ├── day 04.cpp ├── day 05.cpp ├── day 06.cpp ├── day 07.cpp ├── day 08.cpp ├── day 09.cpp ├── day 10.cpp ├── day 11.cpp ├── day 12.cpp ├── day 13.cpp ├── day 14.cpp ├── day 15.cpp ├── day 16.cpp ├── day 17.cpp ├── day 18.cpp ├── day 19.cpp ├── day 20.cpp ├── day 21.cpp ├── day 22.cpp ├── day 23.cpp ├── day 24.cpp ├── day 25.cpp ├── day 26.cpp ├── day 27.cpp ├── day 28.cpp └── day 29.cpp ├── 30daysOFcode_C ├── day 00.c ├── day 01.c ├── day 02.c ├── day 03.c ├── day 04.c ├── day 05.c ├── day 06.c ├── day 07.c ├── day 08.c ├── day 09.c ├── day 10.c ├── day 11.c ├── day 12.c ├── day 14.c ├── day 15.c ├── day 18 Stacks.C ├── day 18 queue.C ├── day 20.c ├── day 22.c ├── day 23.c ├── day 24.c ├── day 25.c ├── day 26.c ├── day 28.c └── day 29.c ├── 30daysOFcode_Dart ├── day 00.dart ├── day 01.dart ├── day 02.dart ├── day 03.dart ├── day 04.dart ├── day 05.dart ├── day 10.dart ├── day 16.dart └── day 25.dart ├── 30daysOFcode_Go ├── solution_set1 │ ├── day 00.go │ ├── day 01.go │ ├── day 02.go │ ├── day 03.go │ ├── day 04.go │ ├── day 05.go │ ├── day 06.go │ ├── day 07.go │ ├── day 08.go │ ├── day 09.go │ ├── day 10.go │ ├── day 11.go │ ├── day 12.go │ ├── day 13.go │ ├── day 14.go │ ├── day 15.go │ ├── day 16.go │ ├── day 17.go │ ├── day 18.go │ ├── day 19.go │ ├── day 20.go │ ├── day 21.go │ ├── day 22.go │ ├── day 23.go │ ├── day 25.go │ ├── day 26.go │ ├── day 28.go │ └── day 29.go └── solution_set2 │ ├── day 00.go │ ├── day 01.go │ ├── day 02.go │ ├── day 03.go │ ├── day 04.go │ ├── day 05.go │ ├── day 06.go │ ├── day 07.go │ ├── day 08.go │ ├── day 09.go │ ├── day 10.go │ ├── day 11.go │ ├── day 12.go │ ├── day 13.go │ ├── day 20.go │ ├── day 25.go │ ├── day 26.go │ ├── day 28.go │ └── day 29.go ├── 30daysOFcode_Java ├── day 00.java ├── day 01.java ├── day 02.java ├── day 03.java ├── day 04.java ├── day 05.java ├── day 06.java ├── day 07.java ├── day 08.java ├── day 09.java ├── day 10.java ├── day 11.java ├── day 12.java ├── day 13.java ├── day 14.java ├── day 15.java ├── day 16.java ├── day 17.java ├── day 18.java ├── day 19.java ├── day 20.java ├── day 21.java ├── day 22.java ├── day 23.java ├── day 24.java ├── day 25.java ├── day 26.java ├── day 27.java ├── day 28.java └── day 29.java ├── 30daysOFcode_JavaScript ├── README.md ├── day 00.js ├── day 01.js ├── day 02.js ├── day 03.js ├── day 04.js ├── day 05.js ├── day 06.js ├── day 07.js ├── day 08.js ├── day 09.js ├── day 10.js ├── day 11.js ├── day 12.js ├── day 13.js ├── day 14.js ├── day 15.js ├── day 16.js ├── day 17.js ├── day 18.js ├── day 19.js ├── day 20.js ├── day 21.js ├── day 22.js ├── day 23.js ├── day 24.js ├── day 25.js ├── day 26.js ├── day 28.js ├── day 29.js ├── package-lock.json ├── package.json └── tests │ ├── day 00.test.js │ ├── day 02.test.js │ ├── day 03.test.js │ ├── day 05.test.js │ ├── day 06.test.js │ ├── day 07.test.js │ ├── day 09.test.js │ ├── day 10.test.js │ ├── day 13.test.js │ └── day 14.test.js ├── 30daysOFcode_Julia ├── day 00.jl ├── day 01.jl ├── day 02.jl ├── day 03.jl ├── day 04.jl ├── day 05.jl ├── day 06.jl ├── day 07.jl ├── day 08.jl ├── day 09.jl ├── day 10.jl ├── day 11.jl └── day 20.jl ├── 30daysOFcode_Kotlin ├── day 00.kt ├── day 02.kt ├── day 03.kt ├── day 05.kt ├── day 06.kt ├── day 07.kt ├── day 08.kt ├── day 09.kt ├── day 10.kt ├── day 11.kt ├── day 20.kt ├── day 26.kt ├── day 28.kt └── day 29.kt ├── 30daysOFcode_Php ├── day 00.php ├── day 01.php ├── day 02.php ├── day 03.php ├── day 04.php ├── day 05.php ├── day 06.php ├── day 07.php ├── day 08.php ├── day 09.php ├── day 10.php ├── day 11.php ├── day 12.php ├── day 13.php ├── day 14.php ├── day 15.php ├── day 16.php ├── day 17.php ├── day 18.php ├── day 19.php ├── day 20.php ├── day 22.php ├── day 23.php ├── day 24.php ├── day 25.php ├── day 26.php ├── day 28.php └── day 29.php ├── 30daysOFcode_Python ├── day 00.py ├── day 01.py ├── day 02.py ├── day 03.py ├── day 04.py ├── day 05.py ├── day 06.py ├── day 07.py ├── day 08.py ├── day 09.py ├── day 10.py ├── day 11.py ├── day 12.py ├── day 13.py ├── day 14.py ├── day 15.py ├── day 16.py ├── day 17.py ├── day 18.py ├── day 19.py ├── day 20.py ├── day 21.py ├── day 22.py ├── day 23.py ├── day 24.py ├── day 25.py ├── day 26.py ├── day 27.py ├── day 28.py └── day 29.py ├── 30daysOFcode_Ruby ├── day 00.rb ├── day 01.rb ├── day 02.rb ├── day 03.rb ├── day 04.rb ├── day 05.rb ├── day 06.rb ├── day 07.rb ├── day 08.rb ├── day 09.rb ├── day 10.rb ├── day 11.rb ├── day 12.rb ├── day 13.rb ├── day 15.rb ├── day 17.rb ├── day 18.rb ├── day 20.rb ├── day 26.rb └── day 29.rb ├── 30daysOFcode_Rust ├── day 00.rs ├── day 01.rs ├── day 02.rs ├── day 03.rs ├── day 04.rs ├── day 05.rs ├── day 06.rs ├── day 07.rs ├── day 08.rs ├── day 11.rs ├── day 12.rs └── day 29.rs ├── 30daysOFcode_Scala ├── day 00.scala ├── day 01.scala ├── day 02.scala ├── day 03.scala ├── day 04.scala ├── day 05.scala ├── day 06.scala ├── day 07.scala ├── day 08.scala ├── day 09.scala ├── day 10.scala ├── day 18.scala └── day 29.scala ├── 30daysOFcode_Swift ├── day 00.swift ├── day 01.swift ├── day 02.swift ├── day 03.swift ├── day 04.swift ├── day 05.swift ├── day 06.swift ├── day 07.swift ├── day 08.swift ├── day 09.swift ├── day 10.swift ├── day 11.swift ├── day 12.swift ├── day 14.swift ├── day 15.swift ├── day 16.swift ├── day 17.swift ├── day 18.swift ├── day 20.swift ├── day 21.swift ├── day 22.swift ├── day 23.swift ├── day 24.swift ├── day 25.swift ├── day 26.swift ├── day 28.swift └── day 29.swift ├── 30daysOFcode_TypeScript ├── day 00.ts ├── day 01.ts ├── day 02.ts ├── day 03.ts ├── day 04.ts ├── day 05.ts ├── day 06.ts ├── day 07.ts ├── day 08.ts ├── day 09.ts ├── day 10.ts ├── day 11.ts ├── day 12.ts ├── day 13.ts ├── day 14.ts ├── day 15.ts ├── day 16.ts ├── day 17.ts ├── day 18.ts ├── day 19.ts ├── day 20.ts ├── day 21.ts ├── day 22.ts ├── day 23.ts ├── day 24.ts ├── day 25.ts ├── day 26.ts ├── day 27.ts ├── day 28.ts └── day 29.ts ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .idea 3 | node_modules 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /30daysOFcode_C#/day 00.cs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | static void Main(String[] args) { 4 | 5 | String inputString; 6 | 7 | inputString = Console.ReadLine(); 8 | Console.WriteLine("Hello, World."); 9 | 10 | Console.WriteLine(inputString); 11 | } 12 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 01.cs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | static void Main(String[] args) { 4 | 5 | int ii = int.Parse(Console.ReadLine()); 6 | double dd = double.Parse(Console.ReadLine()); 7 | string ss = Console.ReadLine(); 8 | 9 | Console.WriteLine(i + ii); 10 | Console.WriteLine(string.Format("{0:0.0}", d + dd)); 11 | Console.WriteLine(s + ss); 12 | } 13 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 02.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | 5 | class Solution { 6 | 7 | static void Main(String[] args) { 8 | 9 | double mealCost = double.Parse(Console.ReadLine()); 10 | int tipPercent = int.Parse(Console.ReadLine()); 11 | int taxPercent = int.Parse(Console.ReadLine()); 12 | 13 | double tip = mealCost * tipPercent / 100; 14 | double tax = mealCost * taxPercent / 100; 15 | 16 | double totalCost = Math.Round(mealCost + tip + tax, 0); 17 | 18 | Console.WriteLine("The total meal cost is " + totalCost + " dollars."); 19 | } 20 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 03.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | class Solution { 7 | 8 | static void Main(String[] args) { 9 | 10 | int N = Convert.ToInt32(Console.ReadLine()); 11 | 12 | if(N % 2 != 0){ 13 | Console.WriteLine("Weird"); 14 | }else{ 15 | if(N >= 2 && N <= 5){ 16 | Console.WriteLine("Not Weird"); 17 | } 18 | else if(N >= 6 && N <= 20){ 19 | Console.WriteLine("Weird"); 20 | } 21 | else if(N > 20){ 22 | Console.WriteLine("Not Weird"); 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 04.cs: -------------------------------------------------------------------------------- 1 | 2 | class Person { 3 | 4 | public int age; 5 | 6 | public Person(int initialAge) { 7 | if(initialAge > 0){ 8 | age = initialAge; 9 | }else{ 10 | age = 0; 11 | Console.WriteLine("Age is not valid, setting age to 0."); 12 | } 13 | } 14 | 15 | public void amIOld() { 16 | if(age < 13){ 17 | Console.WriteLine("You are young."); 18 | }else if(age >= 13 && age < 18){ 19 | Console.WriteLine("You are a teenager."); 20 | }else{ 21 | Console.WriteLine("You are old."); 22 | } 23 | } 24 | 25 | public void yearPasses() { 26 | age++; 27 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 05.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | class Solution { 7 | 8 | static void Main(String[] args) { 9 | 10 | int N = Convert.ToInt32(Console.ReadLine()); 11 | 12 | for(int i = 1; i < 11; i++){ 13 | Console.WriteLine(N + " x " + i + " = " + N*i); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 06.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | 5 | class Solution { 6 | 7 | static void Main(String[] args) { 8 | 9 | int n = int.Parse(Console.ReadLine()); 10 | string[] strings = new string[n]; 11 | string s0; 12 | string s1; 13 | 14 | for(int i = 0; i < n; i++){ 15 | strings[i] = Console.ReadLine(); 16 | 17 | s0 = ""; 18 | s1 = ""; 19 | 20 | for(int x = 0; x <= strings[i].Length - 1; x++){ 21 | if(x % 2 == 0){ 22 | s0 += strings[i][x]; 23 | }else{ 24 | s1 += strings[i][x]; 25 | } 26 | } 27 | 28 | Console.WriteLine(s0 + " " + s1); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 07.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | class Solution { 7 | 8 | static void Main(String[] args) { 9 | 10 | int n = Convert.ToInt32(Console.ReadLine()); 11 | string[] arr_temp = Console.ReadLine().Split(' '); 12 | int[] arr = Array.ConvertAll(arr_temp,Int32.Parse); 13 | 14 | string o = ""; 15 | 16 | for(int i = n-1; i >= 0; i--){ 17 | if(i > 0){ 18 | o += arr[i] + " "; 19 | }else{ 20 | o += arr[i]; 21 | } 22 | } 23 | 24 | Console.WriteLine(o); -------------------------------------------------------------------------------- /30daysOFcode_C#/day 09.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | 5 | class Solution { 6 | 7 | static void Main(String[] args) { 8 | 9 | int n = int.Parse(Console.ReadLine()); 10 | 11 | Console.WriteLine(factorial(n)); 12 | } 13 | 14 | static int factorial(int n) 15 | { 16 | if (n <= 1) 17 | { 18 | return 1; 19 | } 20 | else 21 | { 22 | return n * factorial(n - 1); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 10.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | class Solution { 7 | 8 | static void Main(String[] args) { 9 | 10 | int n = Convert.ToInt32(Console.ReadLine()); 11 | string binary = Convert.ToString(n, 2); 12 | 13 | string[] ones = binary.Split('0'); 14 | 15 | int sum = 0; 16 | int sumMax = 0; 17 | 18 | foreach (string o in ones){ 19 | 20 | for(int i=0; i < o.Length; i++){ 21 | if (o[i] == '1'){ 22 | sum++; 23 | } 24 | } 25 | 26 | sumMax = sumMax >= sum ? sumMax : sum; 27 | sum = 0; 28 | } 29 | 30 | Console.WriteLine(sumMax); 31 | } 32 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 11.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | class Solution { 7 | 8 | static void Main(String[] args) { 9 | 10 | int[][] arr = new int[6][]; 11 | 12 | for(int arr_i = 0; arr_i < 6; arr_i++){ 13 | string[] arr_temp = Console.ReadLine().Split(' '); 14 | arr[arr_i] = Array.ConvertAll(arr_temp,Int32.Parse); 15 | } 16 | 17 | int sum = 0; 18 | int sumMax = -64; 19 | 20 | for(int i=0; i < 6;i++){ 21 | for(int j=0; j < 6; j++){ 22 | if(i+1 < 6 && i+2 < 6 && j+1 < 6 && j+2 < 6){ 23 | sum = arr[i][j] + arr[i][j+1] + arr[i][j+2] + 24 | arr[i+1][j+1] + 25 | arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]; 26 | } 27 | sumMax = sum > sumMax ? sum : sumMax; 28 | } 29 | } 30 | Console.WriteLine(sumMax); 31 | } 32 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 14.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | class Difference { 5 | private int[] elements; 6 | public int maximumDifference; 7 | 8 | 9 | public Difference(int[] elements){ 10 | this.elements = elements; 11 | } 12 | 13 | public void computeDifference(){ 14 | int maxDiff = 0; 15 | 16 | foreach(int n in this.elements){ 17 | for(int i=0; i < this.elements.Length;i++){ 18 | maxDiff = n - this.elements[i]; 19 | this.maximumDifference = maxDiff > this.maximumDifference ? maxDiff : this.maximumDifference; 20 | } 21 | } 22 | } 23 | 24 | } // End of Difference Class 25 | 26 | class Solution { 27 | static void Main(string[] args) { 28 | Convert.ToInt32(Console.ReadLine()); 29 | 30 | int[] a = Console.ReadLine().Split(' ').Select(x=>Convert.ToInt32(x)).ToArray(); 31 | 32 | Difference d = new Difference(a); 33 | 34 | d.computeDifference(); 35 | 36 | Console.Write(d.maximumDifference); 37 | } 38 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 15.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | class Node 3 | { 4 | public int data; 5 | public Node next; 6 | public Node(int d){ 7 | data=d; 8 | next=null; 9 | } 10 | 11 | } 12 | class Solution { 13 | 14 | public static Node insert(Node head,int data) 15 | { 16 | if(head == null){ 17 | return new Node(data); 18 | } 19 | else if(head.next == null){ 20 | head.next = new Node(data); 21 | } 22 | else{ 23 | insert(head.next, data); 24 | } 25 | 26 | return head; 27 | } 28 | 29 | public static void display(Node head) 30 | { 31 | Node start=head; 32 | while(start!=null) 33 | { 34 | Console.Write(start.data+" "); 35 | start=start.next; 36 | } 37 | } 38 | static void Main(String[] args) { 39 | 40 | Node head=null; 41 | int T=Int32.Parse(Console.ReadLine()); 42 | while(T-->0){ 43 | int data=Int32.Parse(Console.ReadLine()); 44 | head=insert(head,data); 45 | } 46 | display(head); 47 | } 48 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 16.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | class Solution { 7 | 8 | static void Main(String[] args) { 9 | 10 | string S = Console.ReadLine(); 11 | 12 | try{ 13 | int i = int.Parse(S); 14 | Console.WriteLine(i); 15 | }catch(FormatException e){ 16 | Console.WriteLine("Bad String"); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 17.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Calculator{ 4 | 5 | public Calculator(){ 6 | 7 | } 8 | 9 | public int power(int n, int p){ 10 | if(n < 0 || p < 0){ 11 | throw new ArgumentException("n and p should be non-negative"); 12 | }else{ 13 | return Convert.ToInt32(Math.Pow(Convert.ToDouble(n), Convert.ToDouble(p))); 14 | } 15 | } 16 | } 17 | 18 | class Solution{ 19 | static void Main(String[] args){ 20 | Calculator myCalculator=new Calculator(); 21 | int T=Int32.Parse(Console.ReadLine()); 22 | while(T-->0){ 23 | string[] num = Console.ReadLine().Split(); 24 | int n = int.Parse(num[0]); 25 | int p = int.Parse(num[1]); 26 | try{ 27 | int ans=myCalculator.power(n,p); 28 | Console.WriteLine(ans); 29 | } 30 | catch(Exception e){ 31 | Console.WriteLine(e.Message); 32 | 33 | } 34 | } 35 | 36 | 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 19.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public interface AdvancedArithmetic{ 4 | int divisorSum(int n); 5 | } 6 | 7 | class Calculator : AdvancedArithmetic{ 8 | public int divisorSum(int n){ 9 | int sum = 0; 10 | 11 | for(int i = n; i > 0; i--){ 12 | if(n % i == 0){ 13 | sum += i; 14 | } 15 | } 16 | 17 | return sum; 18 | 19 | } 20 | } 21 | 22 | class Solution{ 23 | 24 | static void Main(string[] args){ 25 | 26 | int n = Int32.Parse(Console.ReadLine()); 27 | AdvancedArithmetic myCalculator = new Calculator(); 28 | int sum = myCalculator.divisorSum(n); 29 | Console.WriteLine("I implemented: AdvancedArithmetic\n" + sum); 30 | } 31 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 20.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Bubble 4 | { 5 | static void bubbleSort(int []arr) 6 | { 7 | int n = arr.Length; 8 | for (int i = 0; i < n - 1; i++) 9 | for (int j = 0; j < n - i - 1; j++) 10 | if (arr[j] > arr[j + 1]) 11 | { 12 | // swap temp and arr[i] 13 | int temp = arr[j]; 14 | arr[j] = arr[j + 1]; 15 | arr[j + 1] = temp; 16 | } 17 | } 18 | 19 | /* Prints the array */ 20 | static void printArray(int []arr) 21 | { 22 | int n = arr.Length; 23 | for (int i = 0; i < n; ++i) 24 | Console.Write(arr[i] + " "); 25 | Console.WriteLine(); 26 | } 27 | 28 | // Driver method 29 | public static void Main() 30 | { 31 | int []arr = {64, 34, 25, 12, 22, 11, 90}; 32 | bubbleSort(arr); 33 | Console.WriteLine("Sorted array"); 34 | printArray(arr); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /30daysOFcode_C#/day 21.cs: -------------------------------------------------------------------------------- 1 | /* https://www.hackerrank.com/challenges/30-binary-trees */ 2 | 3 | static void levelOrder(Node root){ 4 | //Write your code here 5 | Queue Q = new Queue(); 6 | Q.Enqueue(root); 7 | 8 | while (Q.Count > 0) 9 | { 10 | Node curNode = (Node)Q.Peek(); 11 | if (curNode.left != null) 12 | Q.Enqueue(curNode.left); 13 | if (curNode.right != null) 14 | Q.Enqueue(curNode.right); 15 | Console.Write(curNode.data + " "); 16 | Q.Dequeue(); 17 | } 18 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 25.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | class Solution { 5 | static void Main(string[] args) 6 | { 7 | int Counter = int.Parse(Console.ReadLine()); 8 | 9 | for (int i = 0; i < Counter; i++) 10 | { 11 | int Number= int.Parse(Console.ReadLine()); 12 | 13 | if (IsPrime(Number)) 14 | Console.WriteLine("Prime"); 15 | else 16 | Console.WriteLine("Not prime"); 17 | } 18 | } 19 | public static bool IsPrime(int n) 20 | { 21 | if (n < 2) 22 | return false; 23 | for (int i = 2; i * i <= n; i++) 24 | if (n % i == 0) 25 | return false; 26 | return true; 27 | } 28 | } -------------------------------------------------------------------------------- /30daysOFcode_C#/day 26.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Globalization; 4 | using System.IO; 5 | class Solution { 6 | static void Main(String[] args) { 7 | 8 | CultureInfo provider = new CultureInfo("fr-FR"); 9 | 10 | DateTime returnedDate = DateTime.Parse(Console.ReadLine(), provider); 11 | DateTime expireDate = DateTime.Parse(Console.ReadLine(), provider); 12 | 13 | if (returnedDate < expireDate) 14 | Console.WriteLine(0); 15 | else if (returnedDate.Year > expireDate.Year) 16 | Console.WriteLine(10000); 17 | else if (returnedDate.Month > expireDate.Month) 18 | Console.WriteLine(500 * (returnedDate.Month - expireDate.Month)); 19 | else if (returnedDate.Day > expireDate.Day) 20 | Console.WriteLine(15 * (returnedDate.Day - expireDate.Day)); 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /30daysOFcode_C++/day 00.cpp: -------------------------------------------------------------------------------- 1 | #include //includes all the header files 2 | using namespace std; 3 | int main() { 4 | // Declare a variable named 'input_string' to hold our input. 5 | string input_string; 6 | 7 | // Read a full line of input from stdin (cin) and save it to our variable, input_string. 8 | getline(cin, input_string); 9 | 10 | // Print a string literal saying "Hello, World." to stdout using cout. 11 | cout << "Hello, World." << endl; 12 | 13 | // TODO: Write a line of code here that prints the contents of input_string to stdout. 14 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int i = 4; 10 | double d = 4.0; 11 | string s = "HackerRank "; 12 | 13 | // Declare second integer, double, and String variables. 14 | int j = 0, sum1; 15 | double e, sum2 = 0.0; 16 | string t, sum3 = ""; 17 | 18 | // Read and save an integer, double, and String to your variables. 19 | // Note: If you have trouble reading the entire string, please go back and review the Tutorial closely. 20 | cin >> j >> e; 21 | cin.ignore(); 22 | getline(cin, t); 23 | 24 | // Print the sum of both integer variables on a new line. 25 | sum1 = i + j; 26 | printf("%d\n", sum1); 27 | 28 | // Print the sum of the double variables on a new line. 29 | sum2 = d +e ; 30 | cout << fixed << setprecision(1) << sum2 << "\n"; 31 | 32 | // Concatenate and print the String variables on a new line 33 | // The 's' variable above should be printed first. 34 | sum3 = s + t; 35 | cout << sum3; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /30daysOFcode_C++/day 02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | float mealCost, tipPercent, taxPercent, totalCost; //Declaration 6 | cin >> mealCost >> tipPercent >> taxPercent; //Taking The Input 7 | tipPercent = mealCost * tipPercent/100; //Calculating The Tip Percent 8 | taxPercent = mealCost * taxPercent/100; //Calculating The Tax Percent 9 | totalCost = mealCost + tipPercent + taxPercent; // Total Cost 10 | cout << round(totalCost); // Rounding Up The Cost 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | cin.ignore(numeric_limits::max(), '\n'); 9 | if( N%2 == 1 || (N > 5 && N < 21)){ 10 | cout<<"Weird"< 2 | using namespace std; 3 | int main(){ 4 | int n, i; 5 | cin >> n; 6 | for(i=1;i<=10;i++){ 7 | cout << n << " x "<< i << " = " << n*i << endl; 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | string s; 6 | int i, n; 7 | cin >> n; 8 | while(n--){ 9 | cin >> s; 10 | for(i=0;i 2 | using namespace std; 3 | int main(){ 4 | int i, n; //Variable Declaration 5 | cin >> n; 6 | int arr[n]; //Array Delcaration 7 | for(i=0;i> arr[i]; //Inputting Elements Into The Array 9 | } 10 | for(i=n-1;i>=0;i--){ 11 | cout << arr[i] << " "; //Outputting The Elements In Reverse Order 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int n; 13 | string name; 14 | long num; 15 | cin >> n; 16 | cin.ignore(); 17 | map pBook; 18 | for (int i = 0; i < n; i++) { 19 | cin >> name; 20 | cin >> num; 21 | pBook[name] = num; 22 | } 23 | while(cin >> name) { 24 | if (pBook.find(name) != pBook.end()) { 25 | cout << name << "=" << pBook.find(name)->second << endl; 26 | } else { 27 | cout << "Not found" << endl; 28 | } 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Complete the factorial function below. 6 | int factorial(int n) { 7 | int fact=1; 8 | while(n>0){ 9 | fact=fact*n; 10 | n--; 11 | } return fact; 12 | } 13 | 14 | int main() 15 | { 16 | ofstream fout(getenv("OUTPUT_PATH")); 17 | 18 | int n; 19 | cin >> n; 20 | cin.ignore(numeric_limits::max(), '\n'); 21 | 22 | int result = factorial(n); 23 | 24 | fout << result << "\n"; 25 | 26 | fout.close(); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | cin.ignore(numeric_limits::max(), '\n'); 9 | int rem=0,s=0,t=0; 10 | 11 | while(n>0) { 12 | rem=n%2; 13 | n=n/2; 14 | if(rem==1) { 15 | s++; 16 | if(s>=t) 17 | t=s; 18 | } 19 | else { 20 | s=0; 21 | } 22 | } 23 | 24 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int hourglass(vector>arr) 6 | { 7 | int n =arr.size(), o=arr[0].size(); 8 | int max = 0; 9 | for(int i=0;imax || i==0 && j==0)max=sum; 19 | } 20 | } 21 | return max; 22 | } 23 | 24 | 25 | int main(){ 26 | vector>arr(6,std::vector(6,0)); 27 | for(int i=0;i<6;i++) 28 | { 29 | for(int j=0;j<6;j++) 30 | { 31 | cin>>arr[i][j]; 32 | } 33 | } 34 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | string S; 6 | cin >> S; 7 | try 8 | { 9 | int n = stoi(S); 10 | cout << n << endl; 11 | } 12 | catch (exception a) 13 | { 14 | cout << "Bad String"; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /30daysOFcode_C++/day 17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | //Write your code here 8 | class Calculator { 9 | public: 10 | int power(int n, int p) 11 | { 12 | if (n < 0 || p < 0) 13 | { 14 | throw runtime_error("n and p should be non-negative"); 15 | } 16 | return pow(n, p); 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | Calculator myCalculator = Calculator(); 23 | int T, n, p; 24 | cin >> T; 25 | while (T-- > 0) { 26 | if (scanf("%d %d", &n, &p) == 2) { 27 | try { 28 | int ans = myCalculator.power(n, p); 29 | cout << ans << endl; 30 | } 31 | catch (exception& e) { 32 | cout << e.what() << endl; 33 | } 34 | } 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /30daysOFcode_C++/day 19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | class AdvancedArithmetic{ 9 | public: 10 | virtual int divisorSum(int n)=0; 11 | }; 12 | class Calculator : public AdvancedArithmetic { 13 | public: 14 | int divisorSum(int n) { 15 | int i, sum=0; 16 | for(int i=1;i<=n;i++ ){ 17 | if(n%i == 0){ 18 | sum+=i; 19 | } 20 | } 21 | return sum; 22 | } 23 | }; 24 | 25 | int main(){ 26 | int n; 27 | cin >> n; 28 | AdvancedArithmetic *myCalculator = new Calculator(); 29 | int sum = myCalculator->divisorSum(n); 30 | cout << "I implemented: AdvancedArithmetic\n" << sum; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | 11 | int i, j, n, temp, count=0, Swaps; 12 | cin>>n; 13 | int array[n]; 14 | 15 | for (i = 0; i >array[i]; 18 | } 19 | for (i = n-1; i >0; i--) 20 | { 21 | Swaps = 0; 22 | for (j = 0; j array[j+1]) 25 | { 26 | temp=array[j]; 27 | array[j]=array[j+1]; 28 | array[j+1]=temp; 29 | Swaps++; 30 | count++; 31 | } 32 | } 33 | if (Swaps == 0) 34 | { 35 | break; 36 | } 37 | } 38 | cout<<"Array is sorted in "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | template 7 | 8 | void printArray(vector arr) { 9 | for (int i = 0; i < arr.size(); i++) 10 | cout << arr[i] << endl; 11 | } 12 | 13 | int main() { 14 | vector vInt{1, 2, 3}; 15 | vector vString{"Hello", "World"}; 16 | 17 | printArray(vInt); 18 | printArray(vString); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isPrime(int n) { 7 | for (int i = 2; i <= sqrt(n); i++) 8 | if (n % i == 0) return false; 9 | return true; 10 | } 11 | 12 | int main() { 13 | int T; 14 | cin >> T; 15 | 16 | for (int i = 0; i < T; i++) { 17 | int n; 18 | cin >> n; 19 | 20 | if (n >= 2 && isPrime(n)) cout << "Prime" << endl; 21 | else cout << "Not prime" << endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int da, ma, ya; 7 | cin >> da; 8 | cin >> ma; 9 | cin >> ya; 10 | 11 | int de, me, ye; 12 | cin >> de; 13 | cin >> me; 14 | cin >> ye; 15 | 16 | int fine = 0; 17 | 18 | if (ya > ye) fine = 10000; 19 | else if (ya == ye) { 20 | if (ma > me) fine = (ma - me) * 500; 21 | else if (ma == me && da > de) fine = (da - de) * 15; 22 | } 23 | 24 | cout << fine; 25 | } 26 | -------------------------------------------------------------------------------- /30daysOFcode_C++/day 28.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | vector vs; 8 | while(t--){ 9 | string name,email; 10 | cin>>name>>email; 11 | /* 12 | The C++ function find() finds the first occurrence of the element. 13 | Here string::npos means not found. 14 | It is usually defined like so: 15 | static const size_t npos = -1; 16 | It is better to compare to npos instead of -1 17 | because the code is more legible. 18 | So here we are checking whether the email contains 19 | "@gmail.com" if yes, then push the email to the 20 | vector. 21 | */ 22 | if(email.find("@gmail.com")!= string::npos) 23 | vs.push_back(name); 24 | } 25 | // Sorts the vector in ascending order 26 | sort(vs.begin(),vs.end()); 27 | // Printing all the elements using forEach loop 28 | for(auto c : vs) 29 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,k; 9 | cin>>n>>k; 10 | vector v(n); 11 | int ans=0; 12 | iota(v.begin(),v.end(),1); 13 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | int i = 4; 8 | double d = 4.0; 9 | char s[] = "HackerRank "; 10 | 11 | // Declare second integer, double, and String variables. 12 | int second_integer; 13 | double second_double; 14 | char second_string[100]; 15 | 16 | // Read and save an integer, double, and String to your variables. 17 | scanf("%d\n",&second_integer); 18 | scanf("%lf\n",&second_double); 19 | fgets(second_string, 100, stdin); 20 | 21 | // Print the sum of both integer variables on a new line. 22 | printf("%d\n",i+second_integer); 23 | 24 | // Print the sum of the double variables on a new line. 25 | printf("%.1lf\n",d+second_double); 26 | 27 | // Concatenate and print the String variables on a new line 28 | // The 's' variable above should be printed first. 29 | printf("%s%s",s,second_string); 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /30daysOFcode_C/day 03.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // If is odd, print Weird 12 | // If is even and in the inclusive range of to , print Not Weird 13 | // If is even and in the inclusive range of to , print Weird 14 | // If is even and greater than , print Not Weird 15 | 16 | 17 | 18 | int main() 19 | { 20 | int n; 21 | scanf("%d",&n); 22 | 23 | if(n%2==1 || (n%2==0 && n>=6 && n<=20)) 24 | printf("Weird\n"); 25 | else if(n%2==0 && ( (n>=2 && n<=5) || n>20)) 26 | printf("Not Weird\n"); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /30daysOFcode_C/day 05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,i; 6 | scanf("%d",&n); 7 | for(i=1;i<=10;i++) 8 | { 9 | printf("%d x %d = %d\n",n,i,n*i); 10 | } 11 | return 0; 12 | -------------------------------------------------------------------------------- /30daysOFcode_C/day 06.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void string(){ 7 | int i = 0; 8 | char str[10000]; 9 | scanf("%s",&str); 10 | char even[5000], odd[5000]; 11 | for(i; i < strlen(str) ; i++){ 12 | if(i % 2 == 0) 13 | even[i/2] = str[i]; 14 | else 15 | odd[(i - 1) / 2] = str[i]; 16 | } 17 | printf("%s %s \n",even,odd); 18 | } 19 | 20 | 21 | int main() { 22 | int n; 23 | scanf("%d",&n); 24 | while(n-- > 0){ 25 | string(); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /30daysOFcode_C/day 07.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main() 12 | { 13 | int n,i; 14 | scanf("%d",&n); 15 | int A[n]; 16 | for(i=0;i=0;i--) 21 | printf("%d ",A[i]); 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /30daysOFcode_C/day 08.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | int n; 12 | char name[20]; 13 | long int num; 14 | scanf("%d",&n); 15 | map pBook; 16 | for (int i = 0; i < n; i++) { 17 | scanf("%s",name); 18 | scanf("%d",&num); 19 | pBook[name] = num; 20 | } 21 | while(scanf("%s",name)>0) { 22 | if (pBook.find(name) != pBook.end()) { 23 | printf("%s=%ld\n",name,pBook.find(name)->second); 24 | } else { 25 | printf("Not found\n"); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /30daysOFcode_C/day 10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main() { 12 | long int n; 13 | char bin_str[100]; 14 | int i=0; 15 | scanf("%ld",&n); 16 | int count=0; 17 | int max_count=count; 18 | while(n>0){ 19 | int temp=n%2; 20 | n=n/2; 21 | if(temp==0) 22 | { 23 | count=0; 24 | }else{ 25 | count++; 26 | } 27 | if(max_count 2 | 3 | int main(void) 4 | { 5 | char first[11], last[11]; 6 | char grade; 7 | int ID; 8 | 9 | int score_num; 10 | int score; 11 | int avg, cnt; 12 | 13 | scanf("%s %s %d", first, last, &ID); 14 | scanf("%d", &score_num); 15 | for(avg=0, cnt=0;cnt= avg) && (avg >= 90)) printf("Grade : O\n", avg / cnt); 27 | else if (avg >= 80) printf("Grade : E\n", avg / cnt); 28 | else if (avg >= 70) printf("Grade : A\n", avg / cnt); 29 | else if (avg >= 55) printf("Grade : P\n", avg / cnt); 30 | else if (avg >= 40) printf("Grade : D\n", avg / cnt); 31 | else printf("Grade : T\n", avg / cnt); 32 | 33 | } 34 | 35 | /* 36 | Heraldo Memelli 8135627 37 | 2 38 | 100 80 39 | */ -------------------------------------------------------------------------------- /30daysOFcode_C/day 14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int n, max=0; 7 | int absdiff; 8 | int element[10]; 9 | 10 | scanf("%d", &n); 11 | for(int i=0;i 2 | #include 3 | typedef struct Node 4 | { 5 | int data; 6 | struct Node* next; 7 | }Node; 8 | 9 | Node* insert(Node *head,int data) 10 | { Node * nod= 11 | nod=(Node *)malloc(sizeof(Node)); 12 | nod->data=data; 13 | if(head==NULL) {head =nod; return head;} 14 | Node *p; 15 | p=head; 16 | while(p->next!=NULL) 17 | p=p->next; 18 | p->next=nod; 19 | return head; 20 | 21 | //Complete this function 22 | } 23 | 24 | void display(Node *head) -------------------------------------------------------------------------------- /30daysOFcode_C/day 20.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | 4 | int i, j, n, temp, swap=0; //Variable Declaration 5 | scanf("%d", &n); 6 | int arr[n]; //Array Declaration 7 | 8 | for(i=0;i arr[j]){ 14 | temp = arr[i]; 15 | arr[i] = arr[j]; //Sorting The Elements Of The Array 16 | arr[j] = temp; 17 | swap++; 18 | } 19 | } 20 | } 21 | //Output 22 | printf("Array is sorted in %d swaps.\n", swap); 23 | printf("First Element: %d\n", arr[0]); 24 | printf("Last Element: %d\n", arr[n-1]); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /30daysOFcode_C/day 25.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | int T; 9 | long int n[T]; 10 | scanf("%d",&T); 11 | for(int i=0;i1;j--){ 15 | if(n[i]%j==0){ 16 | flag=1; 17 | break; 18 | } 19 | } 20 | if(flag){ 21 | printf("Not prime\n"); 22 | }else 23 | if(n[i]==1){ 24 | printf("Not prime\n"); 25 | }else{ 26 | printf("Prime\n"); 27 | } 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /30daysOFcode_C/day 26.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 9 | int Aday, Amonth, Ayear, Eday,Emonth, Eyear; 10 | int fine = 0; 11 | scanf("%d %d %d %d %d %d", &Aday, &Amonth, &Ayear, &Eday, &Emonth, &Eyear); 12 | if (Ayear == Eyear){ 13 | if (Amonth == Emonth){ 14 | fine = 15 * (Aday - Eday); 15 | } 16 | else { 17 | fine = 500 * (Amonth - Emonth); 18 | } 19 | } 20 | else if (Ayear > Eyear){ 21 | fine = 10000; 22 | } 23 | 24 | if (fine < 0){ 25 | fine = 0; 26 | } 27 | 28 | printf("%d", fine); 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 00.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main() { 4 | String s = stdin.readLineSync(); 5 | print('Hello, World.'); 6 | print(s); 7 | } 8 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 01.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main() { 4 | int i = 4; 5 | double d = 4.0; 6 | String s = "HackerRank "; 7 | 8 | int a; 9 | double b; 10 | String c; 11 | a = int.parse(stdin.readLineSync()); 12 | b = double.parse(stdin.readLineSync()); 13 | c = stdin.readLineSync(); 14 | 15 | print(i + a); 16 | print((d + b).toStringAsFixed(1)); 17 | print(s + c); 18 | } 19 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 02.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main() { 4 | double meal_cost; 5 | int tipPercent, taxPercent; 6 | 7 | meal_cost = double.parse(stdin.readLineSync()); 8 | tipPercent = int.parse(stdin.readLineSync()); 9 | taxPercent = int.parse(stdin.readLineSync()); 10 | 11 | double tip = (meal_cost * tipPercent) / 100.0; 12 | double tax = (meal_cost * taxPercent) / 100.0; 13 | 14 | double output = (meal_cost + tip + tax).roundToDouble(); 15 | print(output); 16 | } 17 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 03.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main() { 4 | int n; 5 | 6 | n = int.parse(stdin.readLineSync()); 7 | 8 | if (n % 2 == 0) { 9 | if ((n >= 2 && n <= 5) || n > 20) 10 | print("Not Weird\n"); 11 | else 12 | print("Weird\n"); 13 | } else 14 | print("Weird\n"); 15 | } 16 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 04.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | class Person { 4 | int age; 5 | Person(int initialAge) { 6 | age = initialAge; 7 | if (age < 0) { 8 | print("Age is not valid, setting age to 0."); 9 | age = 0; 10 | } 11 | } 12 | void amIOld() { 13 | if (age < 13) 14 | print("You are young."); 15 | else if (age >= 13 && age < 18) 16 | print("You are a teenager."); 17 | else 18 | print("You are old."); 19 | } 20 | 21 | void yearPasses() { 22 | age++; 23 | } 24 | } 25 | 26 | void main() { 27 | int test; 28 | test = int.parse(stdin.readLineSync()); 29 | for (int t = 1; t <= test; t++) { 30 | int age; 31 | age = int.parse(stdin.readLineSync()); 32 | Person p = new Person(age); 33 | p.amIOld(); 34 | for (int j = 0; j < 3; j++) { 35 | p.yearPasses(); 36 | } 37 | p.amIOld(); 38 | print(""); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 05.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main() { 4 | int n; 5 | 6 | n = int.parse(stdin.readLineSync()); 7 | 8 | for (int i = 1; i <= 10; i++) { 9 | int multiple = n * i; 10 | print("$n x $i = $multiple\n"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 10.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main() { 4 | int n = int.parse(stdin.readLineSync()); 5 | int rem = 0; 6 | int s = 0; 7 | int t = 0; 8 | 9 | while (n > 0) { 10 | rem = n % 2; 11 | n = n ~/ 2; 12 | if (rem == 1) { 13 | s = s + 1; 14 | if (s >= t) { 15 | t = s; 16 | } 17 | } else { 18 | s = 0; 19 | } 20 | } 21 | 22 | print(t); 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 16.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main() { 4 | String name = stdin.readLineSync(); 5 | try { 6 | int converted_int = int.parse(name); 7 | print(converted_int); 8 | } catch (E) { 9 | print("Bad String"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /30daysOFcode_Dart/day 25.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:math'; 3 | 4 | bool isPrime(int n) { 5 | int root = sqrt(n).toInt(); 6 | 7 | for (int i = 2; i <= root; i++) { 8 | if (n % i == 0) return false; 9 | } 10 | return true; 11 | } 12 | 13 | void main() { 14 | int T = int.parse(stdin.readLineSync()); 15 | while (T > 0) { 16 | int n = int.parse(stdin.readLineSync()); 17 | if (n >= 2 && isPrime(n)) { 18 | print('Prime'); 19 | } else { 20 | print('Not Prime'); 21 | } 22 | T--; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 00.go: -------------------------------------------------------------------------------- 1 | package main 2 | import ( 3 | "fmt" 4 | "bufio" 5 | "os" 6 | "io" 7 | ) 8 | 9 | func readStr() string { 10 | var reader *bufio.Reader 11 | var line []rune 12 | var char rune 13 | var err error 14 | 15 | reader = bufio.NewReader(os.Stdin) 16 | 17 | for { 18 | char, _, err = reader.ReadRune() 19 | if err == io.EOF || char == '\n' {break} 20 | if err != nil { 21 | os.Exit(1) 22 | } 23 | line = append(line, char) 24 | } 25 | return string(line) 26 | } 27 | 28 | 29 | func main() { 30 | var input string 31 | input = readStr() 32 | fmt.Printf("Hello, World.\n%s", input) 33 | } 34 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 01.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | 4 | import ( 5 | "fmt" 6 | "os" 7 | "bufio" 8 | "strconv" 9 | ) 10 | 11 | func main() { 12 | var _ = strconv.Itoa // Ignore this comment. You can still use the package "strconv". 13 | 14 | var i uint64 = 4 15 | var d float64 = 4.0 16 | var s string = "HackerRank " 17 | 18 | scanner := bufio.NewScanner(os.Stdin) 19 | // Declare second integer, double, and String variables. 20 | var a uint64 21 | var b float64 22 | var c string 23 | 24 | // Read and save an integer, double, and String to your variables. 25 | fmt.Scanln(&a) 26 | fmt.Scanln(&b) 27 | for scanner.Scan() { 28 | c = scanner.Text() 29 | } 30 | 31 | // Print the sum of both integer variables on a new line. 32 | fmt.Println(a+i) 33 | 34 | // Print the sum of the double variables on a new line. 35 | fmt.Printf("%.1f\n",b+d) 36 | 37 | // Concatenate and print the String variables on a new line 38 | // The 's' variable above should be printed first. 39 | fmt.Println(s+c) 40 | } -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 03.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | ) 7 | 8 | func main() { 9 | //Enter your code here. Read input from STDIN. Print output to STDOUT 10 | var num float64 11 | fmt.Scan(&num) 12 | //odd 13 | if math.Mod(num, 2) == 1 { 14 | fmt.Println("Weird") 15 | } else { 16 | if num < 6 && num > 1 { 17 | fmt.Println("Not Weird") 18 | } 19 | if num < 21 && num > 5 { 20 | fmt.Println("Weird") 21 | } 22 | if num > 20 { 23 | fmt.Println("Not Weird") 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 05.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | 13 | 14 | func main() { 15 | reader := bufio.NewReaderSize(os.Stdin, 1024 * 1024) 16 | 17 | nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 18 | checkError(err) 19 | n := int32(nTemp) 20 | 21 | for i := 1; i <= 10; i++{ 22 | fmt.Print(n," x ",i," = ",n*int32(i),"\n") 23 | } 24 | } 25 | 26 | func readLine(reader *bufio.Reader) string { 27 | str, _, err := reader.ReadLine() 28 | if err == io.EOF { 29 | return "" 30 | } 31 | 32 | return strings.TrimRight(string(str), "\r\n") 33 | } 34 | 35 | func checkError(err error) { 36 | if err != nil { 37 | panic(err) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 06.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "math" 7 | "os" 8 | ) 9 | 10 | func main() { 11 | var num int 12 | fmt.Scan(&num) 13 | scanner := bufio.NewReader(os.Stdin) 14 | var lines []string 15 | var i int 16 | for i <= num { 17 | line, _ := scanner.ReadString('\n') 18 | lines = append(lines, line) 19 | i++ 20 | } 21 | 22 | for _, v := range lines { 23 | if len(v) > 1 { 24 | var ( 25 | fword, lword string 26 | ) 27 | for i := range v { 28 | if v[i] != '\r' && v[i] != '\n' { 29 | if math.Mod(float64(i), 2) == 0 { 30 | fword = fword + string(v[i]) 31 | } else { 32 | lword = lword + string(v[i]) 33 | } 34 | } 35 | } 36 | fmt.Printf("%s %s\n", fword, lword) 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 07.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strings" 8 | ) 9 | 10 | func main() { 11 | var num int 12 | fmt.Scan(&num) 13 | scanner := bufio.NewReader(os.Stdin) 14 | buffer, _ := scanner.ReadString('\n') 15 | buffer = strings.Trim(buffer, "\r\n") 16 | bufferSlice := strings.Split(buffer, " ") 17 | for i := num - 1; i >= 0; i-- { 18 | fmt.Printf("%s ", bufferSlice[i]) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 08.go: -------------------------------------------------------------------------------- 1 | package main 2 | import ( 3 | "fmt" 4 | "bufio" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | //Enter your code here. Read input from STDIN. Print output to STDOUT 10 | var n int 11 | var k,v string 12 | m := make(map[string]string) 13 | scanner := bufio.NewScanner(os.Stdin) 14 | fmt.Scan(&n) 15 | for i := 0; i < n; i++ { 16 | fmt.Scanf("%s %s", &k, &v) 17 | m[k] = v 18 | } 19 | 20 | for scanner.Scan() { 21 | s := scanner.Text() 22 | if m[s] == "" { 23 | fmt.Println("Not found") 24 | } else { 25 | fmt.Printf("%s=%s\n",s,m[s]) 26 | } 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 09.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func factorial(n int) int { 6 | 7 | if(n==0){ 8 | 9 | return 1 10 | 11 | } 12 | 13 | if (n == 1) { 14 | 15 | return 1 16 | } 17 | 18 | return n * factorial(n-1) 19 | } 20 | 21 | func main() { 22 | 23 | var n int 24 | 25 | fmt.Scan(&n) 26 | 27 | 28 | result := factorial(n) 29 | 30 | fmt.Printf("%d", result) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 10.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | 4 | func main() { 5 | var n int 6 | 7 | fmt.Scan(&n) 8 | 9 | max := 0 10 | current := 0 11 | for _, c := range fmt.Sprintf("%b", n) { 12 | if (string(c) == "1") { 13 | current++ 14 | } else { 15 | if current > max { 16 | max = current 17 | } 18 | current = 0 19 | } 20 | } 21 | if current > max { 22 | max = current 23 | } 24 | fmt.Println(max) 25 | } 26 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 13.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Book struct { 6 | Title string 7 | Author string 8 | } 9 | 10 | type Booker interface { 11 | Display() 12 | } 13 | 14 | type MyBook struct { 15 | Book Book 16 | Price int 17 | } 18 | 19 | func(b *MyBook) Display() { 20 | fmt.Printf("Title: %s\n", b.Book.Author) 21 | fmt.Printf("Author: %s\n", b.Book.Author) 22 | fmt.Printf("Price: %d\n", b.Price) 23 | } 24 | 25 | func main() { 26 | a := MyBook{Book{"The Alchemist","Paulo Coelho"},248} 27 | a.Display() 28 | } -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 14.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | type Difference struct { 9 | elements []int 10 | maximumDiffernece int 11 | } 12 | 13 | func (d *Difference) ComputeDifference() int { 14 | sort.Ints(d.elements) 15 | d.maximumDiffernece = d.elements[len(d.elements)-1] - d.elements[0] 16 | return d.maximumDiffernece 17 | } 18 | 19 | func main() { 20 | a := Difference{[]int{3, 2, 5},0} 21 | fmt.Println(a.ComputeDifference()) 22 | 23 | } -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 15.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type Node struct { 4 | data interface{} 5 | next *Node 6 | } 7 | 8 | type LinkedList struct { 9 | head *Node 10 | tail *Node 11 | size int 12 | } 13 | 14 | func(list *LinkedList) Add(item interface{}) { 15 | newElement := &Node{data:item} 16 | if list.size == 0{ 17 | list.head = newElement 18 | list.tail = newElement 19 | } else { 20 | list.tail.next = newElement 21 | list.tail = newElement 22 | } 23 | list.size++ 24 | } 25 | 26 | func New() *LinkedList { 27 | return &LinkedList{} 28 | } 29 | 30 | func main() { 31 | a := New() 32 | for i:=0; i<5; i++ { 33 | a.Add(i) 34 | } 35 | } -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 16.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | "fmt" 6 | ) 7 | 8 | func conversion(s string) int { 9 | n, err := strconv.Atoi(s) 10 | if err != nil { 11 | fmt.Println("Bad String") 12 | return 0 13 | } else { 14 | return n 15 | } 16 | } 17 | 18 | func main() { 19 | 20 | fmt.Println(conversion("32")) 21 | } -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 17.go: -------------------------------------------------------------------------------- 1 | package main 2 | import ( 3 | "fmt" 4 | "math" 5 | "errors" 6 | ) 7 | 8 | func main() { 9 | var num int 10 | 11 | fmt.Scan(&num) 12 | 13 | for i := 0; i < num; i++ { 14 | var n, p int 15 | fmt.Scan(&n, &p) 16 | val, err := calcPower(float64(n), float64(p)) 17 | if err != nil { 18 | fmt.Println(err.Error()) 19 | continue 20 | } 21 | fmt.Println(val) 22 | } 23 | } 24 | 25 | func calcPower(n, p float64) (float64, error) { 26 | if n < 0 || p < 0 { 27 | return -1, errors.New("n and p should be non-negative") 28 | } 29 | return math.Pow(n, p), nil 30 | } -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 18.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | 4 | func main() { 5 | var s string 6 | fmt.Scan(&s) 7 | obj := &Solution{} 8 | for _, c := range s { 9 | obj.pushCharacter(c) 10 | obj.enqueueCharacter(c) 11 | } 12 | 13 | isPalindrome := true 14 | for i := 0; i < len(s)/2; i++ { 15 | if obj.popCharacter() != obj.dequeueCharacter() { 16 | isPalindrome = false 17 | break 18 | } 19 | } 20 | 21 | if isPalindrome { 22 | fmt.Println("The word, "+s+", is a palindrome.") 23 | } else { 24 | fmt.Println("The word, "+s+", is not a palindrome.") 25 | } 26 | } 27 | 28 | type Solution struct { 29 | queue []rune 30 | stack []rune 31 | } 32 | 33 | func (s *Solution) pushCharacter(c rune) { 34 | s.stack = append(s.stack, c) 35 | } 36 | func (s *Solution) enqueueCharacter(c rune) { 37 | s.queue = append(s.queue, c) 38 | } 39 | func (s *Solution) popCharacter() (rune) { 40 | c := s.stack[len(s.stack) - 1] 41 | s.stack = s.stack[:len(s.stack) - 1] 42 | return c 43 | } 44 | func (s *Solution) dequeueCharacter() (rune) { 45 | c := s.queue[0] 46 | s.queue = s.queue[1:] 47 | fmt.Println(c) 48 | return c 49 | } 50 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 19.go: -------------------------------------------------------------------------------- 1 | package main 2 | import ( 3 | "fmt" 4 | "math" 5 | ) 6 | 7 | type AdvancedArithmetic interface { 8 | divisorSum(n int) int 9 | } 10 | 11 | type impl struct{} 12 | func (impl) divisorSum(n int) int { 13 | sum := 0 14 | for i := 1; i <= int(math.Sqrt(float64(n))); i++ { 15 | if n % i == 0 { 16 | sum += i 17 | if i != n/i { 18 | sum += n/i 19 | } 20 | } 21 | } 22 | return sum 23 | } 24 | 25 | func main() { 26 | var n int 27 | fmt.Scan(&n) 28 | 29 | obj := impl{} 30 | fmt.Println("I implemented: AdvancedArithmetic") 31 | fmt.Println(obj.divisorSum(n)) 32 | } 33 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 20.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var a []int = []int{} 7 | var i, j, e, n int 8 | 9 | fmt.Scan(&n) 10 | 11 | for i = 0; i < n; i++ { 12 | fmt.Scan(&e) 13 | a = append(a, e) 14 | } 15 | 16 | totalSwaps := 0 17 | 18 | for i = 0; i < n; i++ { 19 | numberOfSwaps := 0 20 | 21 | for j = 0; j < n-1; j++ { 22 | if a[j] > a[j+1] { 23 | a[j], a[j+1] = a[j+1], a[j] 24 | numberOfSwaps++ 25 | totalSwaps++ 26 | } 27 | } 28 | 29 | if numberOfSwaps == 0 { 30 | break 31 | } 32 | } 33 | 34 | fmt.Printf("Array is sorted in %d swaps.\n", totalSwaps) 35 | fmt.Printf("First Element: %d\n", a[0]) 36 | fmt.Printf("Last Element: %d\n", a[len(a)-1]) 37 | } 38 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 21.go: -------------------------------------------------------------------------------- 1 | // Go 1.15 doesn't yet support generics so this is implemented using interface{} which allows you to appends any type to the array 2 | // For example, you can append both ints and strings to the same array. 3 | // Once Go is updated to support generics, this solution can be updated as well 4 | // See: https://blog.golang.org/generics-next-step, https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-contracts.md 5 | 6 | package main 7 | import "fmt" 8 | 9 | func printArray(arr []interface{}) { 10 | for _, v := range arr { 11 | fmt.Println(v) 12 | } 13 | } 14 | 15 | func main() { 16 | var size int 17 | fmt.Scan(&size) 18 | 19 | arr := make([]interface{}, size) 20 | for i := 0; i < size; i++ { 21 | var n int 22 | fmt.Scan(&n) 23 | arr[i] = n 24 | } 25 | 26 | printArray(arr) 27 | } 28 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set1/day 25.go: -------------------------------------------------------------------------------- 1 | package main 2 | import ( 3 | "fmt" 4 | ) 5 | 6 | func CheckPrime(number int) { 7 | isPrime := true 8 | if number <= 1 { 9 | fmt.Println("Not prime") 10 | } else if number<=3{ 11 | fmt.Println("Prime") 12 | } else if number%3==0 || number%2==0 { 13 | fmt.Println("Not prime") 14 | } else { 15 | for i := 5; i*i <= number; i=i+6 { 16 | if number%i == 0||number%(i+2)==0 { 17 | fmt.Println("Not prime") 18 | isPrime = false 19 | break 20 | } 21 | } 22 | if isPrime == true { 23 | fmt.Println("Prime") 24 | } 25 | } 26 | } 27 | func main() { 28 | var t int; 29 | fmt.Scanln(&t) 30 | for i:=0; i y1 { 11 | fmt.Print(10000) 12 | } else if m2 > m1 && y2 == y1 { 13 | fmt.Print(500 * (m2 - m1)) 14 | } else if d2 > d1 && m2 == m1 && y2 == y1 { 15 | fmt.Print(15 * (d2 - d1)) 16 | } else { 17 | fmt.Print(0) 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 00.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | //Enter your code here. Read input from STDIN. Print output to STDOUT 11 | // bufio implements buffered textual I/O 12 | reader := bufio.NewReader(os.Stdin) 13 | // to read string till '\n' newline sequence. 14 | // .Readstring return two values : 1st the string itself and 2nd an error (if it occured else nil) 15 | inputString, _ := reader.ReadString('\n') 16 | fmt.Println("Hello, World.") 17 | fmt.Println(inputString) 18 | } 19 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 01.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strconv" 8 | ) 9 | 10 | func main() { 11 | var _ = strconv.Itoa // Ignore this comment. You can still use the package "strconv". 12 | 13 | var i uint64 = 4 14 | var d float64 = 4.0 15 | var s string = "HackerRank " 16 | 17 | scanner := bufio.NewScanner(os.Stdin) 18 | // Declare second integer, double, and String variables. 19 | var num uint64 20 | var dec float64 21 | var str string 22 | // Read and save an integer, double, and String to your variables. 23 | fmt.Scanln(&num) 24 | fmt.Scanln(&dec) 25 | for scanner.Scan() { 26 | str = scanner.Text() 27 | } 28 | // Print the sum of both integer variables on a new line. 29 | fmt.Println(num + i) 30 | // Print the sum of the double variables on a new line. 31 | fmt.Printf("%.1f\n", dec+d) 32 | // Concatenate and print the String variables on a new line 33 | // The 's' variable above should be printed first. 34 | fmt.Println(s + str) 35 | } 36 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 03.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | //Enter your code here. Read input from STDIN. Print output to STDOUT 9 | var number uint32 10 | fmt.Scan(&number) 11 | // condition : If n is odd, print Weird 12 | if number%2 == 1 { 13 | fmt.Println("Weird") 14 | } else { 15 | // to apply second given condition 16 | if number < 6 && number > 2 { 17 | fmt.Println("Not Weird") 18 | } 19 | // to apply third given condition 20 | if number <= 20 && number > 6 { 21 | fmt.Println("Weird") 22 | } 23 | // to apply fourth given condition 24 | if number > 21 { 25 | fmt.Println("Not Weird") 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 05.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | func main() { 13 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 14 | 15 | nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 16 | checkError(err) 17 | n := int32(nTemp) 18 | //Printing the table using C style syntax 19 | var i int32 20 | for i = 1; i <= 10; i++ { 21 | fmt.Printf("%d x %d = %d\n", n, i, n*i) 22 | } 23 | } 24 | 25 | func readLine(reader *bufio.Reader) string { 26 | str, _, err := reader.ReadLine() 27 | if err == io.EOF { 28 | return "" 29 | } 30 | 31 | return strings.TrimRight(string(str), "\r\n") 32 | } 33 | 34 | func checkError(err error) { 35 | if err != nil { 36 | panic(err) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 06.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | //Enter your code here. Read input from STDIN. Print output to STDOUT 7 | // reading the number of string to be given 8 | var numberOfString int 9 | fmt.Scanln(&numberOfString) 10 | 11 | for i := 0; i < numberOfString; i++ { 12 | // reading the string in same variable again and again 13 | var inputString string 14 | fmt.Scanln(&inputString) 15 | 16 | for x := 0; x < len(inputString); x++ { 17 | //printing even position charaters 18 | if x%2 == 0 { 19 | // remember to convert to string because inputString[x] returns thier ASCII code/number 20 | fmt.Print(string(inputString[x])) 21 | } 22 | } 23 | fmt.Print(" ") 24 | for x := 0; x < len(inputString); x++ { 25 | //printing odd position charaters 26 | if x%2 != 0 { 27 | fmt.Print(string(inputString[x])) 28 | } 29 | } 30 | fmt.Print("\n") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 07.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | func main() { 13 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 14 | 15 | nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 16 | checkError(err) 17 | n := int32(nTemp) 18 | 19 | arrTemp := strings.Split(readLine(reader), " ") 20 | 21 | var arr []int32 22 | 23 | for i := 0; i < int(n); i++ { 24 | arrItemTemp, err := strconv.ParseInt(arrTemp[i], 10, 64) 25 | checkError(err) 26 | arrItem := int32(arrItemTemp) 27 | arr = append(arr, arrItem) 28 | } 29 | // Solution : a for loop that start from len(arr)-1 (becuz index starts from zero) till i>=0 30 | for i := len(arr) - 1; i >= 0; i-- { 31 | fmt.Print(arr[i], " ") 32 | } 33 | } 34 | 35 | func readLine(reader *bufio.Reader) string { 36 | str, _, err := reader.ReadLine() 37 | if err == io.EOF { 38 | return "" 39 | } 40 | 41 | return strings.TrimRight(string(str), "\r\n") 42 | } 43 | 44 | func checkError(err error) { 45 | if err != nil { 46 | panic(err) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 12.go: -------------------------------------------------------------------------------- 1 | // Golang doesnt have inheritance thats why 2 | // hackerRank doesnt have Golang as a option in this challenge 3 | // so please try in some other language to solve it 4 | 5 | package day12 6 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 13.go: -------------------------------------------------------------------------------- 1 | // Golang doesnt have abstract classes 2 | // hackerRank doesnt have Golang as a option in this challenge 3 | // so please try in some other language to solve it 4 | 5 | package day13 6 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 20.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var a []int = []int{} 7 | var i, j, e, n int 8 | 9 | fmt.Scan(&n) 10 | 11 | for i = 0; i < n; i++ { 12 | fmt.Scan(&e) 13 | a = append(a, e) 14 | } 15 | 16 | totalSwaps := 0 17 | 18 | for i = 0; i < n; i++ { 19 | numberOfSwaps := 0 20 | 21 | for j = 0; j < n-1; j++ { 22 | if a[j] > a[j+1] { 23 | a[j], a[j+1] = a[j+1], a[j] 24 | numberOfSwaps++ 25 | totalSwaps++ 26 | } 27 | } 28 | 29 | if numberOfSwaps == 0 { 30 | break 31 | } 32 | } 33 | 34 | fmt.Printf("Array is sorted in %d swaps.\n", totalSwaps) 35 | fmt.Printf("First Element: %d\n", a[0]) 36 | fmt.Printf("Last Element: %d\n", a[len(a)-1]) 37 | } 38 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 25.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | var numbers int 9 | var number int 10 | fmt.Scanln(&numbers) 11 | for i := 0; i < numbers; i++ { 12 | fmt.Scanln(&number) 13 | isPrime := true 14 | if number <= 1 { 15 | fmt.Println("Not prime") 16 | } else if number <= 3 { 17 | fmt.Println("Prime") 18 | } else if number%3 == 0 || number%2 == 0 { 19 | fmt.Println("Not prime") 20 | } else { 21 | for i := 5; i*i <= number; i = i + 1 { 22 | if number%i == 0 { 23 | fmt.Println("Not prime") 24 | isPrime = false 25 | break 26 | } 27 | } 28 | if isPrime == true { 29 | fmt.Println("Prime") 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 26.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var y1, y2, m1, m2, d1, d2 int 7 | fmt.Scanln(&d2, &m2, &y2) 8 | fmt.Scanln(&d1, &m1, &y1) 9 | 10 | if y2 > y1 { 11 | fmt.Print(10000) 12 | } else if m2 > m1 && y2 == y1 { 13 | fmt.Print(500 * (m2 - m1)) 14 | } else if d2 > d1 && m2 == m1 && y2 == y1 { 15 | fmt.Print(15 * (d2 - d1)) 16 | } else { 17 | fmt.Print(0) 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /30daysOFcode_Go/solution_set2/day 29.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | func main() { 13 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 14 | 15 | tTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 16 | checkError(err) 17 | t := int32(tTemp) 18 | 19 | for tItr := 0; tItr < int(t); tItr++ { 20 | nk := strings.Split(readLine(reader), " ") 21 | 22 | nTemp, err := strconv.ParseInt(nk[0], 10, 64) 23 | checkError(err) 24 | n := int32(nTemp) 25 | 26 | kTemp, err := strconv.ParseInt(nk[1], 10, 64) 27 | checkError(err) 28 | k := int32(kTemp) 29 | var res int32 30 | if ((k - 1) | k) <= n { 31 | res = k - 1 32 | } else { 33 | res = k - 2 34 | } 35 | fmt.Println(res) 36 | } 37 | } 38 | 39 | func readLine(reader *bufio.Reader) string { 40 | str, _, err := reader.ReadLine() 41 | if err == io.EOF { 42 | return "" 43 | } 44 | 45 | return strings.TrimRight(string(str), "\r\n") 46 | } 47 | 48 | func checkError(err error) { 49 | if err != nil { 50 | panic(err) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /30daysOFcode_Java/day 00.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class day0 { 3 | public static void main(String[] args) { 4 | String input_string; 5 | Scanner sc=new Scanner(System.in); 6 | input_string=sc.nextLine(); 7 | System.out.println("Hello, World.\n"+input_string); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /30daysOFcode_Java/day 01.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class day1 { 3 | public static void main(String[] args) { 4 | int i=4; 5 | double d=4.0; 6 | String s="HackerRank"; 7 | //variable declaration and object creation 8 | //done in the same line... 9 | int i2=new Scanner(System.in).nextInt(); 10 | double d2=new Scanner(System.in).nextDouble(); 11 | String s2=scan.next()+scan.nextLine(); 12 | // addition and concatination along with 13 | //printing the output. done in same line 14 | System.out.println((i+i2)+"\n"+(d+d2)+"\n"+s.concat(s2)); 15 | } 16 | //shorted and cleaner code provides better readability and 17 | //better understanding... 18 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 02.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Arithmetic { 3 | public static void main(String[] args){ 4 | //creating an object "sc" to call it later 5 | //this saves time 6 | Scanner sc=new Scanner(System.in); 7 | //allocating memory space using the object "sc" 8 | double mealCost = sc.nextDouble(); 9 | int tipPercent = sc.nextInt(); 10 | int taxPercent = sc.nextInt(); 11 | //net cost calculation done in single line saves time 12 | //and helps to make the code look clear and short 13 | double netCost =mealCost+(mealCost*tipPercent/100)+(mealCost*taxPercent/100); 14 | //using round function to round-off the answer\ 15 | //then casting it into integer type 16 | System.out.println((int) Math.round(netCost)); 17 | } 18 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 03.java: -------------------------------------------------------------------------------- 1 | //possibly the cleanest code for this program... 2 | import java.util.*; 3 | public class day3 4 | { 5 | public static void main(String[] args) 6 | { 7 | //Object Creation, variable declaration and memory allocation 8 | Scanner sc=new Scanner(System.in); 9 | int n=sc.nextInt(); 10 | //crisp two conditionals covering all 4 cases 11 | if(n%2!=0 || (n>5 && n<21)) System.out.println("Weird"); 12 | else System.out.println("Not Weird"); 13 | } 14 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 05.java: -------------------------------------------------------------------------------- 1 | //extremely simple code demonstrating loops 2 | import java.util.*; 3 | public class Solution { 4 | public static void main(String[] args) { 5 | //object creation and variable creation 6 | //then memory allocation to the variable... 7 | Scanner scanner=new Scanner(System.in); 8 | int n = scanner.nextInt(); 9 | for(int i=1;i<11;i++) 10 | System.out.println(n+" x "+i+" = "+(n*i)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /30daysOFcode_Java/day 06.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | public class Solution { 4 | public static void main(String[] args) { 5 | //creating an object to allocate memory space for input variables... 6 | Scanner sc=new Scanner(System.in); 7 | int t=sc.nextInt(); 8 | while(t!=0){ 9 | String s=sc.next(); 10 | String s1="",s2=""; 11 | for(int i=0;i=0;i--) System.out.print(a[i]+" "); 13 | } 14 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 08.java: -------------------------------------------------------------------------------- 1 | //Complete this code or write your own from scratch 2 | import java.util.*; 3 | import java.io.*; 4 | 5 | class Solution{ 6 | public static void main(String []argh){ 7 | Scanner in = new Scanner(System.in); 8 | HashMap m=new HashMap(); 9 | int n = in.nextInt(); 10 | for(int i = 0; i < n; i++){ 11 | String name = in.next(); 12 | int phone = in.nextInt(); 13 | 14 | // Write code here 15 | 16 | m.put(name,phone); 17 | 18 | } 19 | 20 | 21 | 22 | while(in.hasNext()){ 23 | String s = in.next(); 24 | // Write code here 25 | if(m.containsKey(s)==true) 26 | System.out.println(s+"="+m.get(s)); 27 | else 28 | System.out.println("Not found"); 29 | } 30 | in.close(); 31 | } 32 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 09.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class day09{ 3 | //defining a recursive function to find factorial... 4 | static int factorial(int n){ 5 | if(n<=1) return 1; //terminating case... 6 | return (n*factorial(n-1)); 7 | } 8 | public static void main(String[] args){ 9 | Scanner sc=new Scanner(System.in); 10 | int n=sc.nextInt(); 11 | //calling the function in the print statement... 12 | System.out.println(factorial(n)); 13 | } 14 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 10.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class day10{ 3 | //a short and simple code with better understandability... 4 | public static void main(String[] args) { 5 | //object creation for memory allocation for variables... 6 | Scanner sc = new Scanner(System.in); 7 | int n = sc.nextInt(); 8 | int sum=0,temp=0; 9 | while(n!=0) 10 | { 11 | int r=n%2; 12 | if(r==1) 13 | { 14 | sum++; 15 | if(sum>=temp) temp=sum; 16 | } 17 | else sum=0; 18 | n=n/2; 19 | } 20 | System.out.println(temp); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /30daysOFcode_Java/day 11.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int arr[][] = new int[6][6]; 12 | for(int i=0; i < 6; i++){ 13 | for(int j=0; j < 6; j++){ 14 | arr[i][j] = in.nextInt(); 15 | } 16 | } 17 | int max = -64; 18 | int sum = 0; 19 | for(int i=1; i<5; i++) 20 | { 21 | for(int j=1; j<5; j++) 22 | { 23 | sum = arr[i][j] + arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1] + arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1]; 24 | if(max 0) { 39 | int ele = sc.nextInt(); 40 | head = insert(head,ele); 41 | } 42 | display(head); 43 | sc.close(); 44 | } 45 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 16.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | import java.lang.*; 7 | 8 | public class Solution { 9 | 10 | public static void main(String[] args) { 11 | Scanner in = new Scanner(System.in); 12 | String S = in.next(); 13 | int l=S.length(); 14 | 15 | try{ 16 | int A = Integer.parseInt(S); 17 | 18 | System.out.print(S); 19 | 20 | } 21 | catch(java.lang.NumberFormatException e) 22 | { 23 | System.out.print("Bad String"); 24 | } 25 | 26 | 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /30daysOFcode_Java/day 17.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | import java.math.*; 4 | //Write your code here 5 | 6 | class Calculator extends Exception { 7 | 8 | public int power(int n, int p) throws Exception{ 9 | 10 | if (n <0 || p<0){ 11 | throw new Exception("n and p should be non-negative"); 12 | } 13 | 14 | return (int) Math.pow(n,p); 15 | } 16 | } 17 | 18 | class Solution{ 19 | 20 | public static void main(String[] args) { 21 | 22 | Scanner in = new Scanner(System.in); 23 | int t = in.nextInt(); 24 | while (t-- > 0) { 25 | 26 | int n = in.nextInt(); 27 | int p = in.nextInt(); 28 | Calculator myCalculator = new Calculator(); 29 | try { 30 | int ans = myCalculator.power(n, p); 31 | System.out.println(ans); 32 | } 33 | catch (Exception e) { 34 | System.out.println(e.getMessage()); 35 | } 36 | } 37 | in.close(); 38 | } 39 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 18.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | // Write your code here. 6 | LinkedList s=new LinkedList(); 7 | 8 | 9 | LinkedList q= new LinkedList(); 10 | 11 | void pushCharacter(char c) 12 | { 13 | s.push(c); 14 | 15 | } 16 | 17 | char popCharacter() 18 | { 19 | 20 | return (char)s.pop(); 21 | 22 | } 23 | 24 | 25 | void enqueueCharacter(char c) 26 | { 27 | q.add(c); 28 | } 29 | 30 | char dequeueCharacter() 31 | { 32 | 33 | return (char)q.remove(); 34 | } 35 | 36 | 37 | 38 | 39 | 40 | public static void main(String[] args) { -------------------------------------------------------------------------------- /30daysOFcode_Java/day 19.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | interface AdvancedArithmetic{ 5 | int divisorSum(int n); 6 | } 7 | 8 | class Calculator implements AdvancedArithmetic { 9 | public int divisorSum(int n) { 10 | // System.out.println("I implemented: AdvancedArithematic"); 11 | int A[]=new int[10000]; 12 | int h=0; 13 | int i=1; 14 | for(;i<=n;i++) 15 | { 16 | if(n%i==0) 17 | { 18 | A[h]=i; 19 | h++; 20 | } 21 | } 22 | 23 | int sum=0; 24 | 25 | for(i=0;ia[j+1]) 17 | { 18 | //below three lines are used for swapping... 19 | int temp=a[j]; 20 | a[j]=a[j+1]; 21 | a[j+1]=temp; 22 | numberOfSwaps++; 23 | } 24 | } 25 | if (numberOfSwaps==0) break; 26 | } 27 | System.out.println("Array is sorted in "+numberOfSwaps+" swaps."); 28 | System.out.println("First Element: "+a[0]); 29 | System.out.println("Last Element: "+a[n-1]); 30 | } 31 | } -------------------------------------------------------------------------------- /30daysOFcode_Java/day 21.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Printer { 4 | 5 | /** 6 | * Method Name: printArray 7 | * Print each element of the generic array on a new line. Do not return anything. 8 | * @param A generic array 9 | **/ 10 | 11 | // Write your code here 12 | 13 | public static void printArray(E[] a) 14 | { 15 | for(E elements: a) 16 | { 17 | System.out.println(elements); 18 | } 19 | } 20 | 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_Java/day 29.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | 4 | public class day29 { 5 | 6 | public static void main(String[] args) { 7 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 8 | Scanner sc=new Scanner(System.in); 9 | int t=sc.nextInt(); 10 | while(t-->0){ 11 | int n=sc.nextInt(); 12 | int k=sc.nextInt(); 13 | int max=0; 14 | for(int i=1;imax && res 5 | ``cd 30daysOFcode_JavaScript`` 6 | 7 | 2. For a particular code file, run the test file by using the following command.
8 | ``npm t filename.test.js`` 9 | 10 | - While contributing, Please add a test file in the tests folder corresponding to the original program file following the naming convention. 11 | 12 | - **Convention**:
13 | If **HackerRank.js** is the solution file, 14 | Test file should be named **HackerRank.test.js** 15 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 00.js: -------------------------------------------------------------------------------- 1 | function processData(inputString) { 2 | // This line of code prints the first line of output 3 | console.log("Hello, World."); 4 | 5 | // Write the second line of output that prints the contents of 'inputString' here. 6 | console.log(inputString) 7 | } 8 | 9 | module.exports = processData; -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 01.js: -------------------------------------------------------------------------------- 1 | // Declare second integer, double, and String variables. 2 | var myInt, myFloat, myString; 3 | 4 | // Read and save an integer, double, and String to your variables. 5 | myInt = parseInt(readLine()); 6 | myFloat = parseFloat(readLine()); 7 | myString = readLine(); 8 | 9 | // Print the sum of both integer variables on a new line. 10 | console.log(i + myInt); 11 | 12 | // Print the sum of the double variables on a new line. 13 | console.log((d + myFloat).toFixed(1)); 14 | 15 | // Concatenate and print the String variables on a new line 16 | // The 's' variable above should be printed first. 17 | console.log(s + myString) 18 | 19 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 02.js: -------------------------------------------------------------------------------- 1 | function operators({mealCost = 0, tipPercent = 0, taxPercent = 0}) { 2 | // Write your code here 3 | mealCost = Math.round(mealCost + (mealCost * tipPercent / 100) + (mealCost * taxPercent / 100)); 4 | 5 | // Use console.log() to print to stdout 6 | console.log('The total meal cost is ' + mealCost + ' dollars.'); 7 | } 8 | 9 | module.exports = operators; -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 03.js: -------------------------------------------------------------------------------- 1 | // N is the input 2 | function main(N) { 3 | // Run this function 4 | try { 5 | // !N means not true, it means if N is false 6 | if (!N) throw new TypeError("N cannot be undefined"); 7 | 8 | // If N is not equal to Number 9 | if (typeof N !== "number") 10 | // Return TypeError 11 | throw new TypeError("N is expected to be of type: Number"); 12 | 13 | // Declare n to N (Parse N to int value) 14 | // Pay attention! n is not equal to N (because it's sensitive case) 15 | const n = parseInt(N); 16 | 17 | // If N is even or n is between 6 and 20 18 | if (n % 2 !== 0 || (n >= 6 && n <= 20)) { 19 | // Print Weird 20 | console.log("Weird"); 21 | } else { 22 | // Print not weird 23 | console.log("Not Weird"); 24 | } 25 | 26 | // If there's something wrong with the code 27 | } catch (e) { 28 | // Throw an error 29 | throw e; 30 | } 31 | } 32 | 33 | module.exports = main; 34 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 04.js: -------------------------------------------------------------------------------- 1 | function Person(initialAge){ 2 | // Add some more code to run some checks on initialAge 3 | var age; 4 | if (initialAge >= 0) { 5 | age = initialAge 6 | } else { 7 | age = 0; 8 | console.log('Age is not valid, setting age to 0.'); 9 | } 10 | 11 | this.amIOld=function(){ 12 | // Do some computations in here and print out the correct statement to the console 13 | if (age < 13) { 14 | console.log('You are young.'); 15 | } else if (age >= 13 && age < 18) { 16 | console.log('You are a teenager.'); 17 | } else { 18 | console.log('You are old.'); 19 | } 20 | }; 21 | this.yearPasses=function(){ 22 | // Increment the age of the person in here 23 | age += 1; 24 | }; 25 | } 26 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 05.js: -------------------------------------------------------------------------------- 1 | function main(n) { 2 | try { 3 | if (!n) 4 | throw new TypeError("n cannot be undefined"); 5 | if (typeof n !== 'number') 6 | throw new TypeError("n is expected to be of type: number"); 7 | 8 | for (i = 1; i <= 10; i++) { 9 | console.log(n + ' x ' + i + ' = ' + (n * i)) 10 | } 11 | } catch (e) { 12 | throw e; 13 | } 14 | } 15 | 16 | module.exports = main; -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 06.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | //Enter your code here 3 | if (typeof input !== 'string') 4 | throw new TypeError('Invalid Type - String expected'); 5 | if (input.trim().length === 0) 6 | throw new TypeError('input cannot be empty'); 7 | 8 | input = input.split('\n'); 9 | 10 | for (i = 0; i < input.length; i++) { 11 | var splitWord = input[i].split(''); 12 | 13 | var even = ''; 14 | var odd = ''; 15 | 16 | for (x = 0; x < splitWord.length; x++) { 17 | if (x % 2 === 0) { 18 | even = even + splitWord[x]; 19 | } else { 20 | odd = odd + splitWord[x]; 21 | } 22 | } 23 | console.log(even + ' ' + odd); 24 | } 25 | } 26 | 27 | module.exports = processData; -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 07.js: -------------------------------------------------------------------------------- 1 | function main(inputArr) { 2 | 3 | if (!inputArr || !Array.isArray(inputArr)) 4 | throw new TypeError('Invalid Type - Array expected'); 5 | 6 | const outputString = inputArr.reverse().join(' '); 7 | console.log(outputString); 8 | } 9 | 10 | module.exports = main; 11 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 08.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | //Enter your code here 3 | input = input.split('\n'); 4 | var phoneBook = []; 5 | 6 | for (i = 1; i <= parseInt(input[0]); i++) { 7 | var contactArray = input[i].split(' '); 8 | phoneBook[contactArray[0]] = contactArray[1]; 9 | } 10 | 11 | for (i = (parseInt(input[0]) + 1); i < input.length; i++) { 12 | if (phoneBook[input[i]]) { 13 | console.log(input[i] + '=' + phoneBook[input[i]]); 14 | } else { 15 | console.log('Not found'); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 10.js: -------------------------------------------------------------------------------- 1 | function main(n) { 2 | if (n == null || n == undefined) 3 | throw new TypeError("Required params missing"); 4 | if (typeof n !== 'number') 5 | throw new TypeError("n is expected to be of type: number"); 6 | if (!Number.isInteger(n)) 7 | throw new TypeError("n is expected to be an Integer"); 8 | if (n < 0) 9 | throw new TypeError("n cannot be negative"); 10 | if (n <= 2) 11 | return n; 12 | 13 | var binary = []; 14 | while (n > 0) { 15 | binary.push(n % 2) 16 | n = Math.floor(n / 2) 17 | } 18 | 19 | binary = binary.reverse() 20 | 21 | var ones = 0; 22 | ; 23 | var max = 0; 24 | 25 | for (i = 0; i < binary.length; i++) { 26 | if (binary[i] === 1) { 27 | ones += 1; 28 | } else if (binary[i] === 0) { 29 | if (ones > max) max = ones; 30 | ones = 0; 31 | } 32 | } 33 | 34 | if (ones > max) max = ones 35 | 36 | // console.log(max) 37 | return max; 38 | } 39 | 40 | module.exports = main; 41 | 42 | 43 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 11.js: -------------------------------------------------------------------------------- 1 | function main() { 2 | var arr = []; 3 | for (arr_i = 0; arr_i < 6; arr_i++) { 4 | arr[arr_i] = readLine().split(' '); 5 | arr[arr_i] = arr[arr_i].map(Number); 6 | } 7 | 8 | var sumArray = []; 9 | for (i = 0; i < arr.length; i++) { 10 | 11 | for (j = 0; j < arr[i].length; j++) { 12 | var singleSum = 0; 13 | if (arr[i] && arr[i + 1] && arr[i + 2]) { 14 | if (arr[i][j + 1] != undefined && arr[i][j + 2] != undefined) { 15 | sumArray.push(arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]) 16 | } 17 | } 18 | } 19 | } 20 | console.log(Math.max.apply(null, sumArray)); 21 | } 22 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 13.js: -------------------------------------------------------------------------------- 1 | class Book { 2 | constructor(title, author) { 3 | this.title = title; 4 | this.author = author; 5 | } 6 | } 7 | 8 | // Declare your class here. 9 | class MyBook extends Book { 10 | /** 11 | * Class Constructor 12 | * 13 | * @param title The book's title. 14 | * @param author The book's author. 15 | * @param price The book's price. 16 | **/ 17 | // Write your constructor here 18 | constructor(title, author, price) { 19 | super(title, author); 20 | this.price = price 21 | } 22 | 23 | /** 24 | * Method Name: display 25 | * 26 | * Print the title, author, and price in the specified format. 27 | **/ 28 | // Write your method here 29 | display() { 30 | console.log('Title: ' + this.title + '\n' + 'Author: ' + this.author + '\n' + 'Price: ' + this.price); 31 | } 32 | 33 | // End class 34 | } 35 | 36 | module.exports = MyBook; 37 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 14.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Let and Const 3 | */ 4 | const PI = Math.PI; 5 | 6 | 7 | // ES5 syntax 8 | function getAreaES5(r) { 9 | let area; 10 | area = PI * Math.pow(r, 2); 11 | return area; 12 | } 13 | 14 | function getPerimeterES5(r) { 15 | let perimeter; 16 | perimeter = 2 * PI * r; 17 | return perimeter; 18 | } 19 | 20 | 21 | // ES6 syntax 22 | const getAreaES6 = (r) => PI * Math.pow(r, 2); 23 | const getPerimeterES6 = (r) => 2 * PI * r; 24 | 25 | 26 | // console.log(getAreaES5(2.6)); // Output should be 21.237166338267002 27 | // console.log(getPerimeterES5(2.6)); //Output should be 16.336281798666924 28 | // 29 | // console.log(getAreaES6(2.6)); // Output should be 21.237166338267002 30 | // console.log(getPerimeterES6(2.6)); //Output should be 16.336281798666924 31 | 32 | 33 | module.exports = { 34 | getAreaES5, 35 | getAreaES6, 36 | getPerimeterES5, 37 | getPerimeterES6 38 | } -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 15.js: -------------------------------------------------------------------------------- 1 | var node = new Node(data); 2 | 3 | if (head == null) { 4 | head = node 5 | } else { 6 | var temp = head; 7 | 8 | while (temp.next != null) 9 | temp = temp.next 10 | temp.next = node; 11 | } 12 | 13 | return head; 14 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 16.js: -------------------------------------------------------------------------------- 1 | function main() { 2 | /* 3 | * Try using `eval` 4 | */ 5 | var S = readLine(); 6 | 7 | try { 8 | console.log(eval(S)) 9 | } catch (e) { 10 | console.log('Bad String') 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 17.js: -------------------------------------------------------------------------------- 1 | function Calculator() { 2 | this.power = function(n, p) { 3 | try { 4 | if (n >= 0 && p >= 0) { 5 | return Math.pow(n, p) 6 | } else { 7 | throw 'n and p should be non-negative' 8 | } 9 | } catch (err) { 10 | return err 11 | } 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 18.js: -------------------------------------------------------------------------------- 1 | function Solution() { 2 | //Write your code here 3 | this.stack = []; 4 | this.queue = []; 5 | 6 | this.pushCharacter = this.stack.push; 7 | this.popCharacter = this.stack.pop; 8 | this.enqueueCharacter = this.queue.push; 9 | this.dequeueCharacter = this.queue.shift; 10 | } 11 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 19.js: -------------------------------------------------------------------------------- 1 | class AdvancedArithmetic { 2 | divisor(n) { 3 | return n || 0; 4 | }; 5 | } 6 | 7 | class Calculator extends AdvancedArithmetic { 8 | constructor(props) { 9 | super(props); 10 | 11 | this.divisor = this.divisorSum.bind(this); 12 | } 13 | 14 | divisorSum(n) { 15 | return Array(n).fill(0).reduce((target, item, index) => { 16 | !(n % (index + 1)) && (target += (index + 1)) ; 17 | 18 | return target; 19 | }, 0); 20 | } 21 | } 22 | 23 | function Solution () { 24 | const n = 6; 25 | 26 | const myCalculator = new Calculator(); 27 | 28 | let sum = myCalculator.divisor(n); 29 | 30 | console.log("I implemented: AdvancedArithmetic\n" + sum); 31 | } 32 | 33 | Solution(); -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 20.js: -------------------------------------------------------------------------------- 1 | function main() { 2 | var n = parseInt(readLine()); 3 | a = readLine().split(' '); 4 | a = a.map(Number); 5 | // Write Your Code Here 6 | var swapped; 7 | var numSwaps = 0; 8 | do { 9 | swapped = false; 10 | for (var i = 0; i < n - 1; i++) { 11 | if (a[i] > a[i + 1]) { 12 | var temp = a[i]; 13 | a[i] = a[i + 1]; 14 | a[i + 1] = temp; 15 | swapped = true; 16 | numSwaps++ 17 | } 18 | } 19 | } while (swapped); 20 | console.log('Array is sorted in ' + numSwaps + ' swaps.') 21 | console.log('First Element:', a[0]); 22 | console.log('Last Element:', a[n - 1]); 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 21.js: -------------------------------------------------------------------------------- 1 | class BaseClass { 2 | hello = function () { 3 | console.log("hello!"); 4 | }; 5 | } 6 | 7 | function GenericClass(T, Base) { 8 | return class extends Base { 9 | field = new T(); 10 | }; 11 | } 12 | 13 | class DerivedFromGeneric extends GenericClass(Array, BaseClass) { 14 | greet = function () { 15 | this.hello(); 16 | console.log("greetings ", this.field); 17 | }; 18 | } 19 | 20 | let i = new DerivedFromGeneric(); 21 | i.greet(); 22 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 24.js: -------------------------------------------------------------------------------- 1 | this.removeDuplicates=function(head){ 2 | //Write your code here 3 | let prev = head; 4 | 5 | while (prev) { 6 | let next = prev.next; 7 | 8 | (next && prev.data == next.data) 9 | ? (prev.next = next.next) 10 | : (prev = prev.next); 11 | } 12 | 13 | return head; 14 | } -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 25.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | var arr = input.split('\n'); 3 | for (var i = 1; i < arr.length; i++) { 4 | var n = arr[i]; 5 | if (isPrime(n)) { 6 | console.log("Prime"); 7 | } else { 8 | console.log("Not prime"); 9 | } 10 | } 11 | } 12 | 13 | function isPrime(n) { 14 | if (n <= 1) { 15 | return false; 16 | } 17 | if (n <= 3) { 18 | return true; 19 | } 20 | 21 | // This is checked so that we can skip 22 | // middle five numbers in below loop 23 | if (n % 2 == 0 || n % 3 == 0) { 24 | return false; 25 | } 26 | 27 | for (var index = 5; index * index <= n; index = index + 6) { 28 | if (n % index == 0 || n % (index + 2) == 0) { 29 | return false; 30 | } 31 | } 32 | return true; 33 | } 34 | 35 | 36 | process.stdin.resume(); 37 | process.stdin.setEncoding("ascii"); 38 | _input = ""; 39 | process.stdin.on("data", function(input) { 40 | _input += input; 41 | }); 42 | 43 | process.stdin.on("end", function() { 44 | processData(_input); 45 | }); -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 26.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | //Enter your code here 3 | var inputArr = input.split('\n'), 4 | actual = inputArr[0].split(' '), 5 | expected = inputArr[1].split(' '), 6 | month = 1, 7 | day = 0, 8 | year = 2, 9 | fine = 0; 10 | 11 | if (actual[year] > expected[year]) { 12 | fine = 10000; 13 | } else if (actual[month] > expected[month]) { 14 | fine = 500 * (actual[month] - expected[month]); 15 | } else if (actual[day] > expected[day]) { 16 | fine = 15 * (actual[day] - expected[day]); 17 | } 18 | 19 | console.log(fine); 20 | } 21 | 22 | process.stdin.resume(); 23 | process.stdin.setEncoding("ascii"); 24 | _input = ""; 25 | process.stdin.on("data", function(input) { 26 | _input += input; 27 | }); 28 | 29 | process.stdin.on("end", function() { 30 | processData(_input); 31 | }); -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 28.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | var input_stdin = ""; 5 | var input_stdin_array = ""; 6 | var input_currentline = 0; 7 | 8 | process.stdin.on('data', function(data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function() { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | /////////////// ignore above this line //////////////////// 22 | 23 | function main() { 24 | var N = parseInt(readLine()), 25 | op = []; 26 | for (var a0 = 0; a0 < N; a0++) { 27 | var firstName_temp = readLine().split(' '); 28 | var firstName = firstName_temp[0]; 29 | var emailID = firstName_temp[1]; 30 | 31 | if (/@gmail\.com/.test(emailID)) op.push(firstName); 32 | } 33 | console.log(op.sort().join("\n")); 34 | } -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/day 29.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | //Enter your code here 3 | let dates = input.split("\n"); 4 | let a = dates[0].split(" ").map(Number); 5 | let b = dates[1].split(" ").map(Number); 6 | let actDate = new Date( a[2] , a[1] , a[0] ); 7 | let expDate = new Date( b[2] , b[1] , b[0] ); 8 | let fine = 0 9 | 10 | if ( actDate <= expDate ) 11 | fine = 0; 12 | else if ( a[1] == b[1] && a[2] == b[2] ) 13 | fine = 15*(a[0] - b[0]); 14 | else if (a[2] == b[2]) 15 | fine = 500*(a[1] - b[1]); 16 | else 17 | fine = 10000; 18 | 19 | console.log(fine); 20 | } -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "30daysofcode_javascript", 3 | "version": "1.0.0", 4 | "description": "js solutions for hackerrank 30 days of code", 5 | "main": "day_1-Data_Types.js", 6 | "directories": { 7 | "test": "tests" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/rahulsain3000/Hackerrank_30daysOFcode.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/rahulsain3000/Hackerrank_30daysOFcode/issues" 17 | }, 18 | "homepage": "https://github.com/rahulsain3000/Hackerrank_30daysOFcode#readme", 19 | "dependencies": { 20 | "jest": "^29.7.0" 21 | }, 22 | "scripts": { 23 | "test": "jest --verbose" 24 | }, 25 | "jest": { 26 | "testMatch": [ 27 | "**/*.test.js" 28 | ] 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/tests/day 02.test.js: -------------------------------------------------------------------------------- 1 | const operators = require('../day_2-Operators'); 2 | 3 | describe('Operators - calculate meal cost', () => { 4 | 5 | // 1 6 | test('Truthy Case', () => { 7 | 8 | let outputData = ""; 9 | const storeLog = inputs => (outputData += ((inputs != null || inputs != undefined) ? inputs : "")); 10 | console["log"] = jest.fn(storeLog); 11 | 12 | operators({ 13 | mealCost: 10, 14 | tipPercent: 1, 15 | taxPercent: 10 16 | }); 17 | 18 | expect(outputData).toBe('The total meal cost is ' + 11 + ' dollars.') 19 | }); 20 | 21 | // 2 22 | test('default value', () => { 23 | 24 | let outputData = ""; 25 | const storeLog = inputs => (outputData += ((inputs != null || inputs != undefined) ? inputs : "")); 26 | console["log"] = jest.fn(storeLog); 27 | 28 | operators({}); 29 | 30 | expect(outputData).toBe('The total meal cost is ' + 0 + ' dollars.') 31 | }); 32 | 33 | }); 34 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/tests/day 05.test.js: -------------------------------------------------------------------------------- 1 | const main = require('../day_5-Loops'); 2 | 3 | 4 | describe('Loops - Error Conditions - should throw error', () => { 5 | 6 | // 1 7 | test('not passing a parameter', () => { 8 | expect(main).toThrowError(); 9 | }); 10 | 11 | // 1 12 | test('Passing in an integer as a type string', () => { 13 | expect(main.bind(null, "10")).toThrowError(); 14 | }); 15 | 16 | }); 17 | describe('Loops - True Conditions', () => { 18 | 19 | // 1 20 | test('correct Case - N as 2', () => { 21 | 22 | // expected output of this test case: table of 2 :D 23 | const outputExpected = `2 x 1 = 22 x 2 = 42 x 3 = 62 x 4 = 82 x 5 = 102 x 6 = 122 x 7 = 142 x 8 = 162 x 9 = 182 x 10 = 20`; 24 | 25 | // to get output of console.log 26 | let outputData = ""; 27 | const storeLog = inputs => (outputData += ((inputs != null || inputs != undefined) ? inputs : "")); 28 | console["log"] = jest.fn(storeLog); 29 | 30 | main(2); 31 | 32 | expect(outputData).toBe(outputExpected); 33 | }); 34 | 35 | }); 36 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/tests/day 07.test.js: -------------------------------------------------------------------------------- 1 | const main = require('../day_7-Arrays'); 2 | 3 | 4 | describe('Arrays - Error Conditions - should throw error', () => { 5 | 6 | // 1 7 | test('not passing a parameter', () => expect(main).toThrowError(TypeError)); 8 | 9 | // 2 10 | test('Passing an empty string', () => expect(main.bind(null, "")).toThrowError(TypeError)); 11 | 12 | // 3 13 | test('Passing a boolean type', () => expect(main.bind(null, true)).toThrowError(TypeError)); 14 | }); 15 | 16 | describe('Arrays - True Conditions', () => { 17 | 18 | // 1 19 | test('Correct Case', () => { 20 | 21 | // expected output of this test case: table of 2 :D 22 | const outputExpected = '4 3 2 1'; 23 | 24 | // to get output of console.log 25 | let outputData = ""; 26 | const storeLog = inputs => (outputData += ((inputs != null || inputs != undefined) ? inputs : "")); 27 | console["log"] = jest.fn(storeLog); 28 | 29 | main([1, 2, 3, 4]); 30 | 31 | expect(outputData).toBe(outputExpected); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/tests/day 09.test.js: -------------------------------------------------------------------------------- 1 | const main = require('../day_9-Recursion'); 2 | 3 | 4 | describe('Recursion - Error Conditions - should throw error', () => { 5 | 6 | // 1 7 | test('not passing a parameter', () => expect(main).toThrowError(TypeError)) 8 | 9 | // 2 10 | test('Passing in an integer as a type string', () => expect(main.bind(null, "10")).toThrowError(TypeError)); 11 | 12 | // 3 13 | test('Passing a negative integer', () => expect(main.bind(null, -10)).toThrowError(TypeError)); 14 | 15 | // 4 16 | test('Passing a float value', () => expect(main.bind(null, 10.5)).toThrowError(TypeError)); 17 | 18 | }); 19 | describe('Recursion- True Conditions', () => { 20 | // 1 21 | test('Correct Case', () => { 22 | 23 | expect(main(5)).toBe(120); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/tests/day 10.test.js: -------------------------------------------------------------------------------- 1 | const main = require('../day_10-Binary_Number'); 2 | 3 | 4 | describe('Binary Numbers - Error Conditions - should throw error', () => { 5 | 6 | // 1 7 | test('not passing a parameter', () => expect(main).toThrowError(TypeError)) 8 | 9 | // 2 10 | test('Passing in an integer as a type string', () => expect(main.bind(null, "10")).toThrowError(TypeError)); 11 | 12 | // 3 13 | test('Passing a negative integer', () => expect(main.bind(null, -10)).toThrowError(TypeError)); 14 | 15 | // 4 16 | test('Passing a float value', () => expect(main.bind(null, 10.5)).toThrowError(TypeError)); 17 | 18 | }); 19 | describe('Binary Numbers- True Conditions', () => { 20 | // 1 21 | test('Correct Case', () => { 22 | expect(main(13)).toBe(2); 23 | }); 24 | 25 | // 2 26 | test('Correct Case 2', () => { 27 | expect(main(5)).toBe(1); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/tests/day 13.test.js: -------------------------------------------------------------------------------- 1 | const MyBook = require('../day_13-Abstract_Classes'); 2 | 3 | describe('Let And Const', () => { 4 | 5 | // 1 6 | test('Correct Case - Case 1', () => { 7 | const myBook = new MyBook('Harry Potter', 'J K Rowling', 550); 8 | 9 | let outputData = ""; 10 | const storeLog = inputs => (outputData += ((inputs != null || inputs != undefined) ? inputs : "")); 11 | console["log"] = jest.fn(storeLog); 12 | 13 | myBook.display() 14 | expect(outputData).toBe('Title: ' + 'Harry Potter' + '\n' + 'Author: ' + 'J K Rowling' + '\n' + 'Price: ' + 550); 15 | }); 16 | 17 | // 2 18 | test('Correct Case - Case 2', () => { 19 | const myBook = new MyBook(); 20 | 21 | let outputData = ""; 22 | const storeLog = inputs => (outputData += ((inputs != null || inputs != undefined) ? inputs : "")); 23 | console["log"] = jest.fn(storeLog); 24 | 25 | myBook.display(); 26 | expect(outputData).toBe('Title: ' + 'undefined' + '\n' + 'Author: ' + 'undefined' + '\n' + 'Price: undefined'); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /30daysOFcode_JavaScript/tests/day 14.test.js: -------------------------------------------------------------------------------- 1 | const {getPerimeterES6, getPerimeterES5, getAreaES6, getAreaES5} = require('../day_14-Let_and_Const'); 2 | 3 | describe('Let And Const', () => { 4 | // 1 5 | test('Correct Case - getArea es5', () => { 6 | expect(getAreaES5(2.6)).toBe(21.237166338267002); 7 | }); 8 | 9 | // 2 10 | test('Correct Case - getArea es6', () => { 11 | expect(getAreaES6(2.6)).toBe(21.237166338267002); 12 | }); 13 | 14 | // 3 15 | test('Correct Case - getPerimeter es5', () => { 16 | expect(getPerimeterES5(2.6)).toBe(16.336281798666924); 17 | }); 18 | 19 | // 4 20 | test('Correct Case - getPerimeter es6', () => { 21 | expect(getPerimeterES6(2.6)).toBe(16.336281798666924); 22 | }); 23 | 24 | 25 | }); 26 | -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 00.jl: -------------------------------------------------------------------------------- 1 | # Day 0: Hello, World. 2 | # Read a full line of input from stdin and print it out. 3 | input_string = readline() 4 | 5 | # Print a string literal saying "Hello, World." to stdout. 6 | println("Hello, World.") 7 | 8 | # TODO: Write a line of code here that prints the contents of input_string to stdout. 9 | println(input_string) -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 01.jl: -------------------------------------------------------------------------------- 1 | # Day 1: Data Types 2 | 3 | # Declared variables 4 | i = 1 5 | d = 2.0 6 | s = "Hello " 7 | # Declare second integer, double, and String variables. 8 | # Read and save an integer, double, and String to your variables. 9 | newI = parse(UInt64, readline()) 10 | newD = parse(Float64, readline()) 11 | newS = readline() 12 | 13 | # Print the sum of both integer variables on a new line. 14 | println(i + newI) 15 | # Print the sum of the double variables on a new line. 16 | println(d + newD) 17 | # Concatenate and print the String variables on a new line 18 | # The 's' variable above should be printed first. 19 | println(string(s , newS)) 20 | -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 02.jl: -------------------------------------------------------------------------------- 1 | mealCost = parse(Float64, readline()) 2 | tipPercentage = parse(Float64, readline()) 3 | taxPercentage = parse(Float64, readline()) 4 | 5 | totalCost = 0.0 6 | totalCost += mealCost 7 | totalCost += tipPercentage * 0.01 * mealCost 8 | totalCost += taxPercentage * 0.01 * mealCost 9 | 10 | println(convert(Int64, round(totalCost) ) ) -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 03.jl: -------------------------------------------------------------------------------- 1 | n = parse(Int64, readline()) 2 | 3 | if n % 2 == 1 4 | println("Weird") 5 | elseif n % 2 == 0 6 | if n >= 2 && n <= 5 7 | println("Not Weird") 8 | elseif n >= 6 && n <= 20 9 | println("Weird") 10 | else 11 | println("Not Weird") 12 | end 13 | end -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 04.jl: -------------------------------------------------------------------------------- 1 | mutable struct Person 2 | age::Int64 3 | 4 | function Person(age::Int64) 5 | this = new() 6 | if age < 0 7 | println("Age is not valid") 8 | this.age = 0 9 | return this 10 | else 11 | this.age = age 12 | return this 13 | end 14 | end 15 | end 16 | 17 | function yearPasses(self::Person) 18 | self.age += 1 19 | end 20 | 21 | function amIOld(self::Person) 22 | if self.age < 13 23 | println("You are young.") 24 | elseif self.age < 18 25 | println("You are a teenager.") 26 | else 27 | println("You are old.") 28 | end 29 | end 30 | 31 | # a = Person(111) 32 | # yearPasses(a) 33 | # println(a.age) 34 | # amIOld(a) 35 | -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 05.jl: -------------------------------------------------------------------------------- 1 | n = parse(Int64, readline()) 2 | 3 | for i in 1:10 4 | println(n," x ",i," = ",n*i) 5 | end -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 06.jl: -------------------------------------------------------------------------------- 1 | n = parse(Int64, readline()) 2 | 3 | for i in 1:n 4 | S = readline() 5 | 6 | len = length(S) 7 | 8 | odd = "" 9 | even = "" 10 | 11 | for j in 1:2:len 12 | odd = odd*S[j] 13 | end 14 | for j in 2:2:len 15 | even = even*S[j] 16 | end 17 | println(odd," ",even) 18 | # println(even) 19 | end -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 07.jl: -------------------------------------------------------------------------------- 1 | n = parse(Int64, readline()) 2 | A = map((x) -> parse(Int64,x) , split(readline(), " ")) 3 | 4 | for i in n:-1:1 5 | print(A[i]," ") 6 | end 7 | println() -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 08.jl: -------------------------------------------------------------------------------- 1 | n = parse(Int64, readline()) 2 | 3 | dict = Dict{String, Int64}() 4 | 5 | for i in 1:n 6 | A = split(readline(), " ") 7 | dict[A[1]] = parse(Int64, A[2]) 8 | end 9 | 10 | for i in 1:n 11 | key = readline() 12 | val = get(dict,key,"-1") 13 | if val == "-1" 14 | println("Not found") 15 | else 16 | println(key,"=",val) 17 | end 18 | end -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 09.jl: -------------------------------------------------------------------------------- 1 | function factorial(n::Int64) 2 | if n == 0 || n == 1 3 | return 1 4 | else 5 | return n * factorial(n-1) 6 | end 7 | end 8 | n = parse(Int64, readline()) 9 | println(factorial(n)) -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 10.jl: -------------------------------------------------------------------------------- 1 | function max_ones(bitstream ::String) 2 | max_count = 0 3 | current = 0 4 | for i = 1:length(bitstream) 5 | if bitstream[i] == '1' 6 | current += 1 7 | else 8 | if current > max_count 9 | max_count = current 10 | end 11 | current = 0 12 | end 13 | end 14 | return max(max_count,current) 15 | end 16 | 17 | n = parse(Int64, readline()) 18 | # Get binary interpretation of n 19 | binary_n = bitstring(n) 20 | # println(binary_n) 21 | println(max_ones(binary_n)) -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 11.jl: -------------------------------------------------------------------------------- 1 | function maximum_hourglass() 2 | # Defined 2d Matrix 3 | B = Matrix{Int64}(undef,6,6) 4 | 5 | for i in 1:1:6 6 | A = map((x) -> parse(Int64,x) , split(readline(), " ")) 7 | for j in 1:1:6 8 | B[i,j] = A[j] 9 | end 10 | end 11 | max_val = -100 12 | 13 | for i in 2:5 14 | for j in 3:5 15 | max_val = max(max_val, B[i,j] + B[i-1,j] + B[i-1,j-1] + B[i-1,j+1] + B[i+1, j] + B[i+1, j-1] + B[i+1,j+1]) 16 | end 17 | end 18 | println(max_val) 19 | 20 | end 21 | 22 | maximum_hourglass() -------------------------------------------------------------------------------- /30daysOFcode_Julia/day 20.jl: -------------------------------------------------------------------------------- 1 | # Enter your code here 2 | n = parse(Int, readline()) 3 | arr = parse.(Int,split(readline())) 4 | numswaps = 0 5 | for i = 1:n 6 | for j = 1:n-1 7 | if arr[j]>arr[j+1] 8 | dummy = arr[j] 9 | arr[j] = arr[j+1] 10 | arr[j+1] = dummy 11 | numswaps += 1 12 | end 13 | end 14 | end 15 | print("Array is sorted in $(numswaps) swaps.",'\n') 16 | print("First Element: $(arr[1])",'\n') 17 | print("Last Element: $(arr[end])",'\n') 18 | -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 00.kt: -------------------------------------------------------------------------------- 1 | 2 | fun main() { 3 | val inputString=readLine()!!; 4 | println("Hello, World."); 5 | println(inputString); 6 | } -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 03.kt: -------------------------------------------------------------------------------- 1 | import java.util.Scanner 2 | 3 | fun main() { 4 | val scan = Scanner(System.`in`) 5 | 6 | val N = scan.nextLine().trim().toInt(); 7 | 8 | if(N%2==1){ 9 | println("Weird"); 10 | } 11 | else{ 12 | if(N>20){ 13 | println("Not Weird"); 14 | } 15 | else 16 | if(N>=6){ 17 | println("Weird"); 18 | } 19 | else{ 20 | println("Not Weird"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 05.kt: -------------------------------------------------------------------------------- 1 | import java.util.Scanner 2 | 3 | fun main(args: Array) { 4 | val scan = Scanner(System.`in`) 5 | 6 | val n = scan.nextLine().trim().toInt(); 7 | 8 | for (i in 1..10) { 9 | println("$n x $i = ${n*i}"); 10 | } 11 | } -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 06.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | var T:Int=readInt(); 3 | 4 | while(T-->0){ 5 | val inputString = readLn(); 6 | for (i in 0..inputString.length-1 step 2) { 7 | print(inputString[i]); 8 | } 9 | print(" "); 10 | for (i in 1..inputString.length-1 step 2) { 11 | print(inputString[i]); 12 | } 13 | println(""); 14 | } 15 | } 16 | 17 | //for ease 18 | 19 | private fun readLn() = readLine()!! 20 | private fun readInt() = readLn().toInt() -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 07.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val n:Int=readInt(); 3 | val array=readInts(); 4 | for(i in array.reversed()) 5 | { 6 | print("$i "); 7 | } 8 | } 9 | 10 | private fun readLn() = readLine()!! 11 | private fun readInt() = readLn().toInt() 12 | private fun readStrings() = readLn().split(" ") 13 | private fun readInts() = readStrings().map { it.toInt() } -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 08.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | 3 | var T:Int=readInt(); 4 | 5 | val contactList=mutableMapOf(); 6 | 7 | while(T-->0){ 8 | val contact=readStrings(); 9 | contactList[contact[0]]=contact[1].toLong(); 10 | } 11 | println(contactList) 12 | while(true){ 13 | val query=readLn(); 14 | if(query.isEmpty()){ 15 | break; 16 | } 17 | if(contactList.containsKey(query)){ 18 | println("$query=${contactList[query]}"); 19 | }else{ 20 | println("Not found"); 21 | } 22 | } 23 | 24 | } 25 | 26 | private fun readLn() = readLine()!! ; 27 | private fun readInt() = readLn().toInt(); 28 | private fun readLong() = readLn().toLong(); 29 | private fun readStrings() = readLn().split(" ") ; 30 | private fun readInts() = readStrings().map { it.toInt() } -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 09.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val n:Int=readLine()!!.toInt(); 3 | println(factorial(n)); 4 | } 5 | 6 | private fun factorial(n:Int):Int{ 7 | if(n==0||n==1) 8 | return 1; 9 | else 10 | return n*factorial(n-1); 11 | } -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 10.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | var n:Int=readLine()!!.toInt(); 3 | var ans=0; 4 | while(n!=0){ 5 | n=(n and (n shl 1)); 6 | ans++; 7 | } 8 | print(ans); 9 | } 10 | -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 11.kt: -------------------------------------------------------------------------------- 1 | import java.util.Scanner 2 | fun main(args: Array) { 3 | var matrix=Array(6){IntArray(6)} 4 | 5 | val input = Scanner(System.`in`); 6 | 7 | for(i in 0..5){ 8 | for(j in 0..5){ 9 | matrix[i][j]=input.nextInt(); 10 | } 11 | } 12 | 13 | var sum=Int.MIN_VALUE; 14 | 15 | for(i in 0..3){ 16 | for(j in 0..3){ 17 | val curSum=matrix[i][j] + matrix[i][j+1] + matrix[i][j+2]+ matrix[i+1][j+1]+ matrix[i+2][j] + matrix[i+2][j+1] + matrix[i+2][j+2]; 18 | if(curSum>sum){ 19 | sum=curSum; 20 | } 21 | } 22 | } 23 | 24 | print(sum); 25 | } 26 | -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 20.kt: -------------------------------------------------------------------------------- 1 | 2 | fun main(args: Array) { 3 | val n:Int=readInt(); 4 | var inputArray=readInts().toTypedArray(); 5 | 6 | var ans=0; 7 | for(i in 0..n-1){ 8 | for(j in 0..n-2){ 9 | if(inputArray[j]>inputArray[j+1]){ 10 | inputArray[j]=inputArray[j+1].also( { inputArray[j+1]=inputArray[j]}); //swap 11 | 12 | ans++; 13 | } 14 | } 15 | if(ans==0) 16 | break; 17 | } 18 | println("Array is sorted in $ans swaps."); 19 | println("First Element: ${inputArray[0]}\nLast Element: ${inputArray[n-1]}"); 20 | } 21 | private fun readLn() = readLine()!! ; 22 | private fun readInt() = readLn().toInt(); 23 | private fun readStrings() = readLn().split(" ") ; 24 | private fun readInts() = readStrings().map { it.toInt() } -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 26.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | var actualDate=readInts().toTypedArray(); 3 | var expectedDate=readInts().toTypedArray(); 4 | 5 | var fine=0; 6 | if(expectedDate[2]==actualDate[2]){ 7 | if(expectedDate[1]) { 3 | val names=ArrayList(); 4 | val n=readInt(); 5 | 6 | for (i in 0..n-1) { 7 | var input=readStrings().toTypedArray(); 8 | var isvalid=input[1].split("@"); 9 | 10 | if(isvalid[1].equals("gmail.com")){ 11 | names.add(input[0]); 12 | } 13 | } 14 | names.sort(); 15 | for (i in names) { 16 | println(i); 17 | } 18 | } 19 | private fun readLn() = readLine()!! ; 20 | private fun readInt() = readLn().toInt(); 21 | private fun readStrings() = readLn().split(" ") ; 22 | private fun readInts() = readStrings().map { it.toInt() } -------------------------------------------------------------------------------- /30daysOFcode_Kotlin/day 29.kt: -------------------------------------------------------------------------------- 1 | 2 | fun main(args: Array) { 3 | var T=readLine()!!.toInt(); 4 | while(T-->0){ 5 | val query=readInts().toTypedArray(); 6 | println(findMax(query[0], query[1])); 7 | } 8 | 9 | } 10 | private fun findMax(n:Int,k:Int):Int{ 11 | var max=0; 12 | var a=n-1; 13 | while(a-->0){ 14 | for(b in (a+1)..n){ 15 | val temp=a and b; 16 | if(tempmax){ 17 | max=temp; 18 | } 19 | } 20 | } 21 | return max; 22 | 23 | } 24 | private fun readLn() = readLine()!! ; 25 | private fun readInt() = readLn().toInt(); 26 | private fun readStrings() = readLn().split(" ") ; 27 | private fun readInts() = readStrings().map { it.toInt() } -------------------------------------------------------------------------------- /30daysOFcode_Php/day 00.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 01.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 02.php: -------------------------------------------------------------------------------- 1 | =2 and $N<=5) 12 | print("Not Weird"); 13 | elseif($N%2==0 and $N>=6 and $N<=20) 14 | print("Weird"); 15 | else 16 | print("Not Weird"); 17 | fclose($stdin); 18 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 05.php: -------------------------------------------------------------------------------- 1 | age=0; 8 | print("Age is not valid, setting age to 0.\n"); 9 | } 10 | else{ 11 | $this->age=$initialAge; 12 | } 13 | 14 | } 15 | 16 | public function amIOld(){ 17 | // Do some computations in here and print out the correct statement to the console 18 | if($this->age<13)print("You are young.\n"); 19 | elseif($this->age>=13 and $this->age<18)print("You are a teenager.\n"); 20 | else print("You are old.\n"); 21 | } 22 | public function yearPasses(){ 23 | // Increment the age of the person in here 24 | $this->age=$this->age+1; 25 | 26 | } 27 | 28 | 29 | } 30 | 31 | $T = intval(fgets(STDIN)); -------------------------------------------------------------------------------- /30daysOFcode_Php/day 06.php: -------------------------------------------------------------------------------- 1 | 1){ 11 | $i = 0; 12 | foreach(str_split($str) as $k => $v){ 13 | if($i % 2 == 1){ 14 | $odd .= $v; 15 | }else{ 16 | $even .= $v; 17 | } 18 | $i++; 19 | } 20 | 21 | echo $even . ' '. $odd."\n"; 22 | } 23 | } 24 | 25 | ?> 26 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 07.php: -------------------------------------------------------------------------------- 1 | title=$t; 10 | $this->author=$a; 11 | } 12 | abstract protected function display(); 13 | } 14 | 15 | //Write MyBook class 16 | class MyBook extends Book { 17 | 18 | public function __construct (string $title, string $author, float $price) { 19 | $this->title = $title; 20 | $this->author = $author; 21 | $this->price = $price; 22 | } 23 | 24 | public function display() { 25 | echo "Title: " . $this->title; 26 | echo "Author: " . $this->author; 27 | echo "Price: " . $this->price; 28 | } 29 | } 30 | 31 | $title=fgets(STDIN); 32 | $author=fgets(STDIN); 33 | $price=intval(fgets(STDIN)); 34 | $novel=new MyBook($title,$author,$price); 35 | $novel->display(); 36 | 37 | ?> 38 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 14.php: -------------------------------------------------------------------------------- 1 | elements = $elements; 9 | } 10 | 11 | function computeDifference() { 12 | $el = $this->elements; 13 | $this->maximumDifference = 0; 14 | 15 | for($i = 0; $i < count($el); $i++) { 16 | for($j = 0; $j < count($el); $j++) { 17 | $val = $el[$i] - $el[$j]; 18 | if($val > $this->maximumDifference) { 19 | $this->maximumDifference = $val; 20 | } 21 | } 22 | } 23 | } 24 | 25 | } //End of Difference class 26 | 27 | 28 | $N=intval(fgets(STDIN)); 29 | $a =array_map('intval', explode(' ', fgets(STDIN))); 30 | $d=new Difference($a); 31 | $d->ComputeDifference(); 32 | print ($d->maximumDifference); 33 | ?> 34 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 15.php: -------------------------------------------------------------------------------- 1 | $a[$j+1]) { 18 | $tmp = $a[$j]; 19 | $a[$j] = $a[$j+1]; 20 | $a[$j+1] = $tmp; 21 | ++$swaps; 22 | } 23 | } 24 | 25 | if ($swaps == 0) 26 | break; 27 | 28 | $totalSwaps += $swaps; 29 | } 30 | 31 | echo "Array is sorted in $totalSwaps swaps.\n"; 32 | echo "First Element: $a[0]\n"; 33 | $last = $a[$n-1]; 34 | echo "Last Element: $last"; 35 | 36 | ?> -------------------------------------------------------------------------------- /30daysOFcode_Php/day 16.php: -------------------------------------------------------------------------------- 1 | 0) { 22 | list($n, $p) = explode(" ", trim(fgets(STDIN))); 23 | try { 24 | $ans = $myCalculator->power($n, $p); 25 | echo $ans . "\n"; 26 | } catch (Exception $e) { 27 | echo $e->getMessage() . "\n"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 19.php: -------------------------------------------------------------------------------- 1 | divisorSum($n); 31 | echo 'I implemented: AdvancedArithmetic' . PHP_EOL . $sum; 32 | } else { 33 | echo 'Wrong answer'; 34 | } 35 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 20.php: -------------------------------------------------------------------------------- 1 | 37 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 26.php: -------------------------------------------------------------------------------- 1 | 0 ? 15 * ($d1 - $d2) : 0; 10 | $m = 500 * ($m1 - $m2) > 0 ? 500 * ($m1 - $m2) : 0; 11 | $y = $y1 - $y2 > 0 ? 10000 : 0; 12 | 13 | if ($y > 0) { 14 | return $y; 15 | } 16 | 17 | if ($m > 0) { 18 | return $m; 19 | } 20 | 21 | if ($d > 0) { 22 | return $d + $m + $y; 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | $_fp = fopen("php://stdin", "r"); 29 | 30 | fscanf($_fp, "%[^\n]", $date1); 31 | fscanf($_fp, "%[^\n]", $date2); 32 | 33 | $date1 = date_parse_from_format('j m Y', $date1); 34 | $date2 = date_parse_from_format('j m Y', $date2); 35 | 36 | echo libraryFine($date1['day'], $date1['month'], $date1['year'], $date2['day'], $date2['month'], $date2['year']); 37 | -------------------------------------------------------------------------------- /30daysOFcode_Php/day 28.php: -------------------------------------------------------------------------------- 1 | 29 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 00.py: -------------------------------------------------------------------------------- 1 | ''' 2 | # Day 0: Hello, World. 3 | # Read a full line of input from stdin and save it to our dynamically typed variable, input_string. 4 | input_string = input() 5 | 6 | # Print a string literal saying "Hello, World." to stdout. 7 | print('Hello, World.') 8 | 9 | # TODO: Write a line of code here that prints the contents of input_string to stdout. 10 | print(input_string) 11 | ''' 12 | print('Hello, World.', input(), sep='\n') 13 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 01.py: -------------------------------------------------------------------------------- 1 | ''' 2 | # Day 1: Data Types 3 | i = 4 4 | d = 4.0 5 | s = 'HackerRank ' 6 | # Declare second integer, double, and String variables. 7 | # Read and save an integer, double, and String to your variables. 8 | 9 | print(__dict__) 10 | import sys 11 | sys.exit(1) 12 | newI = 0 13 | newD = 0.0 14 | newS = "" 15 | try: 16 | newI, newD, newS = int(input()), float(input()), input() 17 | except: 18 | print("Error - Data-type mismatch.") 19 | else : 20 | # Print the sum of both integer variables on a new line. 21 | print(i + newI) 22 | # Print the sum of the double variables on a new line. 23 | print(d + newD) 24 | # Concatenate and print the String variables on a new line 25 | # The 's' variable above should be printed first. 26 | print(s + newS) 27 | ''' 28 | 29 | (lambda i, d, s: print( i+int(input()), d+float(input()), s+input(), sep='\n' )) ( 4, 4.0, 'HackerRank ' ) 30 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 02.py: -------------------------------------------------------------------------------- 1 | '''#!/bin/python3 2 | # Day 2: Operators 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the solve function below. 10 | def solve(meal_cost, tip_percent, tax_percent): 11 | result = meal_cost + meal_cost*(tip_percent/100) + meal_cost*(tax_percent/100) + 0.5 12 | print(f"{int(result)}") 13 | 14 | meal_cost = float(input()) 15 | tip_percent = int(input()) 16 | tax_percent = int(input()) 17 | 18 | solve(meal_cost, tip_percent, tax_percent) 19 | ''' 20 | 21 | (lambda meal_cost, tip_percent, tax_percent: print(int(meal_cost + meal_cost*(tip_percent/100) + meal_cost*(tax_percent/100) + 0.5)))(float(input()), int(input()), int(input())) 22 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 03.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #!/bin/python 3 | # compatible with python3 4 | 5 | n = int(input()) 6 | if(n%2==1) or n in range (5,21): 7 | print("Weird") 8 | else: 9 | print("Not Weird") 10 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 05.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #!/bin/python3 3 | import sys 4 | n = int(input().strip()) 5 | for i in range(1, 11): ##Loops from 1 to 10 6 | answer = i * n 7 | print(str(n) + " x " + str(i) + " = " + str(answer)) 8 | ##+ " x " + i + " = " + (N*i)) 9 | ''' 10 | (lambda n: print(*[ f'{n} x {i} = {n*i}' for i in range(1, 11)], sep='\n') ) (int(input())) 11 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 06.py: -------------------------------------------------------------------------------- 1 | ''' 2 | # Enter your code here. Read input from STDIN. Print output to STDOUT 3 | t = int(input().strip()) 4 | for _ in range(t): 5 | s = input().strip() 6 | n = len(s) 7 | 8 | even = [s[i] for i in range(0, n, 2)] # list of even chars 9 | odd = [s[i] for i in range(1, n, 2)] # list of odd chars 10 | 11 | print(''.join(even), ''.join(odd)) 12 | ''' 13 | 14 | (lambda n, apply: [print(''.join(apply(input()))) for _ in range(n) ] )( int(input()), lambda x : [x[i] for i in range(0, len(x), 2)] + [' '] + [x[i] for i in range(1, len(x), 2)] ) 15 | 16 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 07.py: -------------------------------------------------------------------------------- 1 | import math 2 | import os 3 | import random 4 | import re 5 | import sys 6 | 7 | 8 | 9 | if __name__ == '__main__': 10 | n = int(input()) 11 | 12 | arr = list(map(int, input().rstrip().split())) 13 | 14 | for i in range(n//2): 15 | arr[i], arr[-1 * (i + 1)] = arr[-1 * (i + 1)], arr[i] 16 | 17 | print(*arr) 18 | 19 | #(lambda _, arr : print(*arr[::-1], sep=' ') ) ( int(input()), list(map(int, input().strip().split())) ) 20 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 08.py: -------------------------------------------------------------------------------- 1 | phonebook = {} 2 | num = int(input()) 3 | for i in range(0, num): 4 | entry = str(input()).split(" ") 5 | name = entry[0] 6 | number = int(entry[1]) 7 | phonebook[name] = number 8 | 9 | for i in range(0, num): 10 | name = input() 11 | if name in phonebook: 12 | print(name + "=" + str(phonebook[name])) 13 | else: 14 | print("Not found") 15 | 16 | #( lambda phonebook : [ (lambda name: print( f'{name}={phonebook[name]}' if name in phonebook else 'Not found' ) )(input()) for _ in range(len(phonebook)) ] ) ( dict([ input().split() for i in range(int(input())) ] ) ) 17 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 09.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the factorial function below. 10 | def factorial(n): 11 | if n==1: 12 | return 1 13 | return n*factorial(n-1) 14 | 15 | if __name__ == '__main__': 16 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 17 | 18 | n = int(input()) 19 | 20 | result = factorial(n) 21 | 22 | fptr.write(str(result) + '\n') 23 | 24 | fptr.close() 25 | 26 | #print( __import__('math').factorial( int(input()) )) 27 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 10.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | s = "{0:b}".format(int(input())) 10 | result, run = 0, 0 11 | 12 | for c in s: 13 | if c == "1": 14 | run += 1 15 | result = max(result, run) 16 | else: 17 | run = 0 18 | print(result) 19 | 20 | #print(max( [len(i) for i in bin(int(input()))[2:].split('0')] )) 21 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 11.py: -------------------------------------------------------------------------------- 1 | 2 | import math 3 | import os 4 | import random 5 | import re 6 | import sys 7 | 8 | if __name__ == '__main__': 9 | arr = [] 10 | max = -99 11 | for _ in range(6): 12 | arr.append(list(map(int, input().rstrip().split()))) 13 | 14 | for i in range(6): 15 | for j in range(6): 16 | if ((i + 2 < 6) and (j + 2 < 6)): 17 | temp = arr[i][j] + arr[i + 2][j] + arr[i][j + 1] + arr[i + 1][j + 1] + arr[i + 2][j + 1] + arr[i][j + 2] + arr[i + 2][j + 2] 18 | if (temp > max): 19 | max = temp 20 | 21 | print(max) 22 | 23 | 24 | #(lambda arr: print(max( [ arr[i][j] + arr[i + 2][j] + arr[i][j + 1] + arr[i + 1][j + 1] + arr[i + 2][j + 1] + arr[i][j + 2] + arr[i + 2][j + 2] for i in range(6) for j in range(6) if ((i + 2 < 6) and (j + 2 < 6)) ] )) ) ( [ list(map(int, input().rstrip().split())) for _ in range(6) ] ) 25 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 13.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | class Book(object, metaclass=ABCMeta): 3 | def __init__(self,title,author): 4 | self.title=title 5 | self.author=author 6 | @abstractmethod 7 | def display(): pass 8 | class MyBook(Book): 9 | def __init__(self, title, author, price): 10 | super().__init__(title, author) 11 | self.price = price 12 | 13 | def display(self): 14 | print("Title:", self.title) 15 | print("Author:", self.author) 16 | print("Price:", self.price) 17 | 18 | title=input() 19 | author=input() 20 | price=int(input()) 21 | new_novel=MyBook(title,author,price) 22 | new_novel.display() 23 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 14.py: -------------------------------------------------------------------------------- 1 | class Difference: 2 | def __init__(self, a): 3 | self.__elements = a 4 | 5 | # Add your code heredef computeDifference(self): 6 | def computeDifference(self): 7 | self.maximumDifference = abs( 8 | max(self.__elements) - min(self.__elements)) 9 | 10 | # End of Difference class 11 | 12 | 13 | _ = input() 14 | a = [int(e) for e in input().split(' ')] 15 | 16 | d = Difference(a) 17 | d.computeDifference() 18 | 19 | print(d.maximumDifference) 20 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 15.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self,data): 3 | self.data = data 4 | self.next = None 5 | class Solution: 6 | def display(self,head): 7 | current = head 8 | while current: 9 | print(current.data,end=' ') 10 | current = current.next 11 | 12 | def insert(self,head,data): 13 | n = Node(data) 14 | if head: 15 | current = head 16 | while current.next: 17 | current = current.next 18 | current.next = n 19 | return head 20 | else: 21 | return n 22 | mylist= Solution() 23 | T=int(input()) 24 | head=None 25 | for i in range(T): 26 | data=int(input()) 27 | head=mylist.insert(head,data) 28 | mylist.display(head); -------------------------------------------------------------------------------- /30daysOFcode_Python/day 16.py: -------------------------------------------------------------------------------- 1 | t = input() 2 | try: 3 | print(int(t)) 4 | except: 5 | print("Bad String") -------------------------------------------------------------------------------- /30daysOFcode_Python/day 17.py: -------------------------------------------------------------------------------- 1 | #Write your code here 2 | class Calculator(): 3 | def power(self,n,p): 4 | self.n = n 5 | self.p = p 6 | if n < 0 or p < 0: 7 | raise Exception("n and p should be non-negative") 8 | else: 9 | return n**p 10 | 11 | myCalculator=Calculator() 12 | T=int(input()) 13 | for i in range(T): 14 | n,p = map(int, input().split()) 15 | try: 16 | ans=myCalculator.power(n,p) 17 | print(ans) 18 | except Exception as e: 19 | print(e) -------------------------------------------------------------------------------- /30daysOFcode_Python/day 19.py: -------------------------------------------------------------------------------- 1 | class AdvancedArithmetic(object): 2 | def divisorSum(n): 3 | raise NotImplementedError 4 | 5 | class Calculator(AdvancedArithmetic): 6 | def divisorSum(self, n): 7 | sum = 0 8 | for i in range(1, n+1): 9 | if(n%i == 0): 10 | sum += i 11 | return sum 12 | 13 | 14 | n = int(input()) 15 | my_calculator = Calculator() 16 | s = my_calculator.divisorSum(n) 17 | print("I implemented: " + type(my_calculator).__bases__[0].__name__) 18 | print(s) 19 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 20.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import sys 4 | 5 | n = int(input().strip()) 6 | a = list(map(int, input().strip().split(' '))) 7 | 8 | # Write Your Code Here 9 | def swap(a, x, y): 10 | t = a[x] 11 | a[x] = a[y] 12 | a[y] = t 13 | 14 | def bubbleSort(a): 15 | n = len(a) 16 | allSwapCount = 0 17 | for i in range(0, n): 18 | swapCount = 0 19 | 20 | for j in range(0, n -i- 1): 21 | 22 | if a[j] > a[j + 1]: 23 | swap(a, j, j + 1) 24 | swapCount += 1 25 | if swapCount == 0: 26 | break 27 | else: 28 | allSwapCount += swapCount 29 | return (a[0], a[n - 1], allSwapCount) 30 | 31 | 32 | first, last, swaps = bubbleSort(a) 33 | print("Array is sorted in", swaps, "swaps.") 34 | print("First Element:", first) 35 | 36 | print("Last Element:", last) 37 | 38 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 21.py: -------------------------------------------------------------------------------- 1 | Element = TypeVar("Element") 2 | 3 | 4 | def printArray(array: [Element]): 5 | for element in array: 6 | print(element) 7 | 8 | 9 | vInt = [1, 2, 3] 10 | vString = ["Hello", "World"] 11 | 12 | printArray(vInt) 13 | printArray(vString) 14 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 22.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self,data): 3 | self.right=self.left=None 4 | self.data = data 5 | class Solution: 6 | def insert(self,root,data): 7 | if root==None: 8 | return Node(data) 9 | else: 10 | if data<=root.data: 11 | cur=self.insert(root.left,data) 12 | root.left=cur 13 | else: 14 | cur=self.insert(root.right,data) 15 | root.right=cur 16 | return root 17 | 18 | def getHeight(self,root): 19 | if(root == None): 20 | return -1 21 | return 1 + max( self.getHeight(root.left) , self.getHeight(root.right) ) 22 | 23 | T=int(input()) 24 | myTree=Solution() 25 | root=None 26 | for i in range(T): 27 | data=int(input()) 28 | root=myTree.insert(root,data) 29 | height=myTree.getHeight(root) 30 | print(height) -------------------------------------------------------------------------------- /30daysOFcode_Python/day 25.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | def isPrime(n): 4 | 5 | if n < 2: 6 | return False 7 | i = 2 8 | while i**2 <= n: 9 | if n % i: 10 | i += 1 11 | else: 12 | return False 13 | return True 14 | 15 | n = int(input()) 16 | values = [int(input()) for _ in range(n)] 17 | 18 | for v in values: 19 | if isPrime(v): 20 | print("Prime") 21 | else: 22 | print("Not prime") 23 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 26.py: -------------------------------------------------------------------------------- 1 | rd, rm, ry = [int(x) for x in input().split(' ')] 2 | ed, em, ey = [int(x) for x in input().split(' ')] 3 | 4 | if (ry, rm, rd) <= (ey, em, ed): 5 | print(0) 6 | elif (ry, rm) == (ey, em): 7 | print(15 * (rd - ed)) 8 | elif ry == ey: 9 | print(500 * (rm - em)) 10 | else: 11 | print(10000) 12 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 28.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import re 4 | 5 | result = list() 6 | 7 | for _ in range(int(input())): 8 | firstNameEmailID = input().split() 9 | firstName = firstNameEmailID[0] 10 | emailID = firstNameEmailID[1] 11 | 12 | if re.compile("@gmail.com$").search(emailID): 13 | result.append(firstName) 14 | 15 | for name in sorted(result): 16 | print(name) 17 | -------------------------------------------------------------------------------- /30daysOFcode_Python/day 29.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | def solve(n, k): 11 | if not k % 2 and k|k-1 > n: 12 | return k-2 13 | return k-1 14 | 15 | for _ in range(int(input())): 16 | n, k = list(map(int, input().split())) 17 | print(solve(n, k)) 18 | -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 00.rb: -------------------------------------------------------------------------------- 1 | # Read a full line of input from stdin and save it to our dynamically typed variable, input_string. 2 | input_string = gets 3 | 4 | # Print a string literal saying "Hello, World." to stdout. 5 | puts 'Hello, World.' 6 | 7 | # TODO: Write a line of code here that prints the contents of input_string to stdout. 8 | 9 | puts input_string -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 01.rb: -------------------------------------------------------------------------------- 1 | # Declare second integer, double, and String variables. 2 | 3 | # Read and save an integer, double, and String to your variables. 4 | 5 | # Print the sum of both integer variables on a new line. 6 | 7 | # Print the sum of the double variables on a new line. 8 | 9 | # Concatenate and print the String variables on a new line 10 | # The 's' variable above should be printed first. 11 | 12 | i2 = gets.strip.to_i 13 | d2 = gets.strip.to_f 14 | s2 = gets.strip 15 | puts (i2+i), (d2+d), s+s2 16 | -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 02.rb: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | meal = gets.to_f 4 | tip = (gets.to_f / 100) * meal 5 | tax = (gets.to_f / 100) * meal 6 | 7 | total = (meal + tip + tax).round 8 | 9 | puts "The total meal cost is #{ total } dollars." 10 | -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 03.rb: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | N = gets.strip.to_i 4 | if N % 2 == 0 5 | if (N >= 2 && N <= 5) || N > 20 6 | puts 'Not Weird' 7 | elsif (N >= 6 && N <= 20) 8 | puts 'Weird' 9 | end 10 | else 11 | puts 'Weird' 12 | end 13 | 14 | -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 04.rb: -------------------------------------------------------------------------------- 1 | class Person 2 | attr_accessor :age 3 | def initialize(initialAge) 4 | if initialAge>0 5 | @age=initialAge 6 | else 7 | @age=0 8 | puts "Age is not valid, setting age to 0." 9 | end 10 | end 11 | def amIOld() 12 | # Do some computations in here and print out the correct statement to the console 13 | if @age<13 14 | puts "You are young." 15 | elsif 13<=@age && @age<18 16 | puts "You are a teenager." 17 | else 18 | puts "You are old." 19 | end 20 | end 21 | def yearPasses() 22 | # Increment the age of the person in here 23 | @age+=1 24 | end 25 | end -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 05.rb: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | n = gets.to_i 4 | 5 | for i in 1..10 6 | puts "#{n} x #{i} = #{n*i}" 7 | end -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 06.rb: -------------------------------------------------------------------------------- 1 | t = gets.to_i 2 | loop do 3 | s = gets 4 | s.delete!("\n") 5 | for i in (0..s.length).step(2) do 6 | print s[i] 7 | STDOUT.flush 8 | end 9 | print " " 10 | STDOUT.flush 11 | for i in (1..s.length).step(2) do 12 | print s[i] 13 | STDOUT.flush 14 | end 15 | puts 16 | t -= 1 17 | if t == 0 18 | break 19 | end 20 | end -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 07.rb: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | require 'json' 4 | require 'stringio' 5 | 6 | n = gets.to_i 7 | arr = gets.rstrip.split(' ').map(&:to_i) 8 | 9 | puts arr.reverse.join(" ") -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 08.rb: -------------------------------------------------------------------------------- 1 | hashMap = {} 2 | 3 | n = gets.chomp.to_i 4 | 5 | for x in 0..n-1 6 | arr = gets.chomp.split(" ") 7 | hashMap[arr[0]] = arr[1] 8 | end 9 | 10 | while true 11 | query = gets 12 | break unless query 13 | name = query.chomp 14 | if hashMap.key?(name) 15 | puts "#{name}=#{hashMap[name]}" 16 | else 17 | puts "Not found" 18 | end 19 | end -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 09.rb: -------------------------------------------------------------------------------- 1 | N = gets.to_i 2 | 3 | def factorial(num) 4 | if num == 0 || num == 1 5 | return 1 6 | end 7 | return num * factorial(num-1) 8 | end 9 | 10 | print factorial N -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 10.rb: -------------------------------------------------------------------------------- 1 | n = gets.to_i 2 | sum = 0 3 | max = 0 4 | 5 | while n > 0 6 | if n.odd? 7 | sum += 1 8 | max = sum if sum > max 9 | else sum = 0 10 | end 11 | n /= 2 12 | end 13 | 14 | print max -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 11.rb: -------------------------------------------------------------------------------- 1 | arr = Array.new(6) 2 | max = -10000000 3 | 4 | 6.times do |i| 5 | arr[i] = gets.rstrip.split(' ').map(&:to_i) 6 | end 7 | 8 | 6.times do |i| 9 | 6.times do |j| 10 | if (j + 2) < 6 and (i + 2) < 6 11 | sum = (arr[i][j] + arr[i][j + 1] + arr[i][j + 2]) + 12 | (arr[i + 1][j + 1]) + (arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]) 13 | 14 | if sum > max 15 | max = sum 16 | end 17 | end 18 | end 19 | end 20 | 21 | puts max -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 13.rb: -------------------------------------------------------------------------------- 1 | class Book 2 | attr_accessor :title 3 | attr_accessor :author 4 | 5 | def initialize(title, author) 6 | raise 'You cannot instantiate an abstract class.' 7 | end 8 | 9 | def display 10 | raise 'You must override this method in your implementing class.' 11 | end 12 | end 13 | 14 | class MyBook < Book 15 | attr_accessor :price 16 | 17 | def initialize(title, author, price) 18 | @title = title 19 | @author = author 20 | @price = price 21 | end 22 | 23 | def display() 24 | print "Title: #{self.title}" 25 | print "Author: #{self.author}" 26 | print "Price: #{self.price}" 27 | end 28 | end 29 | 30 | title = gets 31 | author = gets 32 | price = gets 33 | 34 | new_novel = MyBook.new(title, author, price) 35 | new_novel.display -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 15.rb: -------------------------------------------------------------------------------- 1 | class Node 2 | 3 | attr_accessor :data,:next 4 | 5 | def initialize data 6 | @data = data 7 | @next = nil 8 | end 9 | end 10 | 11 | class Solution 12 | def insert(head,value) 13 | #Complete this method 14 | if head.nil? 15 | head = Node.new value 16 | else 17 | curr= head 18 | until (curr.next.nil?) do 19 | curr = curr.next 20 | end 21 | curr.next = Node.new value 22 | end 23 | 24 | head 25 | end 26 | def display(head) 27 | current = head 28 | while current 29 | print current.data," " 30 | current = current.next 31 | end 32 | end 33 | end 34 | 35 | mylist= Solution.new 36 | head=nil 37 | T=gets.to_i 38 | for i in 1..T 39 | data=gets.to_i 40 | head=mylist.insert(head,data) 41 | end 42 | mylist.display(head) -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 17.rb: -------------------------------------------------------------------------------- 1 | class RangeError < StandardError 2 | end 3 | 4 | # Write your code here 5 | class Calculator 6 | def power(n,p) 7 | if n < 0 || p < 0 8 | raise RangeError.new "n and p should be non-negative" 9 | else 10 | return n**p 11 | end 12 | end 13 | end 14 | 15 | myCalculator = Calculator.new() 16 | 17 | t = gets.to_i 18 | 19 | for i in 1..t 20 | n, p = gets.split.map(&:to_i) 21 | begin 22 | ans = myCalculator.power(n, p) 23 | puts ans.to_i 24 | rescue RangeError => e 25 | puts e.message 26 | end 27 | end -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 20.rb: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | n = gets.strip.to_i 3 | a = gets.strip 4 | a = a.split(' ').map(&:to_i) 5 | # Write Your Code Here 6 | i=0 7 | numSwaps=0 8 | while n>i do 9 | j=0 10 | while n-1>j do 11 | if a[j]>a[j+1] 12 | a[j], a[j+1] = a[j+1], a[j] 13 | numSwaps+=1 14 | end 15 | j+=1 16 | end 17 | if numSwaps==0 18 | break 19 | end 20 | i+=1 21 | end 22 | puts "Array is sorted in #{numSwaps} swaps." 23 | puts "First Element: #{a[0]}" 24 | puts "Last Element: #{a[n-1]}" -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 26.rb: -------------------------------------------------------------------------------- 1 | line = $stdin.readline() 2 | date = line.split(" ") 3 | 4 | aday = date[0].to_i 5 | amonth = date[1].to_i 6 | ayear = date[2].to_i 7 | 8 | line2 = $stdin.readline() 9 | date2 = line2.split(" ") 10 | 11 | bday = date2[0].to_i 12 | bmonth = date2[1].to_i 13 | byear = date2[2].to_i 14 | 15 | result = 0 16 | 17 | cday = aday - bday 18 | cmonth = amonth - bmonth 19 | cyear = ayear - byear 20 | 21 | if cyear > 0 22 | result = 10000 23 | elsif cmonth > 0 and cyear == 0 24 | result = 500 * cmonth 25 | elsif cday > 0 and cmonth == 0 26 | result = 15 * cday 27 | end 28 | puts result 29 | -------------------------------------------------------------------------------- /30daysOFcode_Ruby/day 29.rb: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | 3 | t = gets.strip.to_i #test cases 4 | for a0 in (0..t-1) 5 | n,k = gets.strip.split(' ') 6 | n = n.to_i #biggest number 7 | k = k.to_i #sup bitwise and (not to be reached) of two numbers <= n 8 | biggest_and = k - 1 9 | man = biggest_and.to_s(2).split("").map { |x| x.to_i }.reverse 10 | change = man.find_index { |e| e.zero? } 11 | if change 12 | man[change] = 1 13 | else 14 | man << 1 15 | end 16 | biggest_and -=1 if man.reverse.join.to_i(2) > n 17 | puts biggest_and 18 | end 19 | -------------------------------------------------------------------------------- /30daysOFcode_Rust/day 00.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | fn main() { 3 | let mut line = String::new(); 4 | io::stdin() 5 | .read_line(&mut line) 6 | .expect("Failed to read line."); 7 | println!("Hello, World.\n{}", line); 8 | } 9 | -------------------------------------------------------------------------------- /30daysOFcode_Rust/day 01.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | 3 | pub fn solution() { 4 | // Some already declared and initialized variables (Task's specific) 5 | let i : i32 = 5; 6 | let d : f32 = 2.5; 7 | let s : String = String::from(", World!"); 8 | 9 | let mut res: Vec = Vec::new(); 10 | for idx in 0..3 { 11 | let mut input : String = String::new(); 12 | io::stdin().read_line(&mut input) 13 | .expect("Failed to read line"); 14 | 15 | if idx == 0 { 16 | let val: i32 = input.trim().parse().expect("Please type an integer!"); 17 | res.push(format!("{}", val + i)); 18 | } else if idx == 1 { 19 | let val: f32 = input.trim().parse().expect("Please type a double!"); 20 | let fmt_sum = format!("{:.*}", 1, val + d); 21 | res.push(format!("{}", fmt_sum)); 22 | } else { 23 | let val: String = input.trim().to_string(); 24 | res.push(format!("{}", val + &s)); 25 | } 26 | } 27 | 28 | for e in &res { 29 | println!("{}", e); 30 | } 31 | } -------------------------------------------------------------------------------- /30daysOFcode_Rust/day 02.rs: -------------------------------------------------------------------------------- 1 | use math::round; 2 | 3 | pub fn solution() { 4 | // Some already declared and initialized variables (Task's specific) 5 | let meal_cost : f64 = 15.50; 6 | let tip_percent : i32 = 15; 7 | let tax_percent : i32 = 10; 8 | 9 | let tip: f64 = meal_cost * tip_percent as f64 / 100.0; 10 | let tax: f64 = meal_cost * tax_percent as f64 / 100.0; 11 | let total_cost: f64 = meal_cost + tip + tax; 12 | let total_cost_rounded = round::floor(total_cost, 0); 13 | println!("{}", total_cost_rounded); 14 | } 15 | -------------------------------------------------------------------------------- /30daysOFcode_Rust/day 03.rs: -------------------------------------------------------------------------------- 1 | pub fn solution() { 2 | // Some already declared and initialized variables (Task's specific) 3 | // Constraints: 1 <= n <= 100 4 | let n : i32 = 24; 5 | 6 | if n % 2 != 0 || (6 <= n && n <= 20) { 7 | println!("Weird"); 8 | } else if (2 <= n && n <= 5) || n > 20 { 9 | println!("Not Weird"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /30daysOFcode_Rust/day 05.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | //while true 3 | 4 | let mut x = 0; 5 | loop { 6 | x+=1; 7 | println!("x={}",x); 8 | 9 | if x==15 { 10 | break; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /30daysOFcode_Rust/day 06.rs: -------------------------------------------------------------------------------- 1 | pub fn solution() { 2 | let s = process("Hacker"); 3 | println!("{}", s); 4 | assert_eq!(s, "Hce akr"); 5 | 6 | let s = process("Rank"); 7 | println!("{}", s); 8 | assert_eq!(s, "Rn ak"); 9 | } 10 | 11 | fn process(text: &str) -> String { 12 | let mut even_chars: String = String::from(""); 13 | let mut odd_chars: String = String::from(""); 14 | 15 | for (idx, ch) in text.char_indices() { 16 | if (idx + 1) % 2 == 0 { 17 | even_chars.push(ch); 18 | } else { 19 | odd_chars.push(ch); 20 | } 21 | } 22 | 23 | return format!("{} {}", odd_chars, even_chars); 24 | } 25 | -------------------------------------------------------------------------------- /30daysOFcode_Rust/day 07.rs: -------------------------------------------------------------------------------- 1 | use super::util; 2 | 3 | pub fn solution() { 4 | let expected_results = util::read_expected_results("7"); 5 | let (n, lines) = util::read_input_lines_with_n("7"); 6 | 7 | let line = lines.get(0); 8 | let integers: Vec = line.unwrap() 9 | .split_whitespace() 10 | .map(|s| s.parse().unwrap()) 11 | .collect(); 12 | 13 | let mut out = String::from(""); 14 | for (i, _) in integers.iter().enumerate() { 15 | let idx = n - i - 1; 16 | out.push_str(&integers.get(idx).unwrap().to_string()); 17 | out.push_str(" "); 18 | } 19 | 20 | assert_eq!(expected_results.get(0).unwrap(), &out.trim()); 21 | } 22 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 00.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | def main(args: Array[String]) { 3 | // Print "Hello, World." 4 | println("Hello, World.") 5 | 6 | // Read a string variable 7 | val s = scala.io.StdIn.readLine() 8 | 9 | // Print the value of the string variable 10 | println(s) 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 01.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | def main(args: Array[String]) { 3 | val i = 4 4 | val d = 4.0 5 | val s = "HackerRank " 6 | 7 | // Read values of another integer, double, and string variables 8 | // Note use scala.io.StdIn to read from stdin 9 | val a = scala.io.StdIn.readLine().toInt 10 | val b = scala.io.StdIn.readLine().toDouble 11 | val c = scala.io.StdIn.readLine() 12 | val sum_1=a+i 13 | val sum_2=b+d 14 | 15 | // Print the sum of both the integer variables 16 | println(sum_1) 17 | // Print the sum of both the double variables 18 | println(sum_2) 19 | // Concatenate and print the string variables 20 | // The 's' variable above should be printed first. 21 | println(s+c) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 02.scala: -------------------------------------------------------------------------------- 1 | import java.io._ 2 | import java.math._ 3 | import java.security._ 4 | import java.text._ 5 | import java.util._ 6 | import java.util.concurrent._ 7 | import java.util.function._ 8 | import java.util.regex._ 9 | import java.util.stream._ 10 | 11 | object Solution { 12 | 13 | // Complete the solve function below. 14 | def solve(meal_cost: Double, tip_percent: Int, tax_percent: Int) { 15 | 16 | 17 | val total=(meal_cost*tip_percent/100)+(meal_cost*tax_percent/100) 18 | val ans=(meal_cost+total).round 19 | println (ans) 20 | } 21 | 22 | def main(args: Array[String]) { 23 | val stdin = scala.io.StdIn 24 | 25 | val meal_cost = stdin.readLine.trim.toDouble 26 | 27 | val tip_percent = stdin.readLine.trim.toInt 28 | 29 | val tax_percent = stdin.readLine.trim.toInt 30 | 31 | solve(meal_cost, tip_percent, tax_percent) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 03.scala: -------------------------------------------------------------------------------- 1 | import java.io._ 2 | import java.math._ 3 | import java.security._ 4 | import java.text._ 5 | import java.util._ 6 | import java.util.concurrent._ 7 | import java.util.function._ 8 | import java.util.regex._ 9 | import java.util.stream._ 10 | 11 | object Solution { 12 | 13 | 14 | 15 | def main(args: Array[String]) { 16 | val stdin = scala.io.StdIn 17 | 18 | val n = stdin.readLine.trim.toInt 19 | 20 | if(n%2!=0){ 21 | println("Weird")} 22 | else if(n%2==0 && n>=2 && n<=5){ 23 | println("Not Weird")} 24 | else if(n%2==0 && n>=6 && n<=20){ 25 | println("Weird")} 26 | else{ 27 | println("Not Weird")} 28 | 29 | 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 05.scala: -------------------------------------------------------------------------------- 1 | import java.io._ 2 | import java.math._ 3 | import java.security._ 4 | import java.text._ 5 | import java.util._ 6 | import java.util.concurrent._ 7 | import java.util.function._ 8 | import java.util.regex._ 9 | import java.util.stream._ 10 | 11 | object Solution { 12 | 13 | 14 | 15 | def main(args: Array[String]) { 16 | val stdin = scala.io.StdIn 17 | 18 | val n = stdin.readLine.trim.toInt 19 | for(i <- 1 to 10 ){ 20 | println(n+" x "+i+" = "+ n*i) 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 06.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution 5 | */ 6 | 7 | val stdin = scala.io.StdIn 8 | 9 | var t = stdin.readLine.trim.toInt 10 | 11 | while(t>0){ 12 | val str = stdin.readLine.trim 13 | 14 | for(i<- 0 to str.length-1){ 15 | if(i%2==0){ 16 | print(str(i))} 17 | } 18 | print(" ") 19 | 20 | for(i<- 0 to str.length-1){ 21 | if(i%2!=0){ 22 | print(str(i))} 23 | } 24 | println("") 25 | 26 | t=t-1} 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 07.scala: -------------------------------------------------------------------------------- 1 | import java.io._ 2 | import java.math._ 3 | import java.security._ 4 | import java.text._ 5 | import java.util._ 6 | import java.util.concurrent._ 7 | import java.util.function._ 8 | import java.util.regex._ 9 | import java.util.stream._ 10 | 11 | object Solution { 12 | 13 | 14 | 15 | def main(args: Array[String]) { 16 | val stdin = scala.io.StdIn 17 | 18 | val n = stdin.readLine.trim.toInt 19 | 20 | val arr = stdin.readLine.split(" ").map(_.trim.toInt) 21 | 22 | for(i<- n-1 to 0 by -1){ 23 | print(arr(i)+" ") 24 | 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 08.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution 5 | */ 6 | var str :String=null 7 | val stdin = scala.io.StdIn 8 | val map = scala.collection.mutable.Map[String,String]() 9 | 10 | var t = stdin.readLine.trim.toInt 11 | while(t>0){ 12 | str = stdin.readLine.trim 13 | val key=str.split(" ").array(0) 14 | val value=str.split(" ").array(1) 15 | map(key)=value 16 | t=t-1 17 | } 18 | var flag=false 19 | 20 | 21 | while(flag==false){ 22 | try{ 23 | str = stdin.readLine.trim}catch{ 24 | case x: Throwable => {flag=true}} 25 | if(str!=null && flag==false){ 26 | try{println(str+"="+map(str))} 27 | catch{ 28 | case x => {println("Not found")} 29 | } 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 09.scala: -------------------------------------------------------------------------------- 1 | import java.io._ 2 | import java.math._ 3 | import java.security._ 4 | import java.text._ 5 | import java.util._ 6 | import java.util.concurrent._ 7 | import java.util.function._ 8 | import java.util.regex._ 9 | import java.util.stream._ 10 | 11 | object Solution { 12 | 13 | // Complete the factorial function below. 14 | def factorial(n: Int): Int = { 15 | if(n==1){ 16 | return 1} 17 | return n*factorial(n-1) 18 | 19 | } 20 | 21 | def main(args: Array[String]) { 22 | val stdin = scala.io.StdIn 23 | 24 | val printWriter = new PrintWriter(sys.env("OUTPUT_PATH")) 25 | 26 | val n = stdin.readLine.trim.toInt 27 | 28 | val result = factorial(n) 29 | 30 | printWriter.println(result) 31 | 32 | printWriter.close() 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 18.scala: -------------------------------------------------------------------------------- 1 | import scala.collection.mutable.ListBuffer 2 | class Solution { 3 | //Write your code here 4 | var lb = new ListBuffer[Char]; 5 | var ch = 'a'; 6 | def outputListBuffer() = { 7 | println(lb) 8 | } 9 | def pushCharacter(c: Char) = { 10 | lb += c; 11 | } 12 | def enqueueCharacter(c: Char) = { 13 | //lb.insert(0, c); 14 | } 15 | def popCharacter() : Char = { 16 | ch = lb.last; 17 | if ( lb.size < 2 ) { 18 | return ch; 19 | } 20 | try { 21 | ch = lb.remove(lb.size-1); 22 | } catch { 23 | case _: Throwable => 24 | } 25 | ch 26 | } 27 | def dequeueCharacter() : Char = { 28 | ch = ' '; 29 | try { 30 | ch = lb.remove(0); 31 | } catch { 32 | case _: Throwable => 33 | } 34 | ch 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /30daysOFcode_Scala/day 29.scala: -------------------------------------------------------------------------------- 1 | object Solution { 2 | 3 | def main(args: Array[String]) { 4 | val sc = new java.util.Scanner (System.in); 5 | var t = sc.nextInt(); 6 | var a0 = 0; 7 | while(a0 < t){ 8 | var n = sc.nextInt(); 9 | var k = sc.nextInt(); 10 | a0+=1; 11 | val list = List.range(1, n+1, 1) 12 | var max = 0 13 | for (i <- 1 to n-1) { 14 | for (j <- i+1 to n) { 15 | // println(s"a: ${i} b: ${j}: (a&b): ${(i & j)}") 16 | if ((i & j) > max && (i & j) < k) { 17 | 18 | max = (i & j) 19 | } 20 | } 21 | } 22 | println(max) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 00.swift: -------------------------------------------------------------------------------- 1 | // Day 0: Hello, World! 2 | // Read the user inout string from readLine() and save it in a String variable, input_string. 3 | 4 | import Foundation 5 | 6 | // Displays Hello, World! 7 | print("Hello, World!") 8 | 9 | // TODO: Enter the string to be displayed 10 | var input_string = readLine() 11 | print(input_string!) 12 | 13 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 01.swift: -------------------------------------------------------------------------------- 1 | // Day 1: Data Types 2 | 3 | var anInt:Int = 3 4 | // by default the decimal values in Swift are considered as Double 5 | var aDouble = 5.6 6 | var aFloat:Float = 3.4 7 | var str = "Hackerrank_30daysOFcode" 8 | 9 | var inInt = Int(readLine()!) 10 | var inDouble = Double(readLine()!) 11 | var inFloat = Float(readLine()!) 12 | var inStr = readLine() 13 | 14 | // prints the sum of the input integer with the initilaised integer 15 | print(anInt + inInt!) 16 | // prints the sum of the input double with the initilaised double 17 | print(aDouble + inDouble!) 18 | // prints the sum of the input float with the initilaised float 19 | print(aFloat + inFloat!) 20 | // prints the sum of the input string with the initilaised string 21 | print(str + inStr!) 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 02.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | let mealCost = Double(readLine()!)!; // original meal price 4 | let tipPercent = Double(readLine()!)!; // tip percentage 5 | let taxPercent = Double(readLine()!)!; // tax percentage 6 | 7 | // Calculate Tax and Tip doubles: 8 | let tip = mealCost * tipPercent / 100; 9 | let tax = mealCost * taxPercent / 100; 10 | 11 | // cast the result of the rounding operation to an int and save it as totalCost 12 | let totalCost = Int(round(mealCost + tax + tip)); 13 | 14 | print("The total meal cost is \(totalCost) dollars."); 15 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 03.swift: -------------------------------------------------------------------------------- 1 | //Day 3 - Intro to connditional Statements 2 | 3 | import Foundation 4 | 5 | let n:Int = Int(readLine()!)! 6 | 7 | if n % 2 != 0 { 8 | print("Weird") 9 | } 10 | else{ 11 | //nested if-else 12 | if n>=2 && n<=5 { 13 | print("Not Weird") 14 | } 15 | else if n>=6 && n<=20{ 16 | print("Weird") 17 | } 18 | else if n>20{ 19 | print("Not Weird") 20 | } 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 04.swift: -------------------------------------------------------------------------------- 1 | // Day 5 - Loops 2 | 3 | import Foundation 4 | 5 | let a = Int(readLine()!)! 6 | 7 | // loop from 1 to 10 8 | for i in 1...10{ 9 | print("\(a) X \(i) = \(a*i)") 10 | } 11 | 12 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 05.swift: -------------------------------------------------------------------------------- 1 | // Day 5 - Loops 2 | 3 | import Foundation 4 | 5 | let a = Int(readLine()!)! 6 | 7 | // loop from 1 to 10 8 | for i in 1...10{ 9 | print("\(a) X \(i) = \(a*i)") 10 | } 11 | 12 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 06.swift: -------------------------------------------------------------------------------- 1 | // Day - 6 Let's review 2 | 3 | import Foundation 4 | let t = Int(readLine()!)! 5 | 6 | for i in 1...t{ 7 | var str = String(readLine()!) 8 | var len = str.count 9 | // var string:[Character] = str 10 | 11 | var even:[Character] = [] 12 | var odd:[Character] = [] 13 | var i = 0 14 | 15 | for char in str{ 16 | if(i % 2 == 0) { 17 | even.append(char) 18 | } 19 | else{ 20 | odd.append(char) 21 | } 22 | i += 1 23 | } 24 | 25 | let even_string = String(even) 26 | let odd_string = String(odd) 27 | 28 | print("\(even_string) \(odd_string)") 29 | 30 | } 31 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 07.swift: -------------------------------------------------------------------------------- 1 | // Day 7 - Arrays 2 | 3 | import Foundation 4 | 5 | 6 | var size = Int(readLine()!)! 7 | 8 | var str = String(readLine()!) 9 | var arr = [Int]() 10 | 11 | for i in str.reversed(){ 12 | if i != " " { 13 | arr.append(Int(String(i))!) 14 | } 15 | } 16 | for i in arr{ 17 | print(i, terminator: " ") 18 | } 19 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 08.swift: -------------------------------------------------------------------------------- 1 | // Day 08 swift 2 | // read input 3 | var n = Int(readLine()!)! 4 | // phone number 5 | var phoneBook = [String: String]() 6 | 7 | for i in 0...n - 1 { 8 | // read contacts into phone book 9 | var contact = readLine()!.characters.split(separator: " ").map(String.init) 10 | phoneBook[contact[0]] = contact[1] 11 | } 12 | 13 | var queryName = readLine() 14 | while queryName != nil { 15 | if let number = phoneBook[queryName!] { 16 | print("\(queryName!)=\(phoneBook[queryName!]!)") 17 | } 18 | else { 19 | print("Not found") 20 | } 21 | queryName = readLine() 22 | } 23 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 09.swift: -------------------------------------------------------------------------------- 1 | let n = Int(readLine()!)! 2 | 3 | func factorial(_ n: Int) -> Int { 4 | if n == 1 { 5 | return 1 6 | } 7 | 8 | return factorial(n - 1) * n; 9 | } 10 | 11 | print(factorial(n)) 12 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 10.swift: -------------------------------------------------------------------------------- 1 | var n = Int(readLine()!)! 2 | 3 | var sum = 0 4 | var max = 0 5 | 6 | while n > 0 { 7 | if n % 2 == 1 { 8 | sum += 1 9 | 10 | if sum > max { 11 | max = sum 12 | } 13 | 14 | } else { 15 | sum = 0 16 | } 17 | 18 | n = n / 2 19 | } 20 | 21 | print(max) 22 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 12.swift: -------------------------------------------------------------------------------- 1 | // Class Student 2 | class Student: Person { 3 | var testScores: [Int] 4 | 5 | // Write the Student class initializer 6 | init(firstName: String, lastName: String, identification: Int, scores: [Int]) { 7 | self.testScores = scores; 8 | super.init(firstName: firstName, lastName: lastName, identification: identification); 9 | } 10 | 11 | // Write the calculate method 12 | func calculate() -> String { 13 | var average = 0; 14 | for i in testScores { 15 | average += i; 16 | } 17 | average = average / testScores.count; 18 | 19 | if(average >= 90) { 20 | return "O"; // Outstanding 21 | } 22 | else if(average >= 80) { 23 | return "E"; // Exceeds Expectations 24 | } 25 | else if(average >= 70) { 26 | return "A"; // Acceptable 27 | } 28 | else if(average >= 55) { 29 | return "P"; // Poor 30 | } 31 | else if(average >= 40) { 32 | return "D"; // Dreadful 33 | } 34 | else { 35 | return "T"; // Troll 36 | } 37 | } 38 | 39 | } // End of class Student 40 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 14.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | class Difference { 4 | private var elements = [Int]() 5 | var maximumDifference: Int 6 | 7 | init(a: [Int]) { 8 | elements = a 9 | maximumDifference = 0 10 | } 11 | 12 | func computeDifference() { 13 | maximumDifference = elements.reduce(0) { result, element in 14 | max(result, elements.map { abs(element - $0) }.max() ?? 0) 15 | } 16 | } 17 | } // End of Difference class 18 | 19 | let n = Int(readLine()!)! 20 | let a = readLine()!.components(separatedBy: " ").map{ Int($0)! } 21 | 22 | let d = Difference(a: a) 23 | 24 | d.computeDifference() 25 | 26 | print(d.maximumDifference) 27 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 15.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | class Node { 4 | let data: Int 5 | var next: Node? 6 | 7 | init(data: Int) { 8 | self.data = data 9 | } 10 | } 11 | 12 | func insert(head: Node?, data: Int!) -> Node? { 13 | // Enter your code here. 14 | if(head == nil) { 15 | return Node(data: data) 16 | } 17 | let node = Node(data: data) 18 | var current = head 19 | while current?.next != nil { 20 | current = current!.next 21 | } 22 | current?.next = node 23 | return head 24 | } 25 | 26 | func display(head: Node?) { 27 | var current = head 28 | 29 | while current != nil { 30 | print(current!.data, terminator: " ") 31 | current = current!.next 32 | } 33 | } 34 | 35 | var head: Node? 36 | let n: Int = Int(readLine()!)! 37 | 38 | for _ in 0.. Int { 14 | guard let int = Int(inputString) else { throw StringToIntTypecastingError.BadString } 15 | return int 16 | } 17 | 18 | /* 19 | * Read the input 20 | */ 21 | let inputString = readLine()! 22 | 23 | do { 24 | try print(stringToInt(inputString: inputString)) 25 | } catch StringToIntTypecastingError.BadString { 26 | print("Bad String") 27 | } 28 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 17.swift: -------------------------------------------------------------------------------- 1 | class Calculator { 2 | // Start of function power 3 | func power(n: Int, p: Int) throws -> Int { 4 | // Add your code here 5 | if (n < 0 || p < 0) { 6 | throw RangeError.NotInRange("n and p should be non-negative") 7 | } 8 | var result = 1 9 | for _ in 0..

{ 2 | var items = [Element]() 3 | mutating func push(_ item: Element) { 4 | items.append(item); 5 | } 6 | mutating func pop() -> Element { 7 | return items.removeLast(); 8 | } 9 | } 10 | 11 | struct Queue { 12 | var items = [Element]() 13 | mutating func enqueue(_ item: Element) { 14 | items.append(item); 15 | } 16 | mutating func dequeue() -> Element { 17 | return items.removeFirst(); 18 | } 19 | } 20 | 21 | 22 | class Solution { 23 | 24 | var stack = Stack(); 25 | var queue = Queue(); 26 | 27 | func pushCharacter(ch: Character){ 28 | stack.push(ch); 29 | } 30 | 31 | func popCharacter() -> Character { 32 | return stack.pop(); 33 | } 34 | 35 | func enqueueCharacter(ch: Character){ 36 | queue.enqueue(ch); 37 | } 38 | 39 | func dequeueCharacter() -> Character { 40 | return queue.dequeue(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 20.swift: -------------------------------------------------------------------------------- 1 | // Bubble Sort Solution 2 | import Foundation 3 | 4 | // read the integer n 5 | let n = Int(readLine()!)! 6 | 7 | // read the array 8 | var arr = readLine()!.components(separatedBy: " ").map{ Int($0)! } 9 | 10 | // SOLUTION: 11 | var numberOfSwaps = 0 12 | 13 | public func bubbleSort (_ elements: [T]) -> [T] where T: Comparable { 14 | return bubbleSort(elements, <) 15 | } 16 | 17 | public func bubbleSort (_ elements: [T], _ comparison: (T,T) -> Bool) -> [T] { 18 | var array = elements 19 | 20 | for i in 0.. { 2 | func printArray(array: [T]) { 3 | array.forEach { array.forEach { print($0) } } 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 23.swift: -------------------------------------------------------------------------------- 1 | var collection:[String] = [String]() 2 | var queue = Array() 3 | queue.append(root!) 4 | while queue.count > 0 { 5 | let current:Node = queue.removeFirst() 6 | collection.append(String(current.data)) 7 | if (current.left != nil) { 8 | queue.append(current.left!) 9 | } 10 | if (current.right != nil) { 11 | queue.append(current.right!) 12 | } 13 | } 14 | print(collection.joinWithSeparator(" ")) 15 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 25.swift: -------------------------------------------------------------------------------- 1 | // Enter your code here 2 | func findPrime(input:Int) -> String { 3 | if input <= 1 { 4 | return "Not prime" 5 | } 6 | if input <= 3 { 7 | return "Prime" 8 | } 9 | var i = 2 10 | while i*i <= input { 11 | if input % i == 0 { 12 | return "Not prime" 13 | } 14 | i = i + 1 15 | } 16 | return "Prime" 17 | } 18 | 19 | var t = Int(readLine()!)! 20 | 21 | for _ in 0...t-1 { 22 | print(findPrime(Int(readLine()!)!)) 23 | } 24 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 28.swift: -------------------------------------------------------------------------------- 1 | // Enter your code here 2 | import Foundation 3 | var n = Int(readLine()!)! 4 | var rows = [String]() 5 | while n-- > 0 { 6 | rows.append(readLine()!) 7 | } 8 | // 9 | let predicate = NSPredicate(format: "SELF contains[c] %@", "@gmail.com") 10 | var filteredArray = rows.filter { predicate.evaluateWithObject($0) }.sort() 11 | 12 | for var element in filteredArray { 13 | print(element.componentsSeparatedByString(" ").first! as String) 14 | } 15 | -------------------------------------------------------------------------------- /30daysOFcode_Swift/day 29.swift: -------------------------------------------------------------------------------- 1 | let T = Int(readLine()!)! 2 | 3 | for i in 0 ..< T { 4 | var input = readLine()!.characters.split(" ").map(String.init) 5 | var N = Int(input[0])! 6 | var K = Int(input[1])! 7 | 8 | var max = 0; 9 | 10 | for j in 1 ..< N - 1 { 11 | for k in j + 1 ..< N { 12 | let h = j & k 13 | if (h < K && max < h) { 14 | max = h 15 | } 16 | } 17 | } 18 | 19 | print(max); 20 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 00.ts: -------------------------------------------------------------------------------- 1 | function processData(inputString: string) { 2 | // This line prints the first line of output 3 | console.log("Hello, World."); 4 | 5 | // This line prints the contents of 'inputString' 6 | console.log(inputString); 7 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 01.ts: -------------------------------------------------------------------------------- 1 | let i: number = 4; 2 | let d: number = 4.0; 3 | let s: string = "HackerRank "; 4 | 5 | 6 | // Declare second integer, double and string variables 7 | let myInt: number; 8 | let myFloat: number; 9 | let myString: string; 10 | 11 | myInt = parseInt(readLine()); 12 | myFloat = parseFloat(readLine()); 13 | myString = readLine(); 14 | 15 | // Prints the sum of both integers on a new line. 16 | console.log(i + myInt); 17 | 18 | // Prints the sum of both floats on a new line. 19 | console.log((d + myFloat).toFixed(1)); 20 | 21 | // Concatenate and print the String variables on a new line 22 | console.log(s + myString); 23 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 02.ts: -------------------------------------------------------------------------------- 1 | function solve(meal_cost: number, tip_percent: number, tax_percent: number): void { 2 | let TotalCost: number = meal_cost + (meal_cost * tip_percent/100) + (meal_cost * tax_percent/100); 3 | 4 | // console.log() to print to stdout 5 | console.log('The total meal cost is ' + TotalCost + ' dollars.'); 6 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 03.ts: -------------------------------------------------------------------------------- 1 | function main(): void { 2 | let N = parseInt(readLine()); 3 | if ((N % 2 !== 0) || (N >= 6 && N <= 20)) { 4 | console.log('Weird'); 5 | } 6 | else { 7 | console.log('Not Weird'); 8 | } 9 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 04.ts: -------------------------------------------------------------------------------- 1 | class Person { 2 | private age: number; 3 | 4 | constructor(initialAge: number) { 5 | if (initialAge >= 0) { 6 | this.age = initialAge; 7 | } 8 | else { 9 | this.age = 0; 10 | console.log('Age is not valid, setting age to 0.'); 11 | } 12 | } 13 | 14 | yearPasses(): void { 15 | this.age = this.age + 1; 16 | } 17 | 18 | amIOld(): void { 19 | if (this.age < 13) { 20 | console.log('You are young.'); 21 | } 22 | else if (this.age >= 13 && this.age <18) { 23 | console.log('You are a teenager'); 24 | } 25 | else { 26 | console.log('You are old'); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 05.ts: -------------------------------------------------------------------------------- 1 | function loops(n: number): void { 2 | if (n < 2 || n > 20) { 3 | return; 4 | } else { 5 | for (var i = 1; i <= 10; i++) { 6 | console.log(n, "x", i, "=", n * i); 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 06.ts: -------------------------------------------------------------------------------- 1 | function lets_review(input: String): void { 2 | let split_input = input.split("\n"); 3 | 4 | for (var i = 1; i < split_input.length; i++) { 5 | var splitWord = split_input[i].split(""); 6 | 7 | let even = ""; 8 | let odd = ""; 9 | 10 | for (var j = 0; j < splitWord.length; j++) { 11 | if (j % 2 === 0) { 12 | even = even + splitWord[j]; 13 | } else { 14 | odd = odd + splitWord[j]; 15 | } 16 | } 17 | console.log(even + " " + odd); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 07.ts: -------------------------------------------------------------------------------- 1 | function solution(inputArr: Array): string { 2 | 3 | let output: string = ''; 4 | for (let i:number = inputArr.length - 1; i >= 0; i--) { 5 | output = output + inputArr[i] + ' '; 6 | } 7 | return output; 8 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 08.ts: -------------------------------------------------------------------------------- 1 | function processData(input: string) { 2 | let inputArr: Array = input.split('\n'); 3 | 4 | let phoneBook = {}; 5 | 6 | for (let i: number = 1; i <= parseInt(inputArr[0]); i++) { 7 | let contact: Array = inputArr[i].split(' '); 8 | phoneBook[contact[0]] = parseInt(contact[1]); 9 | } 10 | 11 | for (let i: number = parseInt(inputArr[0]) + 1; i < inputArr.length; i++) { 12 | if (phoneBook[inputArr[i]]) { 13 | console.log(inputArr[i] + '=' + phoneBook[inputArr[i]]); 14 | } 15 | else { 16 | console.log('Not found'); 17 | } 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 09.ts: -------------------------------------------------------------------------------- 1 | function factorial(n: number): number { 2 | if (n == 0 || n == 1) { 3 | return 1; 4 | } 5 | return factorial(n - 1) * n; 6 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 10.ts: -------------------------------------------------------------------------------- 1 | function main(): void { 2 | let decimal: number = parseInt(readLine()); 3 | let binary: Array = []; 4 | 5 | while (decimal > 0) { 6 | let rem: number = decimal % 2; 7 | binary.push(rem); 8 | decimal = Math.floor(decimal/ 2); 9 | } 10 | 11 | binary = binary.reverse(); 12 | 13 | let ones = 0; 14 | let max = 0; 15 | 16 | for (let i: number = 0; i < binary.length; i++) { 17 | if (binary[i] == 1) { 18 | ones += 1; 19 | } 20 | else { 21 | if (ones > max) { 22 | max = ones; 23 | } 24 | ones = 0; 25 | } 26 | } 27 | 28 | if (ones > max) { 29 | max = ones; 30 | } 31 | 32 | console.log(max); 33 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 11.ts: -------------------------------------------------------------------------------- 1 | function main(): void { 2 | var arr: Array = []; 3 | for (let arr_i: number = 0; arr_i < 6; arr_i++) { 4 | arr[arr_i] = readLine().split(' '); 5 | arr[arr_i] = arr[arr_i].map(Number); 6 | } 7 | 8 | var sumArray: Array = []; 9 | for (let i: number = 0; i < arr.length; i++) { 10 | 11 | for (let j: number = 0; j < arr[i].length; j++) { 12 | let singleSum: number = 0; 13 | if (arr[i] && arr[i + 1] && arr[i + 2]) { 14 | if (arr[i][j + 1] != undefined && arr[i][j + 2] != undefined) { 15 | sumArray.push(arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]) 16 | } 17 | } 18 | } 19 | } 20 | console.log(Math.max.apply(null, sumArray)); 21 | } 22 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 13.ts: -------------------------------------------------------------------------------- 1 | class Book { 2 | title: string; 3 | author: string; 4 | 5 | constructor(title: string, author: string) { 6 | if (this.constructor === Book) { 7 | throw new TypeError('Do not attempt to directly instantiate an abstract class.'); 8 | } 9 | else { 10 | this.title = title; 11 | this.author = author; 12 | } 13 | } 14 | 15 | display(): void { 16 | console.log('Implement the \'display\' method!'); 17 | } 18 | } 19 | 20 | class MyBook extends Book { 21 | price: number; 22 | 23 | constructor(title: string, author: string, price: number) { 24 | super(title, author); 25 | this.price = price; 26 | } 27 | 28 | display(): void { 29 | console.log('Title: ' + this.title); 30 | console.log('Author: ' + this.author); 31 | console.log('Price: ' + this.price); 32 | } 33 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 16.ts: -------------------------------------------------------------------------------- 1 | import readline from "readline"; 2 | 3 | const rl = readline.createInterface({ 4 | input: process.stdin, 5 | output: process.stdout 6 | }); 7 | 8 | rl.question("Enter Number", answer => { 9 | try { 10 | console.log(parseFloat(answer)); 11 | } catch (error) { 12 | console.log("Bad String"); 13 | } 14 | rl.close(); 15 | }); 16 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 17.ts: -------------------------------------------------------------------------------- 1 | class Calculator { 2 | power(n: number, p: number): number { 3 | if (n < 0 || p < 0) { 4 | throw new Error('n and p should be non-negative'); 5 | } 6 | 7 | return Math.pow(n, p); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 18.ts: -------------------------------------------------------------------------------- 1 | class Solution { 2 | stack: Array; 3 | queue: Array; 4 | 5 | constructor() { 6 | this.stack = []; 7 | this.queue = []; 8 | } 9 | 10 | pushCharacter(c: string): void { 11 | this.stack.push(c); 12 | } 13 | 14 | popCharacter(): string { 15 | return this.stack.pop(); 16 | } 17 | 18 | enqueueCharacter(c: string): void { 19 | this.queue.push(c); 20 | } 21 | 22 | dequeueCharacter(): string { 23 | return this.queue.shift(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 19.ts: -------------------------------------------------------------------------------- 1 | interface AdvancedArithmetic { 2 | divisorSum(n: number): number; 3 | } 4 | 5 | class Calculator implements AdvancedArithmetic { 6 | 7 | divisorSum(n: number): number { 8 | let sum: number = 0; 9 | 10 | for (let i: number = 1; i <= n; i++) { 11 | if (n % i == 0) { 12 | sum += i; 13 | } 14 | } 15 | return sum; 16 | } 17 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 20.ts: -------------------------------------------------------------------------------- 1 | function main(): void { 2 | let n: number = parseInt(readLine()); 3 | let a: Array = readLine().split(' '); 4 | a = a.map(Number); 5 | 6 | let noOfSwaps: number = 0; 7 | 8 | for (let i: number = 0; i < n - 1; i++) { 9 | for (let j: number = i + 1; j < n; j++) { 10 | if (a[i] > a[j]) { 11 | let temp: number = a[i]; 12 | a[i] = a[j]; 13 | a[j] = temp; 14 | noOfSwaps++; 15 | } 16 | } 17 | } 18 | 19 | console.log('Array is sorted in ' + noOfSwaps + ' swaps.'); 20 | console.log('First Element: ' + a[0]); 21 | console.log('Last Element: ' + a[n - 1]); 22 | } 23 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 21.ts: -------------------------------------------------------------------------------- 1 | interface E { 2 | key: any; 3 | } 4 | 5 | interface T {} 6 | 7 | class Printer { 8 | printArray(a: Array): void { 9 | for (let element of a) { 10 | console.log(element); 11 | } 12 | } 13 | } 14 | 15 | let arr1 = new Printer(); 16 | arr1.printArray([{ key: 1 }, { key: "a" }]); 17 | 18 | -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 24.ts: -------------------------------------------------------------------------------- 1 | class Nodes { 2 | data: number; 3 | next: Nodes; 4 | 5 | constructor(data: number, next: Nodes) { 6 | this.data = data; 7 | this.next = next; 8 | } 9 | } 10 | 11 | this.removeDuplicates = function(head: Nodes): Nodes { 12 | let curr: Nodes = head; 13 | 14 | while (curr) { 15 | let nextNodes: Nodes = curr.next; 16 | 17 | if (nextNodes && curr.data == nextNodes.data) { 18 | curr.next = nextNodes.next; 19 | } 20 | else { 21 | curr = curr.next; 22 | } 23 | } 24 | 25 | return head; 26 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 25.ts: -------------------------------------------------------------------------------- 1 | function checkPrime(n: number): boolean { 2 | for (let i: number = 2; i <= n / 2; i++) { 3 | if (n % i == 0) { 4 | return false; 5 | } 6 | } 7 | return true; 8 | } 9 | 10 | function processData(input: string): void { 11 | let n: number = parseInt(input[0]); 12 | let arr: Array = input.split('\n'); 13 | 14 | for (let i: number = 1; i <= n; i++) { 15 | if (checkPrime(parseInt(arr[i]))) { 16 | console.log('Prime'); 17 | } 18 | else { 19 | console.log('Not prime'); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 26.ts: -------------------------------------------------------------------------------- 1 | function processData(input): void { 2 | const inputArr: string[] = input.split("\n"); 3 | const [returnedDay, returnedMonth, returnedYear]: number[] = inputArr[0].split(" ").map(val => parseInt(val)) 4 | const [expectedDay, expectedMonth, expectedYear]: number[] = inputArr[1].split(" ").map(val => parseInt(val)) 5 | const fine: number = returnedYear > expectedYear ? 10000 : 6 | returnedYear === expectedYear ? 7 | returnedMonth > expectedMonth ? 500 * (returnedMonth - expectedMonth) : 8 | returnedMonth === expectedMonth ? 9 | returnedDay > expectedDay ? 15 * (returnedDay - expectedDay) : 0 10 | : 0 11 | : 0 12 | console.log(fine); 13 | } 14 | 15 | process.stdin.resume(); 16 | process.stdin.setEncoding("ascii"); 17 | _input = ""; 18 | process.stdin.on("data", function (input) { 19 | _input += input; 20 | }); 21 | 22 | process.stdin.on("end", function () { 23 | processData(_input); 24 | }); -------------------------------------------------------------------------------- /30daysOFcode_TypeScript/day 29.ts: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.stdin.resume(); 4 | process.stdin.setEncoding('utf-8'); 5 | 6 | let inputString: string[] | string = ''; 7 | let currentLine: number = 0; 8 | 9 | process.stdin.on('data', inputStdin => { 10 | inputString += inputStdin; 11 | }); 12 | 13 | process.stdin.on('end', _ => { 14 | inputString = inputString.replace(/\s*$/, '') 15 | .split('\n') 16 | .map(str => str.replace(/\s*$/, '')); 17 | 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | 26 | 27 | function main() { 28 | const t: number = parseInt(readLine(), 10); 29 | 30 | for (let tItr = 0; tItr < t; tItr++) { 31 | const nk: string[] = readLine().split(' '); 32 | 33 | const n: number = parseInt(nk[0], 10); 34 | 35 | const k: number = parseInt(nk[1], 10); 36 | 37 | const a: number = k - 1; 38 | const b: number = (~a) & -(~a) 39 | 40 | console.log((a | b) > n ? a - 1 : a) 41 | } 42 | } --------------------------------------------------------------------------------