├── 0 ├── helloworld.clj ├── README.md ├── helloworld.js ├── helloworld.cpp └── Helloworld.java ├── 1 ├── datatypes.py ├── datatypes.cpp ├── DataTypes.java ├── datatypes.js └── README.md ├── 2 ├── run.sh ├── arithmetic.py ├── arithmetic.clj ├── arithmetic.cpp ├── Arithmetic.java ├── arithmetic.js └── README.md ├── 3 ├── run.sh ├── conditionals.py ├── conditionals.clj ├── Conditionals.java ├── conditionals.cpp ├── conditionals.js └── README.md ├── 4 ├── classvinstance.py ├── classvinstance.js ├── go │ └── classvinstance.go ├── classvinstance.cpp ├── classvinstance.java └── README.md ├── 5 ├── loops.py ├── loops.clj ├── go │ └── loops.go ├── README.md ├── loops.java ├── loops.cpp └── loops.js ├── 6 ├── stringreview.clj ├── stringreview.py ├── README.md ├── go │ └── stringreview.go ├── stringreview.cpp ├── stringreview.java └── stringreview.js ├── 7 ├── arrays.py ├── arrays.clj ├── go │ └── arrays.go ├── Solution.java ├── README.md ├── arrays.java ├── arrays.js └── arrays.cpp ├── 8 ├── case0 ├── dictionariesandmaps.clj ├── dictionariesandmaps.py ├── Solution.java ├── dictionariesandmaps.java ├── go │ └── dictionariesandmaps.go ├── dictionariesandmaps.cpp ├── dictionariesandmaps.js └── README.md ├── 9 ├── case0 ├── factorial.py └── README.md ├── 10 ├── case0 ├── case1 ├── output0 ├── output1 ├── bctest.py ├── binarychallenge.py └── README.md ├── 11 ├── case0 ├── hourglass.py └── README.md ├── 12 ├── case0 ├── output0 ├── scores.py └── README.md ├── 13 ├── case0 ├── output0 ├── abstract.py └── README.md ├── 14 ├── case0 ├── scope.py └── README.md ├── 15 ├── case0 ├── linkedlist.py └── README.md ├── 16 ├── parse.py └── README.md ├── 17 ├── case0 ├── exceptthrow.py └── README.md ├── 18 ├── case1 ├── case0 ├── palindrome.py └── README.md ├── 19 └── README.md ├── 20 ├── case0 ├── case1 ├── bubble_sort.py └── README.md ├── 21 └── README.md ├── 22 ├── case0 ├── README.md └── bst.py ├── 23 ├── case0 ├── bfs.py └── README.md ├── 24 ├── case0 ├── linkedlist.py └── README.md ├── 25 ├── case3 ├── case2 ├── output2 ├── output3 ├── case0 ├── output1 ├── case1 ├── prime.py └── README.md ├── 26 ├── output1 ├── case0 ├── case1 ├── fines.py └── README.md ├── 27 ├── input └── README.md ├── 28 ├── case0 ├── emails.py └── README.md ├── 29 ├── case0 ├── bitwise.py ├── README.md ├── out ├── output1 └── case1 ├── README.md └── LICENSE /26/output1: -------------------------------------------------------------------------------- 1 | 0 -------------------------------------------------------------------------------- /10/case0: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /10/case1: -------------------------------------------------------------------------------- 1 | 13 2 | -------------------------------------------------------------------------------- /10/output0: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /10/output1: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /18/case1: -------------------------------------------------------------------------------- 1 | racer 2 | -------------------------------------------------------------------------------- /25/case3: -------------------------------------------------------------------------------- 1 | 1 2 | 1 -------------------------------------------------------------------------------- /18/case0: -------------------------------------------------------------------------------- 1 | racecar 2 | -------------------------------------------------------------------------------- /25/case2: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | -------------------------------------------------------------------------------- /25/output2: -------------------------------------------------------------------------------- 1 | Prime 2 | -------------------------------------------------------------------------------- /9/case0: -------------------------------------------------------------------------------- 1 | 3 2 | 6 3 | -------------------------------------------------------------------------------- /14/case0: -------------------------------------------------------------------------------- 1 | 3 2 | 1 2 5 3 | -------------------------------------------------------------------------------- /20/case0: -------------------------------------------------------------------------------- 1 | 3 2 | 1 2 3 3 | -------------------------------------------------------------------------------- /20/case1: -------------------------------------------------------------------------------- 1 | 3 2 | 3 2 1 3 | -------------------------------------------------------------------------------- /25/output3: -------------------------------------------------------------------------------- 1 | Not prime 2 | -------------------------------------------------------------------------------- /25/case0: -------------------------------------------------------------------------------- 1 | 3 2 | 12 3 | 5 4 | 7 5 | -------------------------------------------------------------------------------- /15/case0: -------------------------------------------------------------------------------- 1 | 4 2 | 2 3 | 3 4 | 4 5 | 1 6 | -------------------------------------------------------------------------------- /26/case0: -------------------------------------------------------------------------------- 1 | 9 6 2015 2 | 6 6 2015 3 | -------------------------------------------------------------------------------- /26/case1: -------------------------------------------------------------------------------- 1 | 31 12 2009 2 | 1 1 2010 3 | -------------------------------------------------------------------------------- /29/case0: -------------------------------------------------------------------------------- 1 | 3 2 | 5 2 3 | 8 5 4 | 2 2 5 | -------------------------------------------------------------------------------- /12/case0: -------------------------------------------------------------------------------- 1 | Heraldo Memelli 8135627 2 | 2 3 | 100 80 4 | -------------------------------------------------------------------------------- /13/case0: -------------------------------------------------------------------------------- 1 | The Alchemist 2 | Paulo Coelho 3 | 248 4 | -------------------------------------------------------------------------------- /17/case0: -------------------------------------------------------------------------------- 1 | 4 2 | 3 5 3 | 2 4 4 | -1 -2 5 | -1 3 6 | -------------------------------------------------------------------------------- /23/case0: -------------------------------------------------------------------------------- 1 | 6 2 | 3 3 | 5 4 | 4 5 | 7 6 | 2 7 | 1 8 | -------------------------------------------------------------------------------- /24/case0: -------------------------------------------------------------------------------- 1 | 6 2 | 1 3 | 2 4 | 2 5 | 3 6 | 3 7 | 4 8 | -------------------------------------------------------------------------------- /22/case0: -------------------------------------------------------------------------------- 1 | 7 2 | 3 3 | 5 4 | 2 5 | 1 6 | 4 7 | 6 8 | 7 9 | -------------------------------------------------------------------------------- /12/output0: -------------------------------------------------------------------------------- 1 | Name: Memelli, Heraldo 2 | ID: 8135627 3 | Grade: O 4 | -------------------------------------------------------------------------------- /13/output0: -------------------------------------------------------------------------------- 1 | Title: The Alchemist 2 | Author: Paulo Coelho 3 | Price: 248 4 | -------------------------------------------------------------------------------- /8/case0: -------------------------------------------------------------------------------- 1 | 3 2 | sam 99912222 3 | tom 11122222 4 | harry 12299933 5 | sam 6 | edward 7 | harry 8 | -------------------------------------------------------------------------------- /21/README.md: -------------------------------------------------------------------------------- 1 | # Day 21: Generics 2 | 3 | https://www.hackerrank.com/challenges/30-generics 4 | 5 | -------------------------------------------------------------------------------- /11/case0: -------------------------------------------------------------------------------- 1 | 1 1 1 0 0 0 2 | 0 1 0 0 0 0 3 | 1 1 1 0 0 0 4 | 0 0 2 4 4 0 5 | 0 0 0 2 0 0 6 | 0 0 1 2 4 0 7 | -------------------------------------------------------------------------------- /2/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo -e "12.00\n20\n8\n" | node arithmetic.js 4 | 5 | echo -e "12.00\n20\n8\n" | python arithmetic.py 6 | 7 | -------------------------------------------------------------------------------- /3/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo -e "12.00\n20\n8\n" | node conditionals.js 4 | 5 | echo -e "12.00\n20\n8\n" | python conditionals.py 6 | 7 | -------------------------------------------------------------------------------- /5/loops.py: -------------------------------------------------------------------------------- 1 | #!python 2 | 3 | N = int(input().strip()) 4 | 5 | for i in range(1, 11): 6 | print("{} x {} = {}".format(N, i, N * i)) 7 | -------------------------------------------------------------------------------- /25/output1: -------------------------------------------------------------------------------- 1 | Not prime 2 | Not prime 3 | Not prime 4 | Not prime 5 | Not prime 6 | Not prime 7 | Not prime 8 | Prime 9 | Not prime 10 | Prime 11 | -------------------------------------------------------------------------------- /0/helloworld.clj: -------------------------------------------------------------------------------- 1 | ; Enter your code here. Read input from STDIN. Print output to STDOUT 2 | ; 3 | 4 | (println "Hello, World.") 5 | (println (read-line)) 6 | -------------------------------------------------------------------------------- /25/case1: -------------------------------------------------------------------------------- 1 | 10 2 | 1000000000 3 | 1000000001 4 | 1000000002 5 | 1000000003 6 | 1000000004 7 | 1000000005 8 | 1000000006 9 | 1000000007 10 | 1000000008 11 | 1000000009 12 | -------------------------------------------------------------------------------- /28/case0: -------------------------------------------------------------------------------- 1 | 6 2 | riya riya@gmail.com 3 | julia julia@julia.me 4 | julia sjulia@gmail.com 5 | julia julia@gmail.com 6 | samantha samantha@gmail.com 7 | tanya tanya@gmail.com 8 | -------------------------------------------------------------------------------- /7/arrays.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | n = int(input().strip()) 4 | arr = [str(int(arr_temp)) for arr_temp in input().strip().split(' ')] 5 | arr.reverse() 6 | print(" ".join(arr)) 7 | -------------------------------------------------------------------------------- /5/loops.clj: -------------------------------------------------------------------------------- 1 | (use '[clojure.string :only (split triml)]) 2 | 3 | (let [N_t (read-line) 4 | N (Integer/parseInt N_t)] 5 | (doseq [i (range 1 11)] 6 | (println (str N " x " i " = " (* N i))))) 7 | 8 | -------------------------------------------------------------------------------- /0/README.md: -------------------------------------------------------------------------------- 1 | Task 2 | 3 | To complete this challenge, you must save a line of input from stdin to a variable, print Hello, World.Hello, World. on a single line, and finally print the value of your variable on a second line. -------------------------------------------------------------------------------- /16/parse.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # https://www.hackerrank.com/challenges/30-exceptions-string-to-integer 4 | 5 | S = input().strip() 6 | 7 | try: 8 | n = int(S) 9 | print(n) 10 | except: 11 | print("Bad String") 12 | -------------------------------------------------------------------------------- /27/input: -------------------------------------------------------------------------------- 1 | print("5") 2 | print("5 3") 3 | print("1 1 1 -1 0") 4 | print("6 3") 5 | print("-1 0 0 1 1 1") 6 | print("7 3") 7 | print("-1 0 1 1 1 1 1") 8 | print("4 3") 9 | print("-1 -1 0 1") 10 | print("3 3") 11 | print("-1 0 1 ") 12 | -------------------------------------------------------------------------------- /7/arrays.clj: -------------------------------------------------------------------------------- 1 | (require 'clojure.string) 2 | 3 | (let [ignore (read-line) 4 | data (read-line) 5 | arr (clojure.string/split data #"\s+") 6 | rarr (reverse arr) 7 | line (clojure.string/join " " rarr)] 8 | (println line)) 9 | 10 | -------------------------------------------------------------------------------- /8/dictionariesandmaps.clj: -------------------------------------------------------------------------------- 1 | (require 'clojure.string) 2 | 3 | (let [ignore (read-line) 4 | data (read-line) 5 | arr (clojure.string/split data #"\s+") 6 | rarr (reverse arr) 7 | line (clojure.string/join " " rarr)] 8 | (println line)) 9 | 10 | -------------------------------------------------------------------------------- /5/go/loops.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 | var n int 8 | fmt.Scanf("%d", &n) 9 | 10 | for i := 1; i <= 10; i++ { 11 | fmt.Printf("%d x %d = %d\n", n, i, n*i) 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /9/factorial.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | n = int(input().strip()) 4 | 5 | 6 | def _factorial(N): 7 | if N <= 1: 8 | return 1 9 | return N * _factorial(N - 1) 10 | 11 | 12 | def factorial(N): 13 | print(_factorial(N)) 14 | 15 | if __name__ == "__main__": 16 | factorial(n) 17 | -------------------------------------------------------------------------------- /3/conditionals.py: -------------------------------------------------------------------------------- 1 | #!python 2 | 3 | import sys 4 | 5 | 6 | N = int(input().strip()) 7 | 8 | if N % 2 == 0: 9 | if 2 <= N <= 5: 10 | print("Not Weird") 11 | elif 6 <= N <= 20: 12 | print("Weird") 13 | else: 14 | print("Not Weird") 15 | else: 16 | print("Weird") 17 | -------------------------------------------------------------------------------- /3/conditionals.clj: -------------------------------------------------------------------------------- 1 | (use '[clojure.string :only (split triml)]) 2 | 3 | (let [N_t (read-line) 4 | N (Integer/parseInt N_t) 5 | is_odd (= 1 (mod N 2)) 6 | ] 7 | (println (cond is_odd "Weird" 8 | (<= N 5) "Not Weird" 9 | (<= N 20) "Weird" 10 | :else "Not Weird"))) 11 | -------------------------------------------------------------------------------- /5/README.md: -------------------------------------------------------------------------------- 1 | #Day 5: Loops 2 | 3 | https://www.hackerrank.com/challenges/30-loops 4 | 5 | 6 | ##Task 7 | 8 | Given an integer, n, print its first 10 multiples 9 | n * i where 1 <= i <= 10 10 | 11 | 12 | Constraints 13 | 14 | 2 <= n <= 20 15 | 16 | ## Output Format 17 | 18 | print 10 lines of output where line i contains the result of nxi in the form N x i = result. 19 | 20 | -------------------------------------------------------------------------------- /28/emails.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # Day 28: RegEx, Patterns 4 | 5 | # https://www.hackerrank.com/challenges/30-regex-patterns 6 | 7 | import re 8 | 9 | names = [] 10 | 11 | T = int(input()) 12 | for i in range(T): 13 | name, id = input().split(" ") 14 | if re.search(r"@gmail\.com$", id): 15 | names.append(name) 16 | 17 | names.sort() 18 | for name in names: 19 | print(name) 20 | -------------------------------------------------------------------------------- /29/bitwise.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # day 29: bitwise 4 | 5 | t = int(input().strip()) 6 | for a0 in range(t): 7 | n, k = input().strip().split(' ') 8 | n, k = [int(n), int(k)] 9 | 10 | max = 0 11 | for a in range(1, n): 12 | for b in range(a + 1, n + 1): 13 | bw = a & b 14 | if bw > max and bw < k: 15 | max = bw 16 | print(max) 17 | -------------------------------------------------------------------------------- /6/stringreview.clj: -------------------------------------------------------------------------------- 1 | (require 'clojure.string) 2 | 3 | (let [N_t (read-line) 4 | N (Integer/parseInt N_t)] 5 | (doseq [i (range N)] 6 | (let [line (read-line) 7 | first (take-nth 2 line) 8 | second (take-nth 2 (rest line))] 9 | (println (clojure.string/join 10 | [(clojure.string/join first) 11 | \space 12 | (clojure.string/join second)]))))) 13 | -------------------------------------------------------------------------------- /7/go/arrays.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 | var n int 8 | fmt.Scanf("%d", &n) 9 | // var line string 10 | var slice = make([]int, n) 11 | 12 | for l := 0; l < n; l++ { 13 | fmt.Scanf("%d", &slice[l]) 14 | } 15 | 16 | for l := n - 1; l >= 0; l-- { 17 | fmt.Printf("%d ", slice[l]) 18 | } 19 | fmt.Printf("\n") 20 | } 21 | -------------------------------------------------------------------------------- /6/stringreview.py: -------------------------------------------------------------------------------- 1 | #!python 2 | 3 | n = int(input().strip()) 4 | 5 | 6 | def walk(line): 7 | first = [] 8 | second = [] 9 | for i, c in enumerate(line): 10 | if i % 2 == 0: 11 | first.append(c) 12 | else: 13 | second.append(c) 14 | out = "".join(first) + " " + "".join(second) 15 | return out 16 | 17 | 18 | for i in range(n): 19 | line = input() 20 | print(walk(line)) 21 | -------------------------------------------------------------------------------- /8/dictionariesandmaps.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | n = int(input().strip()) 4 | phonebook = {} 5 | for i in range(n): 6 | line = input() 7 | k, v = line.split() 8 | phonebook[k] = v 9 | 10 | while True: 11 | try: 12 | number = input() 13 | except EOFError: 14 | break 15 | if number in phonebook: 16 | print("{}={}".format(number, phonebook[number])) 17 | else: 18 | print("Not found") 19 | -------------------------------------------------------------------------------- /9/README.md: -------------------------------------------------------------------------------- 1 | # Day 9: Recursion 2 | 3 | https://www.hackerrank.com/challenges/30-recursion 4 | 5 | Objective 6 | 7 | Today, we're learning and practicing an algorithmic concept called Recursion. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | Recursive Method for Calculating Factorial 10 | 11 | Write a factorial function that takes a positive integer, N as a parameter and prints the result of N! (N factorial). -------------------------------------------------------------------------------- /6/README.md: -------------------------------------------------------------------------------- 1 | #Day 6: Let's Review 2 | 3 | https://www.hackerrank.com/challenges/30-review-loop 4 | 5 | 6 | ##Task 7 | 8 | Given a string, S, of length N that is indexed from 0 to N - 1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail). 9 | 10 | 11 | ## Output Format 12 | 13 | print 10 lines of output where line i contains the result of nxi in the form N x i = result. 14 | 15 | -------------------------------------------------------------------------------- /2/arithmetic.py: -------------------------------------------------------------------------------- 1 | #!python 2 | 3 | mealCost = float(input()) # original meal price 4 | tipPercent = int(input()) # tip percentage 5 | taxPercent = int(input()) # tax percentage 6 | 7 | # Write your calculation code here. 8 | cost = mealCost * (1 + (tipPercent + taxPercent) / 100.0) 9 | 10 | # cast the result of the rounding operation to an int and save it as totalCost 11 | totalCost = round(cost) 12 | 13 | # Print your result 14 | print("The total meal cost is " + str(totalCost) + " dollars.") 15 | -------------------------------------------------------------------------------- /0/helloworld.js: -------------------------------------------------------------------------------- 1 | 2 | function processData(inputString) { 3 | // Your first line of output goes here 4 | process.stdout.write("Hello, World.\n"); 5 | 6 | // Write the second line of output 7 | process.stdout.write(inputString); 8 | } 9 | 10 | 11 | process.stdin.resume(); 12 | process.stdin.setEncoding("ascii"); 13 | _input = ""; 14 | process.stdin.on("data", function (input) { 15 | _input += input; 16 | }); 17 | 18 | process.stdin.on("end", function () { 19 | processData(_input); 20 | }); 21 | -------------------------------------------------------------------------------- /0/helloworld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | string inputString; // declare a variable to hold our input 10 | getline(cin, inputString); // get a line of input from cin and save it to our variable 11 | 12 | // Your first line of output goes here 13 | cout << "Hello, World." << endl; 14 | 15 | // Write the second line of output 16 | cout << inputString << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /5/loops.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 scan = new Scanner(System.in); 11 | int n = scan.nextInt(); 12 | scan.close(); 13 | String ans=""; 14 | 15 | for (int i = 1; i <= 10; i++) { 16 | ans = String.format("%d x %d = %d", n, i, n*i); 17 | System.out.println(ans); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /20/bubble_sort.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # Day 20: Sorting 4 | 5 | n = int(input().strip()) 6 | a = [int(a_temp) for a_temp in input().strip().split(' ')] 7 | 8 | numSwaps = 0 9 | for i in range(len(a)): 10 | for j in range(len(a) - 1): 11 | if a[j] > a[j + 1]: 12 | numSwaps += 1 13 | a[j + 1], a[j] = a[j], a[j + 1] 14 | if numSwaps == 0: 15 | break 16 | 17 | print("Array is sorted in " + str(numSwaps) + " swaps.") 18 | print("First Element: " + str(a[0])) 19 | print("Last Element: " + str(a[-1])) 20 | -------------------------------------------------------------------------------- /6/go/stringreview.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 | var n int 8 | fmt.Scanf("%d", &n) 9 | 10 | for l := 0; l < n; l++ { 11 | var s string 12 | fmt.Scanf("%s", &s) 13 | 14 | var even, odd string 15 | var counter int 16 | for _, r := range s { 17 | if counter%2 == 0 { 18 | even += string(r) 19 | } else { 20 | odd += string(r) 21 | } 22 | counter++ 23 | } 24 | fmt.Printf("%s %s\n", even, odd) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /7/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | 5 | public class Solution { 6 | 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int n = in.nextInt(); 10 | int[] arr = new int[n]; 11 | for(int i=0; i < n; i++){ 12 | arr[i] = in.nextInt(); 13 | } 14 | in.close(); 15 | for(int i=n-1; i <= 0; i--){ 16 | System.out.printf("%s ", arr[i]); 17 | } 18 | System.out.println(""); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /8/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | 5 | public class Solution { 6 | 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int n = in.nextInt(); 10 | int[] arr = new int[n]; 11 | for(int i=0; i < n; i++){ 12 | arr[i] = in.nextInt(); 13 | } 14 | in.close(); 15 | for(int i=n-1; i <= 0; i--){ 16 | System.out.printf("%s ", arr[i]); 17 | } 18 | System.out.println(""); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /7/README.md: -------------------------------------------------------------------------------- 1 | #Day 7: Let's Review 2 | 3 | https://www.hackerrank.com/challenges/30-arrays 4 | 5 | 6 | ##Task 7 | 8 | Given an array of integers, print's elements in reverse order as a single line of space-separated numbers. 9 | 10 | 11 | Input Format 12 | 13 | The first line contains an integer, n (the size of our array). 14 | The second line contains n space-separated integers describing the array's elements. 15 | 16 | ## Output Format 17 | 18 | print 10 lines of output where line i contains the result of nxi in the form N x i = result. 19 | 20 | -------------------------------------------------------------------------------- /7/arrays.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | 5 | public class Solution { 6 | 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int n = in.nextInt(); 10 | int[] arr = new int[n]; 11 | for(int i=0; i < n; i++){ 12 | arr[i] = in.nextInt(); 13 | } 14 | in.close(); 15 | for(int i=n-1; i >= 0; i--){ 16 | System.out.printf("%s ", arr[i]); 17 | } 18 | // System.out.println("Done."); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /2/arithmetic.clj: -------------------------------------------------------------------------------- 1 | (def mealCost (Float/parseFloat (read-line))) ;; base meal cost 2 | (def tipPercent (Integer/parseInt (read-line))) ;; tip percentage 3 | (def taxPercent (Integer/parseInt (read-line))) ;; tax percentage 4 | 5 | ;; Write your calculation code here. 6 | (def cost (* mealCost (+ 1 (/ (+ tipPercent taxPercent) 100.0)))) 7 | 8 | ;; cast the result of the rounding operation to an int and save it as totalCost 9 | (def totalCost (Math/round cost)) 10 | 11 | ;; Print your result 12 | (println (str "The total meal cost is " totalCost " dollars.")) 13 | -------------------------------------------------------------------------------- /1/datatypes.py: -------------------------------------------------------------------------------- 1 | #!python 2 | 3 | i = 4 4 | d = 4.0 5 | s = 'HackerRank ' 6 | 7 | # Declare second integer, double, and String variables. 8 | 9 | # Read and save an integer, double, and String to your variables. 10 | i2 = int(input()) 11 | d2 = float(input()) 12 | s2 = input() 13 | # Print the sum of both integer variables on a new line. 14 | print(i + i2) 15 | # Print the sum of the double variables on a new line. 16 | print(d + d2) 17 | # Concatenate and print the String variables on a new line 18 | print(s + s2) 19 | # The 's' variable above should be printed first. 20 | -------------------------------------------------------------------------------- /8/dictionariesandmaps.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 | int n = in.nextInt(); 9 | for(int i = 0; i < n; i++){ 10 | String name = in.next(); 11 | int phone = in.nextInt(); 12 | // Write code here 13 | } 14 | while(in.hasNext()){ 15 | String s = in.next(); 16 | // Write code here 17 | } 18 | in.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /17/exceptthrow.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # day 17: more exceptions 4 | 5 | 6 | class InputError(Exception): 7 | pass 8 | 9 | 10 | class Calculator: 11 | 12 | def __init__(self): 13 | pass 14 | 15 | def power(self, n, p): 16 | if n < 0 or p < 0: 17 | raise InputError("n and p should be non-negative") 18 | else: 19 | return n**p 20 | 21 | myCalculator = Calculator() 22 | T = int(input()) 23 | for i in range(T): 24 | n, p = map(int, input().split()) 25 | try: 26 | ans = myCalculator.power(n, p) 27 | print(ans) 28 | except Exception as e: 29 | print(e) 30 | -------------------------------------------------------------------------------- /10/bctest.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | 4 | def to_binary(n): 5 | binary = [] 6 | if n == 0: 7 | return [0] 8 | while n > 0: 9 | (n, bit) = divmod(n, 2) 10 | binary.append(bit) 11 | binary.reverse() 12 | return binary 13 | 14 | 15 | def consecutive1s(binary): 16 | max_len = 0 17 | 18 | count = 0 19 | for bit in binary: 20 | if bit: 21 | count += 1 22 | else: 23 | count = 0 24 | if count > max_len: 25 | max_len = count 26 | 27 | return max_len 28 | 29 | for i in range(0, 64): 30 | print(i, to_binary(i), consecutive1s(to_binary(i))) 31 | -------------------------------------------------------------------------------- /10/binarychallenge.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | n = int(input().strip()) 4 | 5 | 6 | def to_binary(n): 7 | binary = [] 8 | if n == 0: 9 | return [0] 10 | while n > 0: 11 | (n, bit) = divmod(n, 2) 12 | binary.append(bit) 13 | binary.reverse() 14 | return binary 15 | 16 | 17 | def consecutive1s(binary): 18 | max_len = 0 19 | 20 | count = 0 21 | for bit in binary: 22 | if bit: 23 | count += 1 24 | else: 25 | count = 0 26 | if count > max_len: 27 | max_len = count 28 | 29 | return max_len 30 | 31 | 32 | print(consecutive1s(to_binary(n))) 33 | -------------------------------------------------------------------------------- /2/arithmetic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | // Enter your code here. Read input from STDIN. Print output to STDOUT 11 | 12 | double mealCost; 13 | int tipPercent; 14 | int taxPercent; 15 | 16 | cin >> mealCost; 17 | cin >> tipPercent; 18 | cin >> taxPercent; 19 | 20 | double cost = mealCost * (1 + (tipPercent + taxPercent) / 100.0); 21 | 22 | double totalCost = round(cost); 23 | 24 | // Print your result 25 | cout << "The total meal cost is " << totalCost << " dollars."; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /25/prime.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 3 2 | 3 | # Day 25: Running Time and Complexity 4 | 5 | import math 6 | 7 | 8 | def is_prime(n): 9 | root = n**.5 10 | upper = math.floor(root) + 1 11 | 12 | if n == 1: 13 | return False 14 | elif n == 2: 15 | return True 16 | 17 | if n % 2 == 0: 18 | return False 19 | 20 | for divisor in range(3, upper, 2): 21 | if n % divisor == 0: 22 | return False 23 | else: 24 | return True 25 | 26 | T = int(input()) 27 | for i in range(T): 28 | data = int(input()) 29 | if is_prime(data): 30 | print("Prime") 31 | else: 32 | print("Not prime") 33 | -------------------------------------------------------------------------------- /14/scope.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # hr14 4 | 5 | 6 | class Difference: 7 | 8 | def __init__(self, a): 9 | self.__elements = a 10 | self.maximumDifference = 0 11 | 12 | def computeDifference(self): 13 | for i in self.__elements: 14 | for j in self.__elements: 15 | diff = abs(i - j) 16 | if diff > self.maximumDifference: 17 | self.maximumDifference = diff 18 | return 19 | 20 | # End of Difference class 21 | 22 | _ = input() 23 | a = [int(e) for e in input().split(' ')] 24 | 25 | d = Difference(a) 26 | d.computeDifference() 27 | 28 | print(d.maximumDifference) 29 | -------------------------------------------------------------------------------- /0/Helloworld.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 Helloworld { 8 | public static void main(String[] args) { 9 | Scanner scan = new Scanner(System.in); // use the Scanner class to read from stdin 10 | String inputString = scan.nextLine(); // read a line of input and save it to a variable 11 | scan.close(); // close the scanner 12 | 13 | // Your first line of output goes here 14 | System.out.println("Hello, World."); 15 | 16 | // Write the second line of output 17 | System.out.println(inputString); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /11/hourglass.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | a = [] 4 | 5 | for i in range(6): 6 | line = input() 7 | a.append([int(x) for x in line.split()]) 8 | 9 | 10 | def hourglass_sum(a, i, j): 11 | sum = a[i][j] + a[i][j + 1] + a[i][j + 2] 12 | i += 1 13 | sum += a[i][j + 1] 14 | i += 1 15 | sum += a[i][j] + a[i][j + 1] + a[i][j + 2] 16 | return sum 17 | 18 | 19 | def max_hourglasses(a): 20 | max = -9 * 36 21 | for i in range(0, 4): 22 | for j in range(0, 4): 23 | sum = hourglass_sum(a, i, j) 24 | # print(sum) 25 | if sum > max: 26 | max = sum 27 | return max 28 | 29 | print(max_hourglasses(a)) 30 | -------------------------------------------------------------------------------- /26/fines.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # Day 26: Nested Logic 4 | # https://www.hackerrank.com/challenges/30-nested-logic 5 | 6 | return_date = [int(x) for x in input().split(' ')] 7 | due_date = [int(x) for x in input().split(' ')] 8 | 9 | year = 2 10 | month = 1 11 | day = 0 12 | 13 | fine = 0 14 | if return_date[year] > due_date[year]: 15 | fine = 10000 16 | elif return_date[year] == due_date[year]: 17 | if return_date[month] > due_date[month]: 18 | fine = 500 * (return_date[month] - due_date[month]) 19 | elif return_date[month] == due_date[month]: 20 | if return_date[day] > due_date[day]: 21 | fine = 15 * (return_date[day] - due_date[day]) 22 | 23 | print(fine) 24 | -------------------------------------------------------------------------------- /10/README.md: -------------------------------------------------------------------------------- 1 | #Day 10: Binary Numbers 2 | 3 | Objective 4 | Today, we're working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video! 5 | 6 | Task 7 | Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1's in n's binary representation. 8 | 9 | Input Format 10 | 11 | A single integer, n. 12 | 13 | Constraints 14 | 15 | Output Format 16 | 17 | Print a single base-10 integer denoting the maximum number of consecutive 1's in the binary representation of n. 18 | 19 | Sample Input 1 20 | 21 | 5 22 | Sample Output 1 23 | 24 | 1 25 | Sample Input 2 26 | 27 | 13 28 | Sample Output 2 29 | 30 | 2 -------------------------------------------------------------------------------- /8/go/dictionariesandmaps.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 | var n int 8 | fmt.Scanf("%d", &n) 9 | // var line string 10 | var name string 11 | var num int 12 | var phonebook = make(map[string]int) 13 | for l := 0; l < n; l++ { 14 | fmt.Scanf("%s", &name) 15 | fmt.Scanf("%d", &num) 16 | phonebook[name] = num 17 | } 18 | 19 | for { 20 | var query string 21 | _, err := fmt.Scanf("%s", &query) 22 | if err != nil { 23 | break 24 | } 25 | 26 | num, ok := phonebook[query] 27 | if ok { 28 | fmt.Printf("%s=%d\n", query, num) 29 | } else { 30 | fmt.Printf("Not found\n") 31 | } 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /5/loops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | 27 | int main(){ 28 | int N; 29 | cin >> N; 30 | 31 | for (int i = 1; i <= 10; i++) { 32 | cout << n << " x " << i << " = " << n*i << endl 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /4/classvinstance.py: -------------------------------------------------------------------------------- 1 | #!python 2 | 3 | 4 | class Person: 5 | 6 | def __init__(self, initialAge): 7 | self.age = initialAge if initialAge >= 0 else 0 8 | if initialAge < 0: 9 | print("Age is not valid, setting age to 0.") 10 | 11 | def amIOld(self): 12 | if self.age < 13: 13 | print("You are young.") 14 | elif self.age < 18: 15 | print("You are a teenager.") 16 | else: 17 | print("You are old.") 18 | 19 | def yearPasses(self): 20 | self.age += 1 21 | return self.age 22 | 23 | t = int(input()) 24 | for i in range(0, t): 25 | age = int(input()) 26 | p = Person(age) 27 | p.amIOld() 28 | for j in range(0, 3): 29 | p.yearPasses() 30 | p.amIOld() 31 | print("") 32 | -------------------------------------------------------------------------------- /5/loops.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 | var statement = ""; 26 | for (var i = 1; i <= 10; i++) { 27 | statement = "" + N + " x " + i + " = " + N*i + "\n"; 28 | process.stdout.write(statement); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /6/stringreview.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | int N; 15 | cin >> N; 16 | cin.ignore(); 17 | 18 | string line; 19 | string first = ""; 20 | string second = ""; 21 | 22 | for (int i = 0; i < N; i++) { 23 | getline(cin, line); 24 | first = ""; 25 | second = ""; 26 | for (int c = 0; c < line.length(); c++) { 27 | if (c % 2 == 0) { 28 | first += line[c]; 29 | } else { 30 | second += line[c]; 31 | } 32 | } 33 | cout << first << " " << second << endl; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /7/arrays.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 | var statement = ""; 26 | var line = readLine(); 27 | line = line.split(" "); 28 | line = line.reverse(); 29 | line = line.join(" ") + "\n"; 30 | process.stdout.write(line); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /3/Conditionals.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | public class Solution { 7 | 8 | public static void main(String[] args) { 9 | Scanner scan = new Scanner(System.in); 10 | int n = scan.nextInt(); 11 | scan.close(); 12 | String ans=""; 13 | 14 | // if 'n' is NOT evenly divisible by 2 (i.e.: n is odd) 15 | if(n%2==1){ 16 | ans = "Weird"; 17 | } 18 | else{ 19 | // Complete the code 20 | if (n < 6) { 21 | ans = "Not Weird"; 22 | } else if (n <= 20) { 23 | ans = "Weird"; 24 | } else { 25 | ans = "Not Weird"; 26 | } 27 | } 28 | System.out.println(ans); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /13/abstract.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | from abc import ABCMeta, abstractmethod 4 | 5 | 6 | class Book(object, metaclass=ABCMeta): 7 | 8 | def __init__(self, title, author): 9 | self.title = title 10 | self.author = author 11 | 12 | @abstractmethod 13 | def display(): pass 14 | 15 | 16 | class MyBook(Book): 17 | 18 | def __init__(self, title, author, price): 19 | # super().__init__(title, author) 20 | self.title = title 21 | self.author = author 22 | self.price = price 23 | 24 | def display(self): 25 | print("Title: " + self.title) 26 | print("Author: " + self.author) 27 | print("Price: " + str(self.price)) 28 | 29 | 30 | title = input() 31 | author = input() 32 | price = int(input()) 33 | new_novel = MyBook(title, author, price) 34 | new_novel.display() 35 | -------------------------------------------------------------------------------- /7/arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | 27 | int main(){ 28 | int n; 29 | cin >> n; 30 | vector arr(n); 31 | for(int arr_i = 0;arr_i < n;arr_i++){ 32 | cin >> arr[arr_i]; 33 | } 34 | for(int arr_i = n-1;arr_i >= 0;arr_i--){ 35 | cout << arr[arr_i] << " "; 36 | } 37 | cout << endl; 38 | 39 | return 0; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /15/linkedlist.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # Day 15 4 | 5 | 6 | class Node: 7 | 8 | def __init__(self, data): 9 | self.data = data 10 | self.next = None 11 | 12 | 13 | class Solution: 14 | 15 | def display(self, head): 16 | current = head 17 | while current: 18 | print(current.data, end=' ') 19 | current = current.next 20 | 21 | def insert(self, head, data): 22 | # Complete this method 23 | node = Node(data) 24 | 25 | current = head 26 | if current: 27 | while current.next: 28 | current = current.next 29 | current.next = node 30 | else: 31 | head = node 32 | return head 33 | 34 | mylist = Solution() 35 | T = int(input()) 36 | head = None 37 | for i in range(T): 38 | data = int(input()) 39 | head = mylist.insert(head, data) 40 | mylist.display(head) 41 | -------------------------------------------------------------------------------- /1/datatypes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int i = 4; 9 | double d = 4.0; 10 | string s = "HackerRank "; 11 | 12 | // Declare second integer, double, and String variables. 13 | int i2; 14 | double d2; 15 | string s2, s3; 16 | // Read and save an integer, double, and String to your variables. 17 | cin >> i2; 18 | cin >> d2; 19 | getline(cin, s2); 20 | getline(cin, s2); 21 | 22 | // Print the sum of both integer variables on a new line. 23 | 24 | cout << i + i2 << endl; 25 | // Print the sum of the double variables on a new line. 26 | cout.precision(1); 27 | cout << fixed << d + d2 << endl; 28 | // Concatenate and print the String variables on a new line 29 | // The 's' variable above should be printed first. 30 | cout << s + s2 << endl; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /29/README.md: -------------------------------------------------------------------------------- 1 | # Day 29: bitwise and 2 | 3 | # https://www.hackerrank.com/challenges/30-bitwise-and 4 | 5 | Objective 6 | 7 | Welcome to the last day! Today, we're discussing bitwise operations. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | Task 10 | 11 | Given set S. Find two integers, A and B (where A < B), from set S such that the value of A&B is the maximum possible and also less than a given integer, K. In this case, & represents the bitwise AND operator. 12 | 13 | Input Format 14 | 15 | The first line contains an integer, T, the number of test cases. 16 | Each of the T subsequent lines defines a test case as 2 space-separated integers, N and K, respectively. 17 | 18 | Constraints 19 | 20 | Output Format 21 | 22 | For each test case, print the maximum possible value of on a new line. 23 | 24 | Sample Input 25 | 26 | 3 27 | 5 2 28 | 8 5 29 | 2 2 30 | 31 | Sample Output 32 | 33 | 1 34 | 4 35 | 0 -------------------------------------------------------------------------------- /2/Arithmetic.java: -------------------------------------------------------------------------------- 1 | package arithmetic; 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | import java.text.*; 6 | import java.math.*; 7 | import java.util.regex.*; 8 | public class Arithmetic { 9 | 10 | public static void main(String[] args) { 11 | Scanner scan = new Scanner(System.in); 12 | double mealCost = scan.nextDouble(); // original meal price 13 | int tipPercent = scan.nextInt(); // tip percentage 14 | int taxPercent = scan.nextInt(); // tax percentage 15 | scan.close(); 16 | 17 | // Write your calculation code here. 18 | double cost = mealCost * (1 + (tipPercent + taxPercent) / 100.0); 19 | // cast the result of the rounding operation to an int and save it as totalCost 20 | int totalCost = (int) Math.round(cost); 21 | 22 | // Print your result 23 | System.out.println("The total meal cost is " + totalCost + " dollars."); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /3/conditionals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | using namespace std; 23 | 24 | 25 | int main() { 26 | int N; 27 | cin >> N; 28 | 29 | string statement; 30 | 31 | if (0 == (N % 2)) { 32 | if (N < 6) { 33 | statement = "Not Weird"; 34 | } else if (N <= 20) { 35 | statement = "Weird"; 36 | } else { 37 | statement = "Not Weird"; 38 | } 39 | } else { 40 | statement = "Weird"; 41 | } 42 | 43 | // Print your result 44 | cout << statement; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /3/conditionals.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 | var statement = ""; 26 | if (N % 2 == 0) { 27 | if (N <= 5) { 28 | statement = "Not Weird" 29 | } else if (N <= 20) { 30 | statement = "Weird" 31 | } else { 32 | statement = "Not Weird" 33 | } 34 | } else { 35 | statement = "Weird" 36 | } 37 | process.stdout.write(statement) 38 | 39 | } 40 | -------------------------------------------------------------------------------- /25/README.md: -------------------------------------------------------------------------------- 1 | # Day 25: Running Time and Complexity 2 | 3 | https://www.hackerrank.com/challenges/30-running-time-and-complexity 4 | 5 | Task 6 | 7 | A prime is a natural number greater than 1 that has no positive divisors other than and itself. Given a number, n, determine and print whether it's prime or not prime. 8 | 9 | Note: If possible, try to come up with a O(root(n)) primality algorithm, or see what sort of optimizations you come up with for an O(n) algorithm. Be sure to check out the Editorial after submitting your code! 10 | 11 | Input Format 12 | 13 | The first line contains an integer, T, the number of test cases. 14 | Each of the T subsequent lines contains an integer, n, to be tested for primality. 15 | 16 | Constraints 17 | 18 | Output Format 19 | 20 | For each test case, print whether n is Prime or Not prime on a new line. 21 | 22 | Sample Input 23 | 24 | 3 25 | 12 26 | 5 27 | 7 28 | 29 | Sample Output 30 | 31 | Not prime 32 | Prime 33 | Prime -------------------------------------------------------------------------------- /6/stringreview.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 stringreview { 8 | 9 | public static void main(String[] args) { 10 | Scanner scan = new Scanner(System.in); 11 | int n = scan.nextInt(); 12 | String line = ""; 13 | String first = ""; 14 | String second = ""; 15 | line = scan.nextLine(); // throwaway 16 | for (int i = 0; i < n; i++) { 17 | first = ""; 18 | second = ""; 19 | line = scan.nextLine(); 20 | 21 | for (int c = 0; c < line.length(); c++) { 22 | char ch = line.charAt(c); 23 | if (c % 2 == 0) { 24 | first += ch; 25 | } else { 26 | second += ch; 27 | } 28 | } 29 | System.out.println(first + " " + second); 30 | } 31 | scan.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Playing here... 2 | 3 | This is my attempt to keep semi-current with some languages while 4 | learning others, in this case my playing with the HackerRank 30 5 | Tutorial (beginner java), but in this case implementing the challenges 6 | in 7 | 8 | + Java 9 | + JavaScript 10 | + Python 11 | + Clojure 12 | + C++ 13 | 14 | The directories are numbered by the challenge. There should be a 15 | readme, and potentially a script file of some sort to run, or compile 16 | and run the code. 17 | 18 | #30 Days of Code 19 | 20 | https://www.hackerrank.com/domains/tutorials/30-days-of-code 21 | 22 | 30 Days of Code is an opportunity for you to learn how to code or 23 | brush up on your fundamentals. Each day, you’ll unlock a new challenge 24 | and a corresponding video tutorial to help you learn. 25 | 26 | The tutorial is Java-based, but you can also choose other popular 27 | languages to submit your solution. Topics include if-else statements, 28 | recursion, data structures and more. 29 | -------------------------------------------------------------------------------- /8/dictionariesandmaps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | int main() { 9 | int n; 10 | cin >> n; 11 | unordered_map phonebook; 12 | for(int i = 0; i < n; i++){ 13 | string name; 14 | int number; 15 | cin >> name; 16 | cin >> number; 17 | phonebook[name] = number; 18 | } 19 | 20 | // c++11 iterate over map 21 | // for (auto const& x : phonebook) 22 | // { 23 | // std::cout << x.first // string (key) 24 | // << ':' 25 | // << x.second // string's value 26 | // << std::endl ; 27 | // } 28 | 29 | string query; 30 | while(true) { 31 | cin >> query; 32 | auto number = phonebook.find(query); 33 | if (number != phonebook.end()) { 34 | cout << query << "=" << number->second << endl; 35 | } else { 36 | cout << "Not found" << endl; 37 | } 38 | if (cin.eof()) break; 39 | } 40 | return 0; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /1/DataTypes.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class DataTypes { 4 | 5 | public static void main(String[] args) { 6 | int i = 4; 7 | double d = 4.0; 8 | String s = "HackerRank "; 9 | 10 | Scanner scan = new Scanner(System.in); 11 | 12 | /* Declare second integer, double, and String variables. */ 13 | int i2; 14 | double d2; 15 | String s2; 16 | 17 | /* Read and save an integer, double, and String to your variables. */ 18 | i2 = scan.nextInt(); 19 | d2 = scan.nextDouble(); 20 | s2 = scan.nextLine(); 21 | s2 = scan.nextLine(); 22 | 23 | /* Print the sum of both integer variables on a new line. */ 24 | System.out.println(i + i2); 25 | 26 | /* Print the sum of the double variables on a new line. */ 27 | System.out.println(d + d2); 28 | 29 | /* 30 | * Concatenate and print the String variables on a new line; the 's' 31 | * variable above should be printed first. 32 | */ 33 | System.out.println(s + s2); 34 | 35 | scan.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /2/arithmetic.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 | // Reads complete line from STDIN 18 | function readLine() { 19 | return input_stdin_array[input_currentline++]; 20 | } 21 | 22 | function main() { 23 | 24 | var mealCost = parseFloat(readLine()); 25 | var tipPercent = parseInt(readLine()); 26 | var taxPercent = parseInt(readLine()); 27 | 28 | // Write your calculation code here. 29 | var cost = mealCost * (1.0 + (tipPercent + taxPercent) / 100.0); 30 | 31 | // cast the result of the rounding operation to an int and save it as totalCost 32 | var totalCost = Math.round(cost); 33 | 34 | // Print your result 35 | process.stdout.write("The total meal cost is " + totalCost + " dollars."); 36 | } 37 | -------------------------------------------------------------------------------- /6/stringreview.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 walk(line) { 24 | var first = []; 25 | var second = []; 26 | for (i = 0; i < line.length; i++) { 27 | if (i % 2 == 0) { 28 | first.push(line[i]); 29 | } else { 30 | second.push(line[i]); 31 | } 32 | } 33 | var out = first.join("") + " " + second.join(""); 34 | return out; 35 | } 36 | 37 | function main() { 38 | var N = parseInt(readLine()); 39 | var statement = ""; 40 | for (var i = 1; i <= N; i++) { 41 | 42 | var line = readLine(); 43 | statement = walk(line) + "\n"; 44 | process.stdout.write(statement); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /16/README.md: -------------------------------------------------------------------------------- 1 | # Day 16: Exceptions - String to Integer 2 | 3 | https://www.hackerrank.com/challenges/30-exceptions-string-to-integer 4 | 5 | Objective 6 | Today, we're getting started with Exceptions by learning how to parse an integer from a string and print a custom error message. Check out the Tutorial tab for learning materials and an instructional video! 7 | 8 | Task 9 | Read a string, S, and print its integer value; if S cannot be converted to an integer, print Bad String. 10 | 11 | Note: You must use the String-to-Integer and exception handling constructs built into your submission language. If you attempt to use loops/conditional statements, you will get a 0 score. 12 | 13 | Input Format 14 | 15 | A single string, S. 16 | 17 | Constraints 18 | 19 | 1 <= |S| <= 6, where |S| is the length of string S. 20 | S is composed of either lowercase letters (a-z) or decimal digits (0-9). 21 | 22 | Output Format 23 | 24 | Print the parsed integer value of S, or Bad String if S cannot be converted to an integer. 25 | 26 | Sample Input 0 27 | 28 | 3 29 | 30 | Sample Output 0 31 | 32 | 3 33 | 34 | Sample Input 1 35 | 36 | za 37 | 38 | Sample Output 1 39 | 40 | Bad String 41 | -------------------------------------------------------------------------------- /23/bfs.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # Day 23: BST Level-Order Traversal 4 | 5 | # Breadth First Search 6 | 7 | 8 | class Node: 9 | 10 | def __init__(self, data): 11 | self.right = self.left = None 12 | self.data = data 13 | 14 | 15 | class Solution: 16 | 17 | def insert(self, root, data): 18 | if root is None: 19 | return Node(data) 20 | else: 21 | if data <= root.data: 22 | cur = self.insert(root.left, data) 23 | root.left = cur 24 | else: 25 | cur = self.insert(root.right, data) 26 | root.right = cur 27 | return root 28 | 29 | def levelOrder(self, root): 30 | 31 | q = [root] 32 | while len(q) > 0: 33 | node = q.pop(0) 34 | print(node.data, end=" ") 35 | if node.left: 36 | q.append(node.left) 37 | if node.right: 38 | q.append(node.right) 39 | print() 40 | 41 | T = int(input()) 42 | myTree = Solution() 43 | root = None 44 | for i in range(T): 45 | data = int(input()) 46 | root = myTree.insert(root, data) 47 | myTree.levelOrder(root) 48 | -------------------------------------------------------------------------------- /4/classvinstance.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 | function Person(initialAge) { 22 | if (initialAge >= 0) { 23 | this.age = initialAge; 24 | } else { 25 | process.stdout.write("Age is not valid, setting age to 0.\n") 26 | this.age = 0; 27 | } 28 | 29 | // Add some more code to run some checks on initialAge 30 | this.amIOld=function(){ 31 | if (this.age < 13) { 32 | process.stdout.write("You are young.\n"); 33 | } 34 | else if (this.age < 18) { 35 | process.stdout.write("You are a teenager.\n"); 36 | } 37 | else { 38 | process.stdout.write("You are old.\n"); 39 | } 40 | 41 | }; 42 | this.yearPasses=function(){ 43 | // Increment the age of the person in here 44 | this.age++; 45 | }; 46 | } 47 | -------------------------------------------------------------------------------- /8/dictionariesandmaps.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 | if (input_currentline < input_stdin_array.length) { 19 | return input_stdin_array[input_currentline++]; 20 | } else { 21 | return false; 22 | } 23 | } 24 | 25 | /////////////// ignore above this line //////////////////// 26 | 27 | function main() { 28 | var N = parseInt(readLine()); 29 | var statement = ""; 30 | var phonebook = {}; 31 | for (var i = 0; i < N; i++) { 32 | var line = readLine(); 33 | var pair = line.split(" "); 34 | phonebook[pair[0]] = pair[1]; 35 | } 36 | 37 | while (query = readLine()) { 38 | if (query in phonebook) { 39 | process.stdout.write(query + "=" + phonebook[query]); 40 | } else { 41 | process.stdout.write("Not found"); 42 | } 43 | process.stdout.write("\n"); 44 | } 45 | 46 | } 47 | 48 | -------------------------------------------------------------------------------- /22/README.md: -------------------------------------------------------------------------------- 1 | # Day 22: Binary Search Trees 2 | 3 | https://www.hackerrank.com/challenges/30-binary-search-trees 4 | 5 | Objective 6 | 7 | Today, we're working with Binary Search Trees (BSTs). Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | Task 10 | 11 | The height of a binary search tree is the number of edges between the tree's root and its furthest leaf. You are given a pointer, root, pointing to the root of a binary search tree. Complete the getHeight function provided in your editor so that it returns the height of the binary search tree. 12 | 13 | Input Format 14 | 15 | The locked stub code in your editor reads the following inputs and assembles them into a binary search tree: 16 | 17 | The first line contains an integer, n, denoting the number of nodes in the tree. 18 | Each of the n subsequent lines contains an integer, data, denoting the value of an element that must be added to the BST. 19 | 20 | Output Format 21 | 22 | The locked stub code in your editor will print the integer returned by your getHeight function denoting the height of the BST. 23 | 24 | Sample Input 25 | 26 | 7 27 | 3 28 | 5 29 | 2 30 | 1 31 | 4 32 | 6 33 | 7 34 | 35 | Sample Output 36 | 37 | 3 -------------------------------------------------------------------------------- /11/README.md: -------------------------------------------------------------------------------- 1 | #Day 11: 2D Arrays 2 | 3 | Objective 4 | Today, we're building on our knowledge of Arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video! 5 | 6 | Context 7 | Given a 6x6 2D Array, A: 8 | 9 | 1 1 1 0 0 0 10 | 0 1 0 0 0 0 11 | 1 1 1 0 0 0 12 | 0 0 0 0 0 0 13 | 0 0 0 0 0 0 14 | 0 0 0 0 0 0 15 | 16 | We define an hourglass in A to be a subset of values with indices falling in this pattern in A's graphical representation: 17 | 18 | a b c 19 | d 20 | e f g 21 | 22 | There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass' values. 23 | 24 | Task 25 | Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum. 26 | 27 | Input Format 28 | 29 | There are 6 lines of input, where each line contains 6 space-separated integers describing 2D Array A; every value in A will be in the inclusive range of -9 to 9 30 | 31 | Constraints 32 | 33 | + 9 <= A[i][j] <= 9 34 | + 0 <= i, j <= 5 35 | 36 | Output Format 37 | 38 | Print the largest (maximum) hourglass sum found in A. 39 | 40 | Sample Input 41 | 42 | 1 1 1 0 0 0 43 | 0 1 0 0 0 0 44 | 1 1 1 0 0 0 45 | 0 0 2 4 4 0 46 | 0 0 0 2 0 0 47 | 0 0 1 2 4 0 48 | 49 | Sample Output 50 | 51 | 19 52 | -------------------------------------------------------------------------------- /18/palindrome.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | 4 | class Solution: 5 | 6 | def __init__(self): 7 | self.stack = [] 8 | self.queue = [] 9 | 10 | def pushCharacter(self, ch): 11 | self.stack.append(ch) 12 | 13 | def popCharacter(self): 14 | ch = self.stack.pop() 15 | return ch 16 | 17 | def enqueueCharacter(self, ch): 18 | self.queue.append(ch) 19 | 20 | def dequeueCharacter(self): 21 | ch = self.queue.pop(0) 22 | return ch 23 | 24 | 25 | # read the string s 26 | s = input() 27 | # Create the Solution class object 28 | obj = Solution() 29 | 30 | l = len(s) 31 | # push/enqueue all the characters of string s to stack 32 | for i in range(l): 33 | obj.pushCharacter(s[i]) 34 | obj.enqueueCharacter(s[i]) 35 | 36 | isPalindrome = True 37 | ''' 38 | pop the top character from stack 39 | dequeue the first character from queue 40 | compare both the characters 41 | ''' 42 | for i in range(l // 2): 43 | if obj.popCharacter() != obj.dequeueCharacter(): 44 | isPalindrome = False 45 | break 46 | # finally print whether string s is palindrome or not. 47 | if isPalindrome: 48 | print("The word, " + s + ", is a palindrome.") 49 | else: 50 | print("The word, " + s + ", is not a palindrome.") 51 | -------------------------------------------------------------------------------- /23/README.md: -------------------------------------------------------------------------------- 1 | # Day 23: BST Level-Order Traversal 2 | 3 | https://www.hackerrank.com/challenges/30-binary-trees 4 | 5 | Objective 6 | 7 | Today, we're going further with Binary Search Trees. Check out the Tutorial tab for 8 | learning materials and an instructional video! 9 | 10 | Task 11 | 12 | A level-order traversal, also known as a breadth-first search, visits each level of a tree's nodes from left to right, top to bottom. You are given a pointer, root, pointing to the root of a binary search tree. Complete the levelOrder function provided in your editor so that it prints the level-order traversal of the binary search tree. 13 | 14 | Hint: You'll find a queue helpful in completing this challenge. 15 | 16 | Input Format 17 | 18 | The locked stub code in your editor reads the following inputs and assembles them into a BST: 19 | 20 | The first line contains an integer, T (the number of test cases). 21 | The T subsequent lines each contain an integer, data, denoting the value of an element that must be added to the BST. 22 | 23 | Output Format 24 | 25 | Print the data value of each node in the tree's level-order traversal as a single line of N space-separated integers. 26 | 27 | Sample Input 28 | 29 | 6 30 | 3 31 | 5 32 | 4 33 | 7 34 | 2 35 | 1 36 | 37 | Sample Output 38 | 39 | 3 2 5 1 4 7 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /19/README.md: -------------------------------------------------------------------------------- 1 | # Day 19: Interfaces 2 | 3 | https://www.hackerrank.com/challenges/30-interfaces 4 | 5 | bjective 6 | Today, we're learning about Interfaces. Check out the Tutorial tab for learning materials and an instructional video! 7 | 8 | Task 9 | The AdvancedArithmetic interface and the method declaration for the abstract int divisorSum(int n) method are provided for you in the editor below. Write the Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum method must be public and take an integer parameter, , and return the sum of all its divisors. 10 | 11 | Note: Because we are writing multiple classes in the same file, do not use an access modifier (e.g.: public) in your class declaration (or your code will not compile); however, you must use the public access modifier before your method declaration for it to be accessible by the other classes in the file. 12 | 13 | Input Format 14 | 15 | A single line containing an integer, . 16 | 17 | Constraints 18 | 19 | Output Format 20 | 21 | You are not responsible for printing anything to stdout. The locked Solution class in the editor below will call your code and print the necessary output. 22 | 23 | Sample Input 24 | 25 | 6 26 | Sample Output 27 | 28 | I implemented: AdvancedArithmetic 29 | 12 30 | 31 | -------------------------------------------------------------------------------- /3/README.md: -------------------------------------------------------------------------------- 1 | #Day 3: Conditionals 2 | 3 | https://www.hackerrank.com/challenges/30-conditional-statements 4 | 5 | ##Objective 6 | 7 | In this challenge, we're getting started with conditional statements. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | ##Task 10 | 11 | Given an integer, , perform the following conditional actions: 12 | 13 | If n is odd, print Weird 14 | If n is even and in the inclusive range of 2 to 5, print Not Weird 15 | If n is even and in the inclusive range of 6 to 20, print Weird 16 | If n is even and greater than 20 , print Not Weird 17 | 18 | Complete the stub code provided in your editor to print whether or not is weird. 19 | 20 | ##Input Format 21 | 22 | A single line containing a positive integer, . 23 | 24 | ## Output Format 25 | 26 | Constraints 27 | 28 | i <= n <= 10 29 | 30 | ## Output Format 31 | 32 | Print if the number is weird; otherwise, print . 33 | 34 | ## Sample Input 0 35 | 36 | 3 37 | 38 | ## Sample Output 0 39 | 40 | Weird 41 | 42 | ## Sample Input 1 43 | 44 | 24 45 | 46 | ## Sample Output 1 47 | 48 | Not Weird 49 | 50 | ## Explanation 51 | 52 | Sample Case 0: 53 | n is odd and odd numbers are weird, so we print Weird 54 | 55 | Sample Case 1: 56 | n and n is even, so it isn't weird. Thus, we print Not Weird -------------------------------------------------------------------------------- /1/datatypes.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 | // Reads complete line from STDIN 18 | function readLine() { 19 | return input_stdin_array[input_currentline++]; 20 | } 21 | 22 | function main() { 23 | var i = 4; 24 | var d = 4.0; 25 | var s = "HackerRank "; 26 | 27 | // Declare second integer, double, and String variables. 28 | var i2, d, s; 29 | 30 | // Read and save an integer, double, and String to your variables. 31 | var i2 = parseInt(readLine()); 32 | 33 | // Print the sum of both integer variables on a new line. 34 | var d2 = parseFloat(readLine()); 35 | 36 | // Print the sum of the double variables on a new line. 37 | var s2 = readLine(); 38 | 39 | // Concatenate and print the String variables on a new line 40 | // The 's' variable above should be printed first. 41 | 42 | process.stdout.write(i + i2 + "\n"); 43 | process.stdout.write((d + d2).toFixed(1) + "\n"); 44 | process.stdout.write(s + s2 + "\n"); 45 | } 46 | -------------------------------------------------------------------------------- /28/README.md: -------------------------------------------------------------------------------- 1 | # Day 28: RegEx, Patterns 2 | 3 | # https://www.hackerrank.com/challenges/30-regex-patterns 4 | 5 | Task 6 | 7 | Consider a database table, Emails, which has the attributes First Name and Email ID. Given N rows of data simulating the Emails table, print an alphabetically-ordered list of people whose email address ends in @gmail.com 8 | 9 | Input Format 10 | 11 | The first line contains an integer, N, total number of rows in the table. 12 | Each of the N subsequent lines contains 2 space-separated strings denoting a person's first name and email ID, respectively. 13 | 14 | Constraints 15 | 16 | Each of the first names consists of lower case letters only. 17 | 18 | Each of the email IDs consists of lower case letters , @ and . only. 19 | 20 | The length of the first name is no longer than 20. 21 | 22 | The length of the email ID is no longer than 50. 23 | 24 | Output Format 25 | 26 | Print an alphabetically-ordered list of first names for every user with a gmail account. Each name must be printed on a new line. 27 | 28 | Sample Input 29 | 30 | 6 31 | riya riya@gmail.com 32 | julia julia@julia.me 33 | julia sjulia@gmail.com 34 | julia julia@gmail.com 35 | samantha samantha@gmail.com 36 | tanya tanya@gmail.com 37 | 38 | Sample Output 39 | 40 | julia 41 | julia 42 | riya 43 | samantha 44 | tanya -------------------------------------------------------------------------------- /12/scores.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | 4 | class Person: 5 | 6 | def __init__(self, firstName, lastName, idNumber): 7 | self.firstName = firstName 8 | self.lastName = lastName 9 | self.idNumber = idNumber 10 | 11 | def printPerson(self): 12 | print("Name:", self.lastName + ",", self.firstName) 13 | print("ID:", self.idNumber) 14 | 15 | 16 | class Student(Person): 17 | 18 | def __init__(self, firstName, lastName, idNumber, scores): 19 | self.firstName = firstName 20 | self.lastName = lastName 21 | self.idNumber = idNumber 22 | self.scores = scores 23 | 24 | def calculate(self): 25 | avg = sum(scores) / len(scores) 26 | if avg >= 90: 27 | return "O" 28 | if avg >= 80: 29 | return "E" 30 | if avg >= 70: 31 | return "A" 32 | if avg >= 55: 33 | return "P" 34 | if avg >= 40: 35 | return "D" 36 | return "T" 37 | 38 | 39 | line = input().split() 40 | firstName = line[0] 41 | lastName = line[1] 42 | idNum = line[2] 43 | numScores = int(input()) # not needed for Python 44 | scores = list(map(int, input().split())) 45 | s = Student(firstName, lastName, idNum, scores) 46 | s.printPerson() 47 | print("Grade:", s.calculate()) 48 | -------------------------------------------------------------------------------- /4/go/classvinstance.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type person struct { 6 | age int 7 | } 8 | 9 | func (p person) NewPerson(initialAge int) person { 10 | //Add some more code to run some checks on initialAge 11 | if initialAge < 0 { 12 | fmt.Println("Age is not valid, setting age to 0.") 13 | p.age = 0 14 | } else { 15 | p.age = initialAge 16 | } 17 | return p 18 | } 19 | 20 | func (p person) amIOld() { 21 | //Do some computation in here and print out the correct statement to the console 22 | 23 | var statement string 24 | if p.age < 13 { 25 | statement = "You are young." 26 | } else if p.age < 18 { 27 | statement = "You are a teenager." 28 | } else { 29 | statement = "You are old." 30 | } 31 | // Write code determining if this person's age is old and print the correct statement: 32 | fmt.Println(statement) 33 | 34 | } 35 | 36 | func (p person) yearPasses() person { 37 | //Increment the age of the person in here 38 | p.age++ 39 | 40 | return p 41 | } 42 | 43 | func main() { 44 | var T, age int 45 | 46 | fmt.Scan(&T) 47 | 48 | for i := 0; i < T; i++ { 49 | fmt.Scan(&age) 50 | p := person{age: age} 51 | p = p.NewPerson(age) 52 | p.amIOld() 53 | for j := 0; j < 3; j++ { 54 | p = p.yearPasses() 55 | } 56 | p.amIOld() 57 | fmt.Println() 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /24/linkedlist.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 3 2 | 3 | # Day 24: more linked lists 4 | 5 | 6 | class Node: 7 | 8 | def __init__(self, data): 9 | self.data = data 10 | self.next = None 11 | 12 | 13 | class Solution: 14 | 15 | def insert(self, head, data): 16 | p = Node(data) 17 | if head is None: 18 | head = p 19 | elif head.next is None: 20 | head.next = p 21 | else: 22 | start = head 23 | while(start.next is not None): 24 | start = start.next 25 | start.next = p 26 | return head 27 | 28 | def display(self, head): 29 | current = head 30 | while current: 31 | print(current.data, end=' ') 32 | current = current.next 33 | 34 | def removeDuplicates(self, head): 35 | current = head 36 | num = current.data 37 | while current.next is not None: 38 | if current.next.data == num: 39 | current.next = current.next.next 40 | else: 41 | current = current.next 42 | num = current.data 43 | return head 44 | 45 | mylist = Solution() 46 | T = int(input()) 47 | head = None 48 | for i in range(T): 49 | data = int(input()) 50 | head = mylist.insert(head, data) 51 | head = mylist.removeDuplicates(head) 52 | mylist.display(head) 53 | -------------------------------------------------------------------------------- /2/README.md: -------------------------------------------------------------------------------- 1 | #Day 2: Operators 2 | 3 | https://www.hackerrank.com/challenges/30-operators 4 | 5 | ##Objective 6 | 7 | In this challenge, you'll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | ##Task 10 | 11 | Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost. 12 | 13 | Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result! 14 | 15 | ##Input Format 16 | 17 | There are 33 lines of numeric input: 18 | The first line has a double, mealCostmealCost (the cost of the meal before tax and tip). 19 | The second line has an integer, tipPercenttipPercent (the percentage of mealCostmealCost being added as tip). 20 | The third line has an integer, taxPercenttaxPercent (the percentage of mealCostmealCost being added as tax). 21 | 22 | ##Output Format 23 | 24 | Print The total meal cost is totalCost dollars.The total meal cost is totalCost dollars., where totalCosttotalCost is the rounded integer result of the entire bill (mealCostmealCost with added tax and tip). 25 | 26 | ##Sample Input 27 | 28 | 12.00 29 | 20 30 | 8 31 | 32 | Sample Output 33 | 34 | The total meal cost is 15 dollars. 35 | -------------------------------------------------------------------------------- /4/classvinstance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Person { 5 | public: 6 | int age; 7 | Person(int initialAge); 8 | void amIOld(); 9 | void yearPasses(); 10 | }; 11 | 12 | Person::Person(int initialAge) { 13 | // Add some more code to run some checks on initialAge 14 | if (initialAge >= 0) { 15 | age = initialAge; 16 | } else { 17 | cout << ("Age is not valid, setting age to 0.") << endl; 18 | age = 0; 19 | } 20 | } 21 | 22 | void Person::amIOld(){ 23 | // Do some computations in here and print out the correct statement to the console 24 | statement string; 25 | if (age < 13) { 26 | statement = "You are young."; 27 | } 28 | else if (age < 18) { 29 | statement = "You are a teenager."; 30 | } 31 | else { 32 | statement = "You are old."; 33 | } 34 | // Write code determining if this person's age is old and print the correct statement: 35 | cout << statement << endl; 36 | 37 | } 38 | 39 | void Person::yearPasses(){ 40 | // Increment the age of the person in here 41 | age++; 42 | } 43 | 44 | 45 | int main() { 46 | int t; 47 | int age; 48 | cin >> t; 49 | for(int i=0; i < t; i++) { 50 | cin >> age; 51 | Person p(age); 52 | p.amIOld(); 53 | for(int j=0; j < 3; j++) { 54 | p.yearPasses(); 55 | } 56 | p.amIOld(); 57 | 58 | cout << '\n'; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /4/classvinstance.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Person { 5 | private int age; 6 | 7 | public Person(int initialAge) { 8 | // Add some more code to run some checks on initialAge 9 | if (initialAge >= 0) { 10 | age = initialAge; 11 | } else { 12 | System.out.println("Age is not valid, setting age to 0."); 13 | age = 0; 14 | } 15 | } 16 | 17 | public void amIOld() { 18 | String statement; 19 | if (age < 13) { 20 | statement = "You are young."; 21 | } 22 | else if (age < 18) { 23 | statement = "You are a teenager."; 24 | } 25 | else { 26 | statement = "You are old."; 27 | } 28 | // Write code determining if this person's age is old and print the correct statement: 29 | System.out.println(statement); 30 | } 31 | public void yearPasses() { 32 | // Increment this person's age. 33 | age++; 34 | } 35 | 36 | public static void main(String[] args) { 37 | Scanner sc = new Scanner(System.in); 38 | int T = sc.nextInt(); 39 | for (int i = 0; i < T; i++) { 40 | int age = sc.nextInt(); 41 | Person p = new Person(age); 42 | p.amIOld(); 43 | for (int j = 0; j < 3; j++) { 44 | p.yearPasses(); 45 | } 46 | p.amIOld(); 47 | System.out.println(); 48 | } 49 | sc.close(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /24/README.md: -------------------------------------------------------------------------------- 1 | # Day 24: More Linked Lists 2 | 3 | https://www.hackerrank.com/challenges/30-linked-list-deletion 4 | 5 | Task 6 | 7 | A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in a list). 8 | 9 | A removeDuplicates function is declared in your editor, which takes a pointer to the node of a linked list as a parameter. Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list. 10 | 11 | Note: The pointer may be null, indicating that the list is empty. Be sure to reset your pointer when performing deletions to avoid breaking the list. 12 | 13 | Input Format 14 | 15 | You do not need to read any input from stdin. The following input is handled by the locked stub code and passed to the removeDuplicates function: 16 | 17 | The first line contains an integer, N, the number of nodes to be inserted. 18 | 19 | The subsequent lines each contain an integer describing the value of a node being inserted at the list's tail. 20 | 21 | Constraints 22 | 23 | The data elements of the linked list argument will always be in non-decreasing order. 24 | Output Format 25 | 26 | Your removeDuplicates function should return the head of the updated linked list. The locked stub code in your editor will print the returned list to stdout. 27 | 28 | Sample Input 29 | 30 | 6 31 | 1 32 | 2 33 | 2 34 | 3 35 | 3 36 | 4 37 | 38 | Sample Output 39 | 40 | 1 2 3 4 -------------------------------------------------------------------------------- /29/out: -------------------------------------------------------------------------------- 1 | 415 2 | 113 3 | 106 4 | 67 5 | 85 6 | 76 7 | 695 8 | 130 9 | 797 10 | 101 11 | 591 12 | 41 13 | 113 14 | 9 15 | 613 16 | 96 17 | 21 18 | 456 19 | 162 20 | 319 21 | 159 22 | 25 23 | 124 24 | 304 25 | 384 26 | 268 27 | 414 28 | 691 29 | 215 30 | 365 31 | 629 32 | 166 33 | 229 34 | 71 35 | 361 36 | 48 37 | 308 38 | 94 39 | 583 40 | 176 41 | 58 42 | 67 43 | 182 44 | 456 45 | 48 46 | 104 47 | 193 48 | 33 49 | 33 50 | 972 51 | 150 52 | 57 53 | 149 54 | 12 55 | 9 56 | 133 57 | 76 58 | 72 59 | 234 60 | 608 61 | 620 62 | 57 63 | 23 64 | 276 65 | 50 66 | 363 67 | 244 68 | 654 69 | 74 70 | 97 71 | 434 72 | 546 73 | 100 74 | 43 75 | 79 76 | 84 77 | 275 78 | 200 79 | 403 80 | 535 81 | 94 82 | 158 83 | 114 84 | 364 85 | 366 86 | 597 87 | 9 88 | 77 89 | 538 90 | 200 91 | 67 92 | 427 93 | 138 94 | 45 95 | 13 96 | 183 97 | 209 98 | 223 99 | 652 100 | 298 101 | 2 102 | 246 103 | 7 104 | 170 105 | 278 106 | 633 107 | 735 108 | 120 109 | 132 110 | 741 111 | 12 112 | 311 113 | 22 114 | 163 115 | 83 116 | 191 117 | 573 118 | 87 119 | 397 120 | 132 121 | 352 122 | 386 123 | 101 124 | 30 125 | 55 126 | 73 127 | 114 128 | 380 129 | 11 130 | 76 131 | 111 132 | 96 133 | 10 134 | 144 135 | 180 136 | 116 137 | 197 138 | 555 139 | 120 140 | 274 141 | 230 142 | 353 143 | 117 144 | 120 145 | 688 146 | 211 147 | 126 148 | 10 149 | 717 150 | 3 151 | 348 152 | 507 153 | 178 154 | 57 155 | 663 156 | 470 157 | 295 158 | 158 159 | 56 160 | 52 161 | 672 162 | 500 163 | -------------------------------------------------------------------------------- /29/output1: -------------------------------------------------------------------------------- 1 | 415 2 | 113 3 | 106 4 | 67 5 | 85 6 | 76 7 | 695 8 | 130 9 | 797 10 | 101 11 | 591 12 | 41 13 | 113 14 | 9 15 | 613 16 | 96 17 | 21 18 | 456 19 | 162 20 | 319 21 | 159 22 | 25 23 | 124 24 | 304 25 | 384 26 | 268 27 | 414 28 | 691 29 | 215 30 | 365 31 | 629 32 | 166 33 | 229 34 | 71 35 | 361 36 | 48 37 | 308 38 | 94 39 | 583 40 | 176 41 | 58 42 | 67 43 | 182 44 | 456 45 | 48 46 | 104 47 | 193 48 | 33 49 | 33 50 | 972 51 | 150 52 | 57 53 | 149 54 | 12 55 | 9 56 | 133 57 | 76 58 | 72 59 | 234 60 | 608 61 | 620 62 | 57 63 | 23 64 | 276 65 | 50 66 | 363 67 | 244 68 | 654 69 | 74 70 | 97 71 | 434 72 | 546 73 | 100 74 | 43 75 | 79 76 | 84 77 | 275 78 | 200 79 | 403 80 | 535 81 | 94 82 | 158 83 | 114 84 | 364 85 | 366 86 | 597 87 | 9 88 | 77 89 | 538 90 | 200 91 | 67 92 | 427 93 | 138 94 | 45 95 | 13 96 | 183 97 | 209 98 | 223 99 | 652 100 | 298 101 | 2 102 | 246 103 | 7 104 | 170 105 | 278 106 | 633 107 | 735 108 | 120 109 | 132 110 | 741 111 | 12 112 | 311 113 | 22 114 | 163 115 | 83 116 | 191 117 | 573 118 | 87 119 | 397 120 | 132 121 | 352 122 | 386 123 | 101 124 | 30 125 | 55 126 | 73 127 | 114 128 | 380 129 | 11 130 | 76 131 | 111 132 | 96 133 | 10 134 | 144 135 | 180 136 | 116 137 | 197 138 | 555 139 | 120 140 | 274 141 | 230 142 | 353 143 | 117 144 | 120 145 | 688 146 | 211 147 | 126 148 | 10 149 | 717 150 | 3 151 | 348 152 | 507 153 | 178 154 | 57 155 | 663 156 | 470 157 | 295 158 | 158 159 | 56 160 | 52 161 | 672 162 | 500 163 | -------------------------------------------------------------------------------- /22/bst.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | # Day 22: binary search trees 4 | 5 | 6 | class Node: 7 | 8 | def __init__(self, data): 9 | self.right = self.left = None 10 | self.data = data 11 | 12 | 13 | class Solution: 14 | 15 | def insert(self, root, data): 16 | if root is None: 17 | return Node(data) 18 | else: 19 | if data <= root.data: 20 | cur = self.insert(root.left, data) 21 | root.left = cur 22 | else: 23 | cur = self.insert(root.right, data) 24 | root.right = cur 25 | return root 26 | 27 | def getHeight(self, root): 28 | if root is None: 29 | return 0 30 | 31 | # print("n: " + str(root.data)) 32 | 33 | if root.left is None and root.right is None: 34 | return 0 35 | 36 | hleft = self.getHeight(root.left) 37 | hright = self.getHeight(root.right) 38 | 39 | height = 1 + max(hleft, hright) 40 | # print("n: " + str(root.data) + " height: " + str(height)) 41 | return height 42 | 43 | def printTree(self, root): 44 | if root is None: 45 | return 46 | 47 | # print(root.data) 48 | 49 | self.printTree(root.left) 50 | self.printTree(root.right) 51 | 52 | T = int(input()) 53 | myTree = Solution() 54 | root = None 55 | for i in range(T): 56 | data = int(input()) 57 | print("insert " + str(data)) 58 | root = myTree.insert(root, data) 59 | height = myTree.getHeight(root) 60 | print(height) 61 | -------------------------------------------------------------------------------- /17/README.md: -------------------------------------------------------------------------------- 1 | # Day 17: More Exceptions 2 | 3 | https://www.hackerrank.com/challenges/30-more-exceptions 4 | 5 | Objective 6 | 7 | Yesterday's challenge taught you to manage exceptional situations by using try and catch blocks. In today's challenge, you're going to practice throwing and propagating an exception. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | Task 10 | 11 | Write a Calculator class with a single method: int power(int,int). The power method takes two integers, n and p, as parameters and returns the integer result of n^p. If either n or p is negative, then the method must throw an exception with the message: n and p should be non-negative. 12 | 13 | Note: Do not use an access modifier (e.g.: public) in the declaration for your Calculator class. 14 | 15 | Input Format 16 | 17 | Input from stdin is handled for you by the locked stub code in your editor. The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers denoting n and p, respectively. 18 | 19 | Constraints 20 | 21 | No Test Case will result in overflow for correctly written code. 22 | 23 | Output Format 24 | 25 | Output to stdout is handled for you by the locked stub code in your editor. There are T lines of output, where each line contains the result of n^p as calculated by your Calculator class' power method. 26 | 27 | Sample Input 28 | 29 | 4 30 | 3 5 31 | 2 4 32 | -1 -2 33 | -1 3 34 | 35 | Sample Output 36 | 37 | 243 38 | 16 39 | n and p should be non-negative 40 | n and p should be non-negative -------------------------------------------------------------------------------- /26/README.md: -------------------------------------------------------------------------------- 1 | # Day 26: Nested Logic 2 | 3 | https://www.hackerrank.com/challenges/30-nested-logic 4 | 5 | 6 | Objective 7 | 8 | Today's challenge puts your understanding of nested conditional statements to the test. You already have the knowledge to complete this challenge, but check out the Tutorial tab for a video on testing! 9 | 10 | Task 11 | 12 | Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows: 13 | 14 | If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine = 0 15 | 16 | If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, fine = 15 Hackos * num days late 17 | 18 | If the book is returned after the expected return month but still within the same calendar year as the expected return date, the fine = 500 Hackos * num months late 19 | 20 | If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos 21 | 22 | Input Format 23 | 24 | The first line contains 3 space-separated integers denoting the respective day, month, and year on which the book was actually returned. 25 | 26 | The second line contains 3 space-separated integers denoting the respective day, month, and year on which the book was expected to be returned (due date). 27 | 28 | Constraints 29 | 30 | Output Format 31 | 32 | Print a single integer denoting the library fine for the book received as input. 33 | 34 | Sample Input 35 | 36 | 9 6 2015 37 | 6 6 2015 38 | 39 | Sample Output 40 | 41 | 45 42 | -------------------------------------------------------------------------------- /1/README.md: -------------------------------------------------------------------------------- 1 | 2 | **Objective** 3 | Today, we're discussing data types. Check out the [Tutorial]("/challenges/30-data-types/tutorial") tab for learning materials and an instructional video! 4 | 5 | **Task** 6 | Complete the code in the editor below. The variables iii, ddd, and sss are already declared and initialized for you. You must declare 333 variables: one of type _int_, one of type _double_, and one of type _String_. Then you must read 333 lines of input from stdin and initialize your 333 variables. Finally, you must use the +++ operator to perform the following operations: 7 | 8 | - Print the sum of iii plus your int variable on a new line. 9 | - Print the sum of ddd plus your double variable to a scale of one decimal place on a new line. 10 | - Concatenate sss with the string you read as input and print the result on a new line. 11 | 12 | **Note:** If you are using a language that doesn't support using +++ for string concatenation (e.g.: CC\text{C}), you can just print one variable immediately following the other on the same line. The string provided in your editor _must_ be printed first, immediately followed by the string you read as input. 13 | 14 | **Input Format** 15 | 16 | The first line contains an integer, iii. 17 | The second line contains a double, ddd. 18 | The third line contains a string, sss. 19 | 20 | **Output Format** 21 | 22 | Print the sum of both integers on the first line, the sum of both doubles on the second line, and then the two concatenated strings on the third line. 23 | 24 | **Sample Input** 25 | 26 | 27 | ``` 28 | 12 29 | 4.0 30 | is the best place to learn and practice coding! 31 | ``` 32 | 33 | **Sample Output** 34 | 35 | 36 | ``` 37 | 16 38 | 8.0 39 | HackerRank is the best place to learn and practice coding! 40 | ``` 41 | 42 | -------------------------------------------------------------------------------- /4/README.md: -------------------------------------------------------------------------------- 1 | #Day 3: Conditionals 2 | 3 | https://www.hackerrank.com/challenges/30-class-vs-instance 4 | 5 | ##Objective 6 | 7 | In this challenge, we're going to learn about the difference between a class and an instance; because this is an Object Oriented concept, it's only enabled in certain languages. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | ##Task 10 | 11 | Write a Person class with an instance variable, age, and a constructor that takes an integer, initialAge, as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as age, the constructor should set age to 0 and print Age is not valid. In addition, you must write the following instance methods: 12 | 13 | 1. yearPasses() should increase the age instance variable by 1. 14 | 2. amIOld() should perform the following conditional actions: 15 | + If age < 13, print You are young. 16 | + If age >= 13 and < 18, print You are a teenager. 17 | + Otherwise, print You are old. 18 | 19 | To help you learn by example and complete this challenge, much of the code is provided for you, but you'll be writing everything in the future. The code that creates each instance of your Person class is in the main method. Don't worry if you don't understand it all quite yet! 20 | 21 | Note: Do not remove or alter the stub code in the editor. 22 | 23 | ##Input Format 24 | 25 | 26 | ## Output Format 27 | 28 | Constraints 29 | 30 | ## Output Format 31 | 32 | 33 | ## Sample Input 34 | 4 35 | -1 36 | 10 37 | 16 38 | 18 39 | 40 | ## Sample Output 41 | 42 | Age is not valid, setting age to 0. 43 | You are young. 44 | You are young. 45 | 46 | You are young. 47 | You are a teenager. 48 | 49 | You are a teenager. 50 | You are old. 51 | 52 | You are old. 53 | You are old. -------------------------------------------------------------------------------- /15/README.md: -------------------------------------------------------------------------------- 1 | # Day 15: Linked List 2 | 3 | https://www.hackerrank.com/challenges/30-linked-list 4 | 5 | Objective 6 | 7 | Today we're working with Linked Lists. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in a list). 10 | 11 | A Node insert function is also declared in your editor. It has two parameters: a pointer, head, pointing to the first node of a linked list, and an integer data value that must be added to the end of the list as a new Node object. 12 | 13 | Task 14 | Complete the insert function in your editor so that it creates a new Node (pass data as the Node constructor argument) and inserts it at the tail of the linked list referenced by the head parameter. Once the new node is added, return the reference to the head node. 15 | 16 | Note: If the argument passed to the insert function is null, then the initial list is empty. 17 | 18 | Input Format 19 | 20 | The insert function has 2 parameters: a pointer to a Node named head, and an integer value, data. 21 | 22 | The constructor for Node has 1 parameter: an integer value for the data field. 23 | 24 | You do not need to read anything from stdin. 25 | 26 | Output Format 27 | 28 | Your insert function should return a reference to the head node of the linked list. 29 | 30 | Sample Input 31 | 32 | The following input is handled for you by the locked code in the editor: 33 | The first line contains T, the number of test cases. 34 | The subsequent lines of test cases each contain an integer to be inserted at the list's tail. 35 | 36 | 4 37 | 2 38 | 3 39 | 4 40 | 1 41 | 42 | Sample Output 43 | 44 | The locked code in your editor prints the ordered data values for each element in your list as a single line of space-separated integers: 45 | 46 | 2 3 4 1 -------------------------------------------------------------------------------- /12/README.md: -------------------------------------------------------------------------------- 1 | # Day 12: Inheritance 2 | 3 | https://www.hackerrank.com/challenges/30-inheritance 4 | 5 | Objective 6 | Today, we're delving into Inheritance. Check out the Tutorial tab for learning materials and an instructional video! 7 | 8 | Task 9 | You are given two classes, Person and Student, where Person is the base class and Student is the derived class. Completed code for Person and a declaration for Student are provided for you in the editor. Observe that Student inherits all the properties of Person. 10 | 11 | Complete the Student class by writing the following: 12 | 13 | A Student class constructor, which has parameters: 14 | A string, first name 15 | A string, last name 16 | An integer, id 17 | An integer array (or vector) of test scores, scores 18 | 19 | A char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average: 20 | 21 | O avg >= 90 22 | E avg >= 80 23 | A avg >= 70 24 | P avg >= 55 25 | D avg >= 40 26 | T avg >= 0 27 | 28 | Input Format 29 | 30 | The locked stub code in your editor calls your Student class constructor and passes it the necessary arguments. It also calls the calculate method (which takes no arguments). 31 | 32 | You are not responsible for reading the following input from stdin: 33 | The first line contains first name, lastname, and id, respectively. The second line contains the number of test scores. The third line of space-separated integers describes scores. 34 | 35 | Constraints 36 | 37 | 4 <= |first name|, |lastname| < 10 38 | 39 | |id| 7 40 | 41 | 0 <= score, average <= 100 42 | 43 | Output Format 44 | 45 | This is handled by the locked stub code in your editor. Your output will be correct if your Student class constructor and calculate() method are properly implemented. 46 | 47 | Sample Input 48 | 49 | Heraldo Memelli 8135627 50 | 2 51 | 100 80 52 | Sample Output 53 | 54 | Name: Memelli, Heraldo 55 | ID: 8135627 56 | Grade: O 57 | 58 | -------------------------------------------------------------------------------- /13/README.md: -------------------------------------------------------------------------------- 1 | # Day 13: Abstract Classes 2 | 3 | Objective 4 | Today, we're taking what we learned yesterday about Inheritance and extending it to Abstract Classes. Because this is a very specific Object-Oriented concept, submissions are limited to the few languages that use this construct. Check out the Tutorial tab for learning materials and an instructional video! 5 | 6 | Task 7 | Given a Book class and a Solution class, write a MyBook class that does the following: 8 | 9 | Inherits from Book 10 | 11 | Has a parameterized constructor taking these parameters: 12 | string title 13 | string author 14 | int price 15 | 16 | Implements the Book class' abstract display() method so it prints these lines: 17 | Title, a space, and then the current instance's title. 18 | author, a space, and then the current instance's author. 19 | price, a space, and then the current instance's price. 20 | 21 | Note: Because these classes are being written in the same file, you must not use an access modifier (e.g.: ) when declaring MyBook or your code will not execute. 22 | 23 | Input Format 24 | 25 | You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object. 26 | 27 | Output Format 28 | 29 | The method should print and label the respective title, author, and price of the MyBook object's instance (with each value on its own line) like so: 30 | 31 | Title: $title 32 | Author: $author 33 | Price: $price 34 | 35 | Note: The $ is prepended to variable names to indicate they are placeholders for variables. 36 | 37 | Sample Input 38 | 39 | The following input from stdin is handled by the locked stub code in your editor: 40 | 41 | The Alchemist 42 | Paulo Coelho 43 | 248 44 | 45 | Sample Output 46 | 47 | The following output is printed by your display() method: 48 | 49 | Title: The Alchemist 50 | Author: Paulo Coelho 51 | Price: 248 52 | -------------------------------------------------------------------------------- /27/README.md: -------------------------------------------------------------------------------- 1 | # Day 27: Testing 2 | 3 | This challenge is very different from the others we've completed because it requires you to generate a valid test case for a problem instead of solving the problem. There is no input to read, you simply have to generate and print test values for the problem that satisfy both the problem's Input Format and the criteria laid out in the Task section. Check out the Tutorial tab for an instructional video on testing! 4 | 5 | Consider the following problem (but do not solve it): 6 | 7 | Problem Statement 8 | 9 | A Discrete Mathematics professor has a class of students. Frustrated with their lack of discipline, the professor decides to cancel class if fewer than students are present when class starts. Given the arrival time of each student, determine if the class is canceled. 10 | Input Format 11 | 12 | The first line of input contains , the number of lectures. 13 | The information for each lecture spans two lines. The first line has two space-separated integers, (the number of students in the class) and (the cancelation threshold). The second line contains space-separated integers describing the array of students' arrival times (). 14 | 15 | Note: Non-positive arrival times () indicate the student arrived early or on time; positive arrival times () indicate the student arrived minutes late. If a student arrives exactly on time , the student is considered to have entered before the class started. 16 | Output Format 17 | 18 | For each test case, print the word YES if the class is canceled or NO if it is not. 19 | Example 20 | 21 | When properly solved, this input: 22 | 2 23 | 4 3 24 | -1 -3 4 2 25 | 4 2 26 | 0 -1 2 1 27 | Produces this output: 28 | YES 29 | NO 30 | For the first test case, . The professor wants at least students in attendance, but only arrive on time ( and ). Thus, the class is canceled. 31 | 32 | For the second test case, . The professor wants at least students in attendance, and do arrive on time ( and ). Thus, the class is not canceled. -------------------------------------------------------------------------------- /18/README.md: -------------------------------------------------------------------------------- 1 | #Day 18: Queues and Stacks 2 | 3 | https://www.hackerrank.com/challenges/30-queues-stacks 4 | 5 | Welcome to Day 18! Today we're learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video! 6 | 7 | A palindrome is a word, phrase, number, or other sequence of characters which reads the same backwards and forwards. Can you determine if a given string, s, is a palindrome? 8 | 9 | To solve this challenge, we must first take each character in s, enqueue it in a queue, and also push that same character onto a stack. Once that's done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means s isn't a palindrome). 10 | 11 | Write the following declarations and implementations: 12 | 13 | Two instance variables: one for your stack, and one for your queue. 14 | A void pushCharacter(char ch) method that pushes a character onto a stack. 15 | A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable. 16 | A char popCharacter() method that pops and returns the character at the top of the stack instance variable. 17 | A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable. 18 | 19 | Input Format 20 | 21 | You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string s. It then calls the methods specified above to pass each character to your instance variables. 22 | 23 | Constraints 24 | 25 | s is composed of lowercase English letters. 26 | 27 | Output Format 28 | 29 | You are not responsible for printing any output to stdout. 30 | If your code is correctly written and s is a palindrome, the locked stub code will print The word s is a palindrome; otherwise, it will print it is not 31 | 32 | Sample Input 33 | 34 | racecar 35 | 36 | Sample Output 37 | 38 | The word, racecar, is a palindrome. -------------------------------------------------------------------------------- /14/README.md: -------------------------------------------------------------------------------- 1 | # Day 14: Scope 2 | 3 | https://www.hackerrank.com/challenges/30-scope 4 | 5 | Objective 6 | Today we're discussing scope. Check out the Tutorial tab for learning materials and an instructional video! 7 | 8 | The absolute difference between two integers, a and b, is written as |a - b|. The maximum absolute difference between two integers in a set of positive integers, , is the largest absolute difference between any two integers in . 9 | 10 | The Difference class is started for you in the editor. It has a private integer array (elements) for storing non-negative integers, and a public integer (maximumDifference) for storing the maximum absolute difference. 11 | 12 | Task 13 | Complete the Difference class by writing the following: 14 | 15 | A class constructor that takes an array of integers as a parameter and saves it to the element instance variable. 16 | 17 | A computeDifference method that finds the maximum absolute difference between any 3 numbers in N and stores it in the maximumDifference instance variable. 18 | 19 | Input Format 20 | 21 | You are not responsible for reading any input from stdin. The locked Solution class in your editor reads in lines of input; the first line contains N, and the second line describes the elements array. 22 | 23 | Constraints 24 | 25 | 1 <= N <= 10 26 | 1 <= elements[i] <= 100, where 0 <= i <= N-1 27 | Output Format 28 | 29 | You are not responsible for printing any output; the Solution class will print the value of the instance variable. 30 | 31 | Sample Input 32 | 33 | 3 34 | 1 2 5 35 | 36 | Sample Output 37 | 38 | 4 39 | 40 | Explanation 41 | 42 | The scope of the array and integer is the entire class instance. The class constructor saves the argument passed to the constructor as the instance variable (where the computeDifference method can access it). 43 | 44 | To find the maximum difference, computeDifference checks each element in the array and finds the maximum difference between any elements: 45 | 46 | 47 | The maximum of these differences is , so it saves the value as the instance variable. The locked stub code in the editor then prints the value stored as , which is . -------------------------------------------------------------------------------- /29/case1: -------------------------------------------------------------------------------- 1 | 162 2 | 840 416 3 | 165 114 4 | 512 107 5 | 123 68 6 | 298 86 7 | 160 77 8 | 769 696 9 | 198 131 10 | 951 798 11 | 172 102 12 | 835 592 13 | 540 42 14 | 667 114 15 | 150 10 16 | 738 614 17 | 296 97 18 | 131 22 19 | 625 457 20 | 345 163 21 | 524 320 22 | 376 160 23 | 350 26 24 | 254 125 25 | 558 305 26 | 560 385 27 | 705 269 28 | 989 415 29 | 814 692 30 | 562 216 31 | 376 366 32 | 637 630 33 | 179 167 34 | 558 230 35 | 263 72 36 | 865 362 37 | 664 49 38 | 855 309 39 | 586 95 40 | 893 584 41 | 582 177 42 | 731 59 43 | 332 68 44 | 288 183 45 | 800 457 46 | 130 49 47 | 124 105 48 | 205 194 49 | 229 34 50 | 202 34 51 | 997 973 52 | 188 151 53 | 809 58 54 | 531 150 55 | 560 13 56 | 145 10 57 | 609 134 58 | 449 77 59 | 149 73 60 | 813 235 61 | 961 609 62 | 769 621 63 | 121 58 64 | 136 24 65 | 311 277 66 | 119 51 67 | 737 364 68 | 352 245 69 | 706 655 70 | 100 75 71 | 175 98 72 | 458 435 73 | 612 547 74 | 159 101 75 | 193 44 76 | 155 80 77 | 215 85 78 | 326 276 79 | 440 201 80 | 579 404 81 | 741 536 82 | 220 95 83 | 214 159 84 | 300 115 85 | 487 365 86 | 670 367 87 | 964 598 88 | 739 10 89 | 147 78 90 | 756 539 91 | 679 201 92 | 586 68 93 | 539 428 94 | 270 139 95 | 255 46 96 | 584 14 97 | 979 184 98 | 764 210 99 | 527 224 100 | 861 653 101 | 529 299 102 | 205 3 103 | 324 247 104 | 119 8 105 | 287 171 106 | 424 279 107 | 769 634 108 | 945 736 109 | 627 121 110 | 440 133 111 | 974 742 112 | 896 13 113 | 443 312 114 | 206 23 115 | 239 164 116 | 126 84 117 | 504 192 118 | 663 574 119 | 175 88 120 | 930 398 121 | 288 133 122 | 637 353 123 | 694 387 124 | 118 102 125 | 158 31 126 | 118 56 127 | 127 74 128 | 139 115 129 | 534 381 130 | 249 12 131 | 383 77 132 | 381 112 133 | 129 97 134 | 163 11 135 | 154 145 136 | 485 181 137 | 642 117 138 | 699 198 139 | 641 556 140 | 570 121 141 | 341 275 142 | 688 231 143 | 720 354 144 | 291 118 145 | 715 121 146 | 715 689 147 | 889 212 148 | 207 128 149 | 124 11 150 | 720 718 151 | 669 4 152 | 655 349 153 | 701 508 154 | 248 179 155 | 300 58 156 | 834 664 157 | 793 471 158 | 347 296 159 | 210 159 160 | 377 57 161 | 350 53 162 | 868 673 163 | 851 501 164 | -------------------------------------------------------------------------------- /20/README.md: -------------------------------------------------------------------------------- 1 | # Day 20: Sorting 2 | 3 | https://www.hackerrank.com/challenges/30-sorting 4 | 5 | Objective 6 | 7 | Today, we're discussing a simple sorting algorithm called Bubble Sort. Check out the Tutorial tab for learning materials and an instructional video! 8 | 9 | Consider the following version of Bubble Sort: 10 | 11 | for (int i = 0; i < n; i++) { 12 | // Track number of elements swapped during a single array traversal 13 | int numberOfSwaps = 0; 14 | 15 | for (int j = 0; j < n - 1; j++) { 16 | // Swap adjacent elements if they are in decreasing order 17 | if (a[j] > a[j + 1]) { 18 | swap(a[j], a[j + 1]); 19 | numberOfSwaps++; 20 | } 21 | } 22 | 23 | // If no elements were swapped during a traversal, array is sorted 24 | if (numberOfSwaps == 0) { 25 | break; 26 | } 27 | } 28 | 29 | Task 30 | 31 | Given an array, a, of size n containing distinct elements, sort array a in ascending order using the Bubble Sort algorithm above. Once sorted, print the following 3 lines: 32 | 33 | Array is sorted in numSwaps swaps 34 | where is the number of swaps that took place. 35 | 36 | First Element: firstElement 37 | where is the first element in the sorted array. 38 | 39 | Last Element: lastElement 40 | where is the last element in the sorted array. 41 | 42 | Hint: To complete this challenge, you will need to add a variable that keeps a running tally of all swaps that occur during execution. 43 | 44 | Input Format 45 | 46 | The first line contains an integer, n, denoting the number of elements in array a. 47 | The second line contains n space-separated integers describing a, where the ith integer is a[i]. 48 | 49 | Constraints 50 | 51 | 0 <= n <= 600 52 | 53 | Output Format 54 | 55 | There should be lines of output: 56 | 57 | 58 | where is the number of swaps that took place. 59 | 60 | where is the first element in the sorted array. 61 | 62 | where is the last element in the sorted array. 63 | 64 | Sample Input 0 65 | 66 | 3 67 | 1 2 3 68 | 69 | Sample Output 0 70 | 71 | Array is sorted in 0 swaps. 72 | First Element: 1 73 | Last Element: 3 74 | Sample Input 1 75 | 76 | 3 77 | 3 2 1 78 | Sample Output 1 79 | 80 | Array is sorted in 3 swaps. 81 | First Element: 1 82 | Last Element: 3 -------------------------------------------------------------------------------- /8/README.md: -------------------------------------------------------------------------------- 1 | #Day 8: Dictionaries and Maps 2 | 3 | https://www.hackerrank.com/challenges/30-dictionaries-and-maps 4 | 5 | Objective 6 | 7 | Today, we're learning about Key-Value pair mappings using a Map or 8 | Dictionary data structure. Check out the Tutorial tab for learning 9 | materials and an instructional video! 10 | 11 | Task 12 | 13 | Given names and phone numbers, assemble a phone book that maps 14 | friends' names to their respective phone numbers. You will then be 15 | given an unknown number of names to query your phone book for; for 16 | each name queried, print the associated entry from your phone book (in the 17 | form name=phonenumber) or notfound if there is no entry for name. 18 | 19 | Note: Your phone book should be a Dictionary/Map/HashMap data structure. 20 | 21 | Input Format 22 | 23 | The first line contains an integer, n , denoting the number of entries 24 | in the phone book. Each of the subsequent lines describes an entry in 25 | the form of 2 space-separated values on a single line. The first value 26 | is a friend's name, and the second value is an 8-digit phone number. 27 | 28 | After the lines of phone book entries, there are an unknown number of 29 | lines of queries. Each line (query) contains a to look up, and you 30 | must continue reading lines until there is no more input. 31 | 32 | Note: Names consist of lowercase English letters and are first names 33 | only. 34 | 35 | Constraints 36 | 37 | Output Format 38 | 39 | On a new line for each query, print not found if the name has no corresponding 40 | entry in the phone book; otherwise, print the full name and phonenumber in the format name=phonenumber. 41 | 42 | Sample Input 43 | 44 | 3 45 | sam 99912222 46 | tom 11122222 47 | harry 12299933 48 | sam 49 | edward 50 | harry 51 | Sample Output 52 | 53 | sam=99912222 54 | Not found 55 | harry=12299933 56 | 57 | Explanation 58 | N = 3 59 | 60 | We add the n subsequent (Key,Value) pairs to our map so it looks like 61 | this: 62 | 63 | phonebook = {(sam, 99912222),(tom,11122222),(harry, 12299933)} 64 | 65 | We then process each query and print key=value if the queried Key is found in 66 | the map, or notfound otherwise. 67 | 68 | Query 0: sam 69 | 70 | Sam is one of the keys in our dictionary, so we print sam=9991222 71 | 72 | Query 1: edward 73 | 74 | Edward is not one of the keys in our dictionary, so we print not found 75 | 76 | Query 2: harry 77 | 78 | Harry is one of the keys in our dictionary, so we print harry=12299933 79 | --------------------------------------------------------------------------------