From 592dc1c71b1b4f5e85518f75c764b9968a314868 Mon Sep 17 00:00:00 2001 From: rshaghoulian <9rodney@gmail.com> Date: Sat, 4 Feb 2017 02:41:59 -0800 Subject: [PATCH] Refactored solutions --- .../Warmup/Solve Me First/Solution.java | 33 +++++++------- Java/Introduction/Java If-Else/Solution.java | 40 +++++++++-------- .../Java Stdin and Stdout I/Solution.java | 8 ++-- .../Java Stdin and Stdout II/Solution.java | 10 +++-- .../Welcome to Java!/Solution.java | 2 - README.md | 2 +- .../Solution.java | 43 +++++++++---------- .../Day 0 - Weighted Mean/Solution.java | 12 +++--- .../Day 1 - Quartiles/Solution.java | 17 ++++---- .../Day 0 - Hello, World/Solution.java | 3 +- .../Day 1 - Data Types/Solution.java | 2 +- .../Day 2 - Operators/Solution.java | 17 ++++---- .../Sorting - Bubble Sort/Solution.java | 13 +++--- 13 files changed, 100 insertions(+), 102 deletions(-) diff --git a/Algorithms/Warmup/Solve Me First/Solution.java b/Algorithms/Warmup/Solve Me First/Solution.java index 7a81145..e774330 100644 --- a/Algorithms/Warmup/Solve Me First/Solution.java +++ b/Algorithms/Warmup/Solve Me First/Solution.java @@ -1,25 +1,22 @@ -import java.io.*; -import java.util.*; -import java.text.*; -import java.math.*; -import java.util.regex.*; +import java.util.Scanner; public class Solution { static int solveMeFirst(int a, int b) { - // Hint: Type return a+b; below - return a + b; - } - + return a + b; + } - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - int a; - a = in.nextInt(); - int b; - b = in.nextInt(); - int sum; - sum = solveMeFirst(a, b); + public static void main(String[] args) { + /* Read input */ + Scanner scan = new Scanner(System.in); + int a = scan.nextInt(); + int b = scan.nextInt(); + scan.close(); + + /* Calculate sum */ + int sum = solveMeFirst(a, b); + + /* Print output */ System.out.println(sum); - } + } } diff --git a/Java/Introduction/Java If-Else/Solution.java b/Java/Introduction/Java If-Else/Solution.java index a22ea10..d17a2e1 100644 --- a/Java/Introduction/Java If-Else/Solution.java +++ b/Java/Introduction/Java If-Else/Solution.java @@ -1,23 +1,25 @@ - import java.io.*; - import java.util.*; - import java.text.*; - import java.math.*; - import java.util.regex.*; +import java.util.Scanner; - public class Solution { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - String ans = ""; - if(n % 2 == 1) { - ans = "Weird"; +public class Solution { + public static void main(String[] args) { + /* Read input */ + Scanner scan = new Scanner(System.in); + int n = scan.nextInt(); + scan.close(); + + /* Determine answer */ + String ans = ""; + if (n % 2 == 1) { + ans = "Weird"; + } else { + if (n >= 6 && n <= 20) { + ans = "Weird"; + } else { + ans = "Not Weird"; } - else { - if (n >= 6 && n <= 20) - ans = "Weird"; - else - ans = "Not Weird"; - } - System.out.println(ans); } + + /* Print output */ + System.out.println(ans); } +} diff --git a/Java/Introduction/Java Stdin and Stdout I/Solution.java b/Java/Introduction/Java Stdin and Stdout I/Solution.java index 76d1697..5ac0eeb 100644 --- a/Java/Introduction/Java Stdin and Stdout I/Solution.java +++ b/Java/Introduction/Java Stdin and Stdout I/Solution.java @@ -1,13 +1,15 @@ -import java.util.*; +import java.util.Scanner; public class Solution { - public static void main(String[] args) { + /* Read and save input */ Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); - + scan.close(); + + /* Print data */ System.out.println(a); System.out.println(b); System.out.println(c); diff --git a/Java/Introduction/Java Stdin and Stdout II/Solution.java b/Java/Introduction/Java Stdin and Stdout II/Solution.java index 3abefeb..ad9bc7b 100644 --- a/Java/Introduction/Java Stdin and Stdout II/Solution.java +++ b/Java/Introduction/Java Stdin and Stdout II/Solution.java @@ -1,14 +1,16 @@ import java.util.Scanner; public class Solution { - public static void main(String[] args) { + /* Read input */ Scanner scan = new Scanner(System.in); - int i = scan.nextInt(); + int i = scan.nextInt(); double d = scan.nextDouble(); - scan.nextLine(); // gets rid of the pesky newline + scan.nextLine(); // gets rid of the pesky newline String s = scan.nextLine(); - + scan.close(); + + /* Print output */ System.out.println("String: " + s); System.out.println("Double: " + d); System.out.println("Int: " + i); diff --git a/Java/Introduction/Welcome to Java!/Solution.java b/Java/Introduction/Welcome to Java!/Solution.java index 93c7115..f07c34f 100644 --- a/Java/Introduction/Welcome to Java!/Solution.java +++ b/Java/Introduction/Welcome to Java!/Solution.java @@ -1,7 +1,5 @@ public class Solution { - public static void main(String[] args) { - /* Enter your code here. Print output to STDOUT. Your class should be named Solution. */ System.out.println("Hello, World."); System.out.println("Hello, Java."); } diff --git a/README.md b/README.md index 20cb752..7830a74 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # HackerRank-solutions -My Java solutions to HackerRank challenges +My Java solutions to [HackerRank](https://www.hackerrank.com/) challenges ## Java diff --git a/Tutorials/10 Days of Statistics/Day 0 - Mean, Median, and Mode/Solution.java b/Tutorials/10 Days of Statistics/Day 0 - Mean, Median, and Mode/Solution.java index 2515f88..530a8cf 100644 --- a/Tutorials/10 Days of Statistics/Day 0 - Mean, Median, and Mode/Solution.java +++ b/Tutorials/10 Days of Statistics/Day 0 - Mean, Median, and Mode/Solution.java @@ -1,44 +1,41 @@ -import java.io.*; -import java.util.*; +import java.util.Scanner; +import java.util.Arrays; +import java.util.HashMap; +/* Runtime: O(n log n) due to sorting */ public class Solution { public static void main(String[] args) { - /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ - - /* Runtime: I solve it in O(n) using 2 passes of the data. I could optionally combine it into - 1 pass but then code will be less clear */ - - Scanner scan = new Scanner(System.in); + /* Read input: Create and fill array */ + Scanner scan = new Scanner(System.in); int size = scan.nextInt(); - scan.nextLine(); // gets rid of newline - /* Create and fill array */ int [] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = scan.nextInt(); } + scan.close(); + + /* Sort array: O(n log n) runtime */ Arrays.sort(array); - /* Mean */ + /* Calculate Mean */ int total = 0; for (int num : array) { total += num; } double mean = (double) total / size; - /* Median */ + /* Calculate Median */ double median; if (size % 2 == 0) { - median = (array[size/2 - 1] + array[size/2]) / 2.0; - } - else { - median = array[size/2]; + median = (array[size / 2 - 1] + array[size / 2]) / 2.0; + } else { + median = array[size / 2]; } - /* Mode */ - + /* Calculate Mode */ HashMap map = new HashMap<>(); int maxOccurrences = 0; int mode = Integer.MAX_VALUE; @@ -46,18 +43,18 @@ public class Solution { for (int num : array) { /* Update occurrences for this number */ int occurrences; - if (map.containsKey(num)) + if (map.containsKey(num)) { occurrences = map.get(num) + 1; - else + } else { occurrences = 1; + } map.put(num, occurrences); /* Update our mode. If there's a tie, choose the smaller number */ - if (occurrences > maxOccurrences){ + if (occurrences > maxOccurrences) { maxOccurrences = occurrences; mode = num; - } - else if (occurrences == maxOccurrences && num < mode){ + } else if (occurrences == maxOccurrences && num < mode) { // this does the tie-breaking maxOccurrences = occurrences; mode = num; } diff --git a/Tutorials/10 Days of Statistics/Day 0 - Weighted Mean/Solution.java b/Tutorials/10 Days of Statistics/Day 0 - Weighted Mean/Solution.java index d8e9cf8..007e24c 100644 --- a/Tutorials/10 Days of Statistics/Day 0 - Weighted Mean/Solution.java +++ b/Tutorials/10 Days of Statistics/Day 0 - Weighted Mean/Solution.java @@ -1,11 +1,9 @@ -import java.io.*; -import java.util.*; +import java.util.Scanner; public class Solution { public static void main(String[] args) { - /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ - + /* Read and save data */ Scanner scan = new Scanner(System.in); int size = scan.nextInt(); @@ -14,15 +12,15 @@ public class Solution { for (int i = 0; i < size; i++) { elements[i] = scan.nextInt(); } - scan.nextLine(); // just to be safe, gets rid of the newline. for (int i = 0; i < size; i++) { weights[i] = scan.nextInt(); - } + } + scan.close(); /* Calculate weighted mean */ int total = 0; int totalWeights = 0; - for (int i = 0; i < size; i++){ + for (int i = 0; i < size; i++) { total += elements[i] * weights[i]; totalWeights += weights[i]; } diff --git a/Tutorials/10 Days of Statistics/Day 1 - Quartiles/Solution.java b/Tutorials/10 Days of Statistics/Day 1 - Quartiles/Solution.java index 1f29ec0..5db3029 100644 --- a/Tutorials/10 Days of Statistics/Day 1 - Quartiles/Solution.java +++ b/Tutorials/10 Days of Statistics/Day 1 - Quartiles/Solution.java @@ -1,34 +1,35 @@ -import java.io.*; -import java.util.*; +import java.util.Scanner; +import java.util.Arrays; public class Solution { - + public static void main(String[] args) { - int [] array = getValues(); Arrays.sort(array); /* Works with both even and odd length arrays */ int q1 = findMedian(array, 0, array.length / 2 - 1); int q2 = findMedian(array, 0, array.length - 1); - int q3 = findMedian(array, (array.length + 1)/ 2, array.length - 1); + int q3 = findMedian(array, (array.length + 1) / 2, array.length - 1); + /* Print output */ System.out.println(q1); System.out.println(q2); System.out.println(q3); } - + + /* Treats elements from "start" to "end" as an array and calculates its median */ private static int findMedian(int [] array, int start, int end) { if ((end - start) % 2 == 0) { // odd number of elements return (array[(end + start) / 2]); - } - else { // even number of elements + } else { // even number of elements int value1 = array[(end + start) / 2]; int value2 = array[(end + start) / 2 + 1]; return Math.round((value1 + value2) / 2); } } + /* Creates array from input */ private static int [] getValues() { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); diff --git a/Tutorials/30 Days of Code/Day 0 - Hello, World/Solution.java b/Tutorials/30 Days of Code/Day 0 - Hello, World/Solution.java index e177fa6..20b0f2e 100644 --- a/Tutorials/30 Days of Code/Day 0 - Hello, World/Solution.java +++ b/Tutorials/30 Days of Code/Day 0 - Hello, World/Solution.java @@ -1,4 +1,3 @@ - public class Solution { public static void main(String[] args) { // Create a Scanner object to read input from stdin. @@ -14,7 +13,7 @@ public class Solution { // Print a string literal saying "Hello, World." to stdout. System.out.println("Hello, World."); - // TODO: Write a line of code here that prints the contents of inputString to stdout. + // Prints the contents of inputString to stdout. System.out.println(inputString); } } diff --git a/Tutorials/30 Days of Code/Day 1 - Data Types/Solution.java b/Tutorials/30 Days of Code/Day 1 - Data Types/Solution.java index e172ee2..1761e2f 100644 --- a/Tutorials/30 Days of Code/Day 1 - Data Types/Solution.java +++ b/Tutorials/30 Days of Code/Day 1 - Data Types/Solution.java @@ -22,7 +22,7 @@ public class Solution { // Note: If you have trouble reading the entire String, please go back and review the Tutorial closely. myInt = scan.nextInt(); myDouble = scan.nextDouble(); - scan.nextLine(); // Rodney: gets rid of newline at end of line 2 + scan.nextLine(); // gets rid of newline at end of line 2 of input myString = scan.nextLine(); /* Print the sum of both integer variables on a new line. */ diff --git a/Tutorials/30 Days of Code/Day 2 - Operators/Solution.java b/Tutorials/30 Days of Code/Day 2 - Operators/Solution.java index e0d5a3e..91260e9 100644 --- a/Tutorials/30 Days of Code/Day 2 - Operators/Solution.java +++ b/Tutorials/30 Days of Code/Day 2 - Operators/Solution.java @@ -1,22 +1,21 @@ -import java.util.*; +import java.util.Scanner; public class Arithmetic { public static void main(String[] args) { + /* Read input */ Scanner scan = new Scanner(System.in); double mealCost = scan.nextDouble(); // original meal price - int tipPercent = scan.nextInt(); // tip percentage - int taxPercent = scan.nextInt(); // tax percentage + int tipPercent = scan.nextInt(); // tip percentage + int taxPercent = scan.nextInt(); // tax percentage scan.close(); - - // Write your calculation code here. - - // cast the result of the rounding operation to an int and save it as totalCost + + /* Calculate total cost */ int totalCost = (int) Math.round(mealCost + mealCost * tipPercent / 100.0 + mealCost * taxPercent / 100.0); - - // Print your result + + /* Print output */ System.out.println("The total meal cost is " + totalCost + " dollars."); } } diff --git a/Tutorials/Cracking the Coding Interview/Algorithms/Sorting - Bubble Sort/Solution.java b/Tutorials/Cracking the Coding Interview/Algorithms/Sorting - Bubble Sort/Solution.java index 0c17ff5..5d71135 100644 --- a/Tutorials/Cracking the Coding Interview/Algorithms/Sorting - Bubble Sort/Solution.java +++ b/Tutorials/Cracking the Coding Interview/Algorithms/Sorting - Bubble Sort/Solution.java @@ -1,23 +1,25 @@ -import java.io.*; -import java.util.*; +import java.util.Scanner; public class Solution { private static int swaps = 0; public static void main(String[] args) { - /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ + /* Read input */ Scanner scan = new Scanner(System.in); int size = scan.nextInt(); int [] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = scan.nextInt(); } + scan.close(); + /* Sort array */ bubbleSort(array); + + /* Print output */ System.out.println("Array is sorted in " + swaps + " swaps."); System.out.println("First Element: " + array[0]); - System.out.println("Last Element: " + array[array.length-1]); - + System.out.println("Last Element: " + array[array.length - 1]); } private static void bubbleSort(int [] array) { @@ -38,6 +40,7 @@ public class Solution { } } + /* Standard swap. Also updates our "swaps" variable */ private static void swap(int [] array, int i, int j) { int temp = array[i]; array[i] = array[j];