From af8ca70939e362bd5af308166f328694c73af1c1 Mon Sep 17 00:00:00 2001 From: Jesse Furlan Date: Mon, 9 Jan 2017 16:22:24 -0800 Subject: [PATCH 1/5] [Java] Challenges 0-7 --- challenge_0/java/jdfurlan/README.md | 17 ++++++ challenge_0/java/jdfurlan/src/HelloWorld.java | 12 +++++ challenge_1/java/jdfurlan/README.md | 20 +++++++ .../java/jdfurlan/src/ReverseAString.java | 18 +++++++ challenge_2/java/jdfurlan/README.md | 22 ++++++++ .../java/jdfurlan/src/SingleNumber.java | 25 +++++++++ challenge_3/java/jdfurlan/README.md | 22 ++++++++ .../java/jdfurlan/src/MajorityElement.java | 24 +++++++++ challenge_4/java/jdfurlan/README.md | 24 +++++++++ .../java/jdfurlan/src/InvertBinaryTree.java | 54 +++++++++++++++++++ challenge_4/java/jdfurlan/src/Node.java | 11 ++++ challenge_5/java/jdfurlan/README.md | 23 ++++++++ .../java/jdfurlan/src/FindTheDifference.java | 21 ++++++++ challenge_6/java/jdfurlan/README.md | 26 +++++++++ challenge_6/java/jdfurlan/src/Ranges.java | 32 +++++++++++ challenge_7/java/jdfurlan/README.md | 22 ++++++++ .../jdfurlan/src/FindTheMissingNumber.java | 21 ++++++++ 17 files changed, 394 insertions(+) create mode 100644 challenge_0/java/jdfurlan/README.md create mode 100644 challenge_0/java/jdfurlan/src/HelloWorld.java create mode 100644 challenge_1/java/jdfurlan/README.md create mode 100644 challenge_1/java/jdfurlan/src/ReverseAString.java create mode 100644 challenge_2/java/jdfurlan/README.md create mode 100644 challenge_2/java/jdfurlan/src/SingleNumber.java create mode 100644 challenge_3/java/jdfurlan/README.md create mode 100644 challenge_3/java/jdfurlan/src/MajorityElement.java create mode 100644 challenge_4/java/jdfurlan/README.md create mode 100644 challenge_4/java/jdfurlan/src/InvertBinaryTree.java create mode 100644 challenge_4/java/jdfurlan/src/Node.java create mode 100644 challenge_5/java/jdfurlan/README.md create mode 100644 challenge_5/java/jdfurlan/src/FindTheDifference.java create mode 100644 challenge_6/java/jdfurlan/README.md create mode 100644 challenge_6/java/jdfurlan/src/Ranges.java create mode 100644 challenge_7/java/jdfurlan/README.md create mode 100644 challenge_7/java/jdfurlan/src/FindTheMissingNumber.java diff --git a/challenge_0/java/jdfurlan/README.md b/challenge_0/java/jdfurlan/README.md new file mode 100644 index 000000000..6c0a36a6e --- /dev/null +++ b/challenge_0/java/jdfurlan/README.md @@ -0,0 +1,17 @@ +# HelloWorld +###### Java 8 + +### 1. Approach to Solving the problem + +Make it unnecessarily slow! + +### 2. How to compile and run this code + +``` +javac HelloWorld.java +java HelloWorld +``` + +### 3. How this program works + +Simply run it and you'll see "Hello, World!" diff --git a/challenge_0/java/jdfurlan/src/HelloWorld.java b/challenge_0/java/jdfurlan/src/HelloWorld.java new file mode 100644 index 000000000..d92338fb9 --- /dev/null +++ b/challenge_0/java/jdfurlan/src/HelloWorld.java @@ -0,0 +1,12 @@ + +public class HelloWorld { + + public static void main(String[] args) { + char[] hw = "Hello, World!".toCharArray(); + for (char c : hw) { + System.out.print(c); + } + + } + +} diff --git a/challenge_1/java/jdfurlan/README.md b/challenge_1/java/jdfurlan/README.md new file mode 100644 index 000000000..384f780e7 --- /dev/null +++ b/challenge_1/java/jdfurlan/README.md @@ -0,0 +1,20 @@ +# ReverseAString +###### Java 8 + +### 1. Approach to Solving the problem + +Don't use built-in methods, just primitives and arrays + +### 2. How to compile and run this code + +``` +javac ReverseAString.java +java ReverseAString +``` + +### 3. How this program works + +Takes user input and converts to a character array +Track first and last values, store one in a temp variable, then swap +increment and decrement the positions and continue to swap. +For odd length strings it just leaves the middle value in place, since no swap needed diff --git a/challenge_1/java/jdfurlan/src/ReverseAString.java b/challenge_1/java/jdfurlan/src/ReverseAString.java new file mode 100644 index 000000000..37d31dc55 --- /dev/null +++ b/challenge_1/java/jdfurlan/src/ReverseAString.java @@ -0,0 +1,18 @@ + +import java.util.Scanner; + +public class ReverseAString { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char[] input = sc.next().toCharArray(); + int j = input.length - 1; + for (int i = 0; i < input.length / 2; i++, j--) { + char temp = input[j]; + input[j] = input[i]; + input[i] = temp; + } + System.out.println(String.valueOf(input)); + sc.close(); + } +} diff --git a/challenge_2/java/jdfurlan/README.md b/challenge_2/java/jdfurlan/README.md new file mode 100644 index 000000000..99773a1f2 --- /dev/null +++ b/challenge_2/java/jdfurlan/README.md @@ -0,0 +1,22 @@ +# SingleNumber +###### Java 8 + +### 1. Approach to Solving the problem + +Maps are usually good for storing unique values since there +can only be 1 of any given key in a map. A set is also good for this +but since we needed to track the occurence of a key, I decided to use map +and track occurance with the value + +### 2. How to compile and run this code + +``` +javac SingleNumber.java +java SingleNumber +``` + +### 3. How this program works + +If a key doesn't already exist in the map, put it in and set its value to 1 +Otherwise, key the value with associated key and increment by 1. +Next, interate through the keySet and check when the value == 1, then return and exit diff --git a/challenge_2/java/jdfurlan/src/SingleNumber.java b/challenge_2/java/jdfurlan/src/SingleNumber.java new file mode 100644 index 000000000..323f68153 --- /dev/null +++ b/challenge_2/java/jdfurlan/src/SingleNumber.java @@ -0,0 +1,25 @@ + +import java.util.HashMap; + +public class SingleNumber { + + public static void main(String[] args) { + String[] arr = { "2", "a", "l", "3", "l", "4", "k", "2", "3", "4", "a", "6", "c", "4", "m", "6", "m", "k", "9", + "10", "9", "8", "7", "8", "10", "7" }; + HashMap map = new HashMap(); + for (String s : arr) { + if (map.containsKey(s)) { + map.put(s, map.get(s) + 1); + } else { + map.put(s, 1); + } + } + for (String s : map.keySet()) { + if (map.get(s) == 1) { + System.out.println(s); + System.exit(0); + } + } + } + +} diff --git a/challenge_3/java/jdfurlan/README.md b/challenge_3/java/jdfurlan/README.md new file mode 100644 index 000000000..ca7393b8a --- /dev/null +++ b/challenge_3/java/jdfurlan/README.md @@ -0,0 +1,22 @@ +# InvertBinarTree +###### Java 8 + +### 1. Approach to Solving the problem + +Trees suck if you haven't messed with them in a while! +Swapping the values is relatively easy using an A-B-C swap approach. +The approach to print was to do a [Breadth-First Traversal](https://www.cs.bu.edu/teaching/c/tree/breadth-first/) + +### 2. How to compile and run this code + +Compile the Node class first + +``` +javac InvertBinarTree.java +java InvertBinarTree +``` + +### 3. How this program works + +Iterate through the array, if the key is new add it and frequency of 1 +Otherwise increment its frequency. After incrementing, check if that frequency is > n/2 diff --git a/challenge_3/java/jdfurlan/src/MajorityElement.java b/challenge_3/java/jdfurlan/src/MajorityElement.java new file mode 100644 index 000000000..d34e1b634 --- /dev/null +++ b/challenge_3/java/jdfurlan/src/MajorityElement.java @@ -0,0 +1,24 @@ + +import java.util.HashMap; + +public class MajorityElement { + + public static void main(String[] args) { + int[] nums = { 2, 2, 3, 7, 5, 7, 7, 7, 4, 7, 2, 7, 4, 5, 6, 7, 7, 8, 6, 7, 7, 8, 10, 12, 29, 30, 19, 10, 7, 7, + 7, 7, 7, 7, 7, 7, 7 }; + HashMap map = new HashMap(); + for (int n : nums) { + if (map.containsKey(n)) { + map.put(n, map.get(n) + 1); + if (map.get(n) > (nums.length / 2)) { + System.out.println(n); + System.exit(0); + } + } else { + map.put(n, 1); + } + } + + } + +} diff --git a/challenge_4/java/jdfurlan/README.md b/challenge_4/java/jdfurlan/README.md new file mode 100644 index 000000000..850adc4ce --- /dev/null +++ b/challenge_4/java/jdfurlan/README.md @@ -0,0 +1,24 @@ +# InvertBinarTree +###### Java 8 + +### 1. Approach to Solving the problem + +Trees suck if you haven't messed with them in a while! +Swapping the values is relatively easy using an A-B-C swap approach. +The approach to print was to do a [Breadth-First Traversal](https://www.cs.bu.edu/teaching/c/tree/breadth-first/) + +### 2. How to compile and run this code + + +``` +javac InvertBinarTree.java +java InvertBinarTree +``` + +### 3. How this program works + +Hand the root node to the swap function. There, while the node is not null +you must take either child and store in a temp, then swap into the other child's place +then swap the child you didn't store into a temp with the temp child. +Call the swap function recursively with either child first, then the other child. +BFT through the tree to print values by level. diff --git a/challenge_4/java/jdfurlan/src/InvertBinaryTree.java b/challenge_4/java/jdfurlan/src/InvertBinaryTree.java new file mode 100644 index 000000000..4d23cd1f2 --- /dev/null +++ b/challenge_4/java/jdfurlan/src/InvertBinaryTree.java @@ -0,0 +1,54 @@ + +import java.util.LinkedList; +import java.util.Queue; + +public class InvertBinaryTree { + + public static void main(String[] args) { + Node root = new Node(4); + Node two = new Node(2); + Node seven = new Node(7); + Node three = new Node(3); + Node six = new Node(6); + Node nine = new Node(9); + Node one = new Node(1); + + root.left = two; + root.right = seven; + two.left = one; + two.right = three; + seven.left = six; + seven.right = nine; + + print(root); + invertBT(root); + System.out.println(); + print(root); + + + } + //breadth-first traversal using Queue interface + private static void print(Node root) { + Queue children = new LinkedList(); + children.add(root); + while (!children.isEmpty()) { + Node temp = children.poll(); + System.out.print(temp.data); + if (temp.left != null) + children.add(temp.left); + if (temp.right != null) + children.add(temp.right); + } + } + //basic swap. Once swapped, swap left subtree then right subtree until null + private static void invertBT(Node root) { + if (root == null) + return; + Node temp = root.right; + root.right = root.left; + root.left = temp; + invertBT(root.left); + invertBT(root.right); + } + +} diff --git a/challenge_4/java/jdfurlan/src/Node.java b/challenge_4/java/jdfurlan/src/Node.java new file mode 100644 index 000000000..7f0e653e5 --- /dev/null +++ b/challenge_4/java/jdfurlan/src/Node.java @@ -0,0 +1,11 @@ + +public class Node { + int data; + Node left; + Node right; + public Node(int data){ + this.data = data; + left = null; + right = null; + } +} diff --git a/challenge_5/java/jdfurlan/README.md b/challenge_5/java/jdfurlan/README.md new file mode 100644 index 000000000..e33b2293d --- /dev/null +++ b/challenge_5/java/jdfurlan/README.md @@ -0,0 +1,23 @@ +# Ranges +###### Java 8 + +### 1. Approach to Solving the problem + +I thought about various ways to compare values in a string. +Using a boolean array is a common solution for comparing characters. + +### 2. How to compile and run this code + + +``` +javac Ranges.java +java Ranges +``` + +### 3. How this program works + +Create a boolean array of size 128, the ASCII code limit +(this program wouldn't work for Unicode, too many chars) +Every char value maps to an index in the array, walk through string +s and put each boolean at the char index to true. Then walk through string +t and as soon as you reach a boolean that is false, we know that's our missing char! diff --git a/challenge_5/java/jdfurlan/src/FindTheDifference.java b/challenge_5/java/jdfurlan/src/FindTheDifference.java new file mode 100644 index 000000000..71ae89b77 --- /dev/null +++ b/challenge_5/java/jdfurlan/src/FindTheDifference.java @@ -0,0 +1,21 @@ + +public class FindTheDifference { + + public static void main(String[] args) { + String s = "abcd"; + String t = "dbeca"; + + boolean[] b = new boolean[128]; + for (int i = 0; i < s.length(); i++) { + b[s.charAt(i)] = true; + } + for (int i = 0; i < t.length(); i++) { + if (!b[t.charAt(i)]) { + System.out.println(t.charAt(i) + " found at index: " + i); + System.exit(0); + } + } + + } + +} diff --git a/challenge_6/java/jdfurlan/README.md b/challenge_6/java/jdfurlan/README.md new file mode 100644 index 000000000..8c878bd7f --- /dev/null +++ b/challenge_6/java/jdfurlan/README.md @@ -0,0 +1,26 @@ +# Ranges +###### Java 8 + +### 1. Approach to Solving the problem + +I very much didn't want to use any additional data structures to help +with traversing the array, and wanted to keep it as is. The reason an +array list is used is simply because I needed a dynamicly sized array +since there's no way to know beforehand how many ranges we will find. + +### 2. How to compile and run this code + +``` +javac Ranges.java +java Ranges +``` + +### 3. How this program works + +Assuming we have at least 2 values, we initialize the first range point +as the first value, and the last to 0. We then check that the next value is +equal to the first + 1, meaning incremental. If true, keep incrementing and +setting the last range point to the current value. To handle index out of bounds +I just break out of the loop when i == the length of the array - 1. The final check +is if last = 0, we know it was never updated and we have array of size 2 but the second +value isn't incremental from the first, so return empty list. diff --git a/challenge_6/java/jdfurlan/src/Ranges.java b/challenge_6/java/jdfurlan/src/Ranges.java new file mode 100644 index 000000000..762ab2c93 --- /dev/null +++ b/challenge_6/java/jdfurlan/src/Ranges.java @@ -0,0 +1,32 @@ + +import java.util.ArrayList; + +public class Ranges { + + public static void main(String[] args) { + int[][] arr = { { 1, 2, 3, 4, 8, 9, 10, 12, 13, 14 }, { 1, 2, 3, 4, 9, 10, 15 }, { 1, 2 }, { 1 }, + { 1, 2, 3, 4, 5, 8, 9, 10}, { 1, 3 }}; + for(int[] a : arr){ + System.out.println(findRanges(a)); + } + } + + private static ArrayList findRanges(int[] arr) { + ArrayList res = new ArrayList(); + int first, last; + for (int i = 0; i < arr.length - 1; i++) { + first = arr[i]; + last = 0; + while (arr[i + 1] == arr[i] + 1) { + i++; + last = arr[i]; + if (i == arr.length - 1) + break; + } + if (last != 0) + res.add(first + "->" + last); + } + return res; + } + +} diff --git a/challenge_7/java/jdfurlan/README.md b/challenge_7/java/jdfurlan/README.md new file mode 100644 index 000000000..6ac223d70 --- /dev/null +++ b/challenge_7/java/jdfurlan/README.md @@ -0,0 +1,22 @@ +# Find the Missing Number +###### Java 8 + +### 1. Approach to Solving the problem + +I kept thinking about using different data structures which really annoying me! +I then though about the relationship between what the actual array would look like +without the missig number, and that's when I realized the only thing separating them +would be the sum of their values! + +### 2. How to compile and run this code + +``` +javac FindTheMissingNumber.java +java FindTheMissingNumber +``` + +### 3. How this program works + +Use "i" to track the incremental sum as if no values were missing. Then use +the same "i" to sum the index values of the array given. Compare the differences +and that's the missing number! \ No newline at end of file diff --git a/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java b/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java new file mode 100644 index 000000000..43ae36e73 --- /dev/null +++ b/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java @@ -0,0 +1,21 @@ + +public class FindTheMissingNumber { + + public static void main(String[] args) { + int[] arr = { 6, 3, 4, 8, 1, 2, 0, 9, 10, 7 }; // missing is 5 + System.out.println(findMissingNum(arr)); + } + + private static int findMissingNum(int[] arr) { + int actualSum = 0; + int thisSum = 0; + int i; + for (i = 0; i < arr.length; i++) { + actualSum += i; + thisSum += arr[i]; + } + actualSum += i; + return actualSum - thisSum; + } + +} From 0d4999cd4b3513210cf8f1c19d04f10f1b6db3d0 Mon Sep 17 00:00:00 2001 From: Jesse Furlan Date: Mon, 9 Jan 2017 20:28:30 -0800 Subject: [PATCH 2/5] [Java] Challenge_9 --- challenge_9/java/jdfurlan/README.md | 27 ++++++++++++ challenge_9/java/jdfurlan/src/Squares.java | 50 ++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 challenge_9/java/jdfurlan/README.md create mode 100644 challenge_9/java/jdfurlan/src/Squares.java diff --git a/challenge_9/java/jdfurlan/README.md b/challenge_9/java/jdfurlan/README.md new file mode 100644 index 000000000..bfdac7e5e --- /dev/null +++ b/challenge_9/java/jdfurlan/README.md @@ -0,0 +1,27 @@ +# Squares +###### Java 8 + +### 1. Approach to Solving the problem + +I struggled with this problem for a while. Originally I made a slow solution +that basically traversed through the array until the number was > than current and placed it there. +This was slow and bad. slack member **zmiller91**'s solution is what I implemented, but only after I fully +understood his approach. It was very clean and elegant and I learned a lot from it. + +### 2. How to compile and run this code + +``` +javac Squares.java +java Squares +``` + +### 3. How this program works + +Add all negatives squared to position 0 of the negatives list (this is bad I believe +as it results in every value shifting at every insertion) and add all positives to their own list. +Then, start counting through both lists from 0, and while at least one list still has values (index < length) +continue to get the value and add it to the result after comparing it with the negative value at that index. +The way index out of bounds is avoided is by constantly checking if the indexs are < length of their +respective list. If it ever exceeds the length, set that value to MAX_VALUE so it will always be greater than the +positive or negative value it was compared with, essentially nullifying that index. Values are touched only once +for O(n); \ No newline at end of file diff --git a/challenge_9/java/jdfurlan/src/Squares.java b/challenge_9/java/jdfurlan/src/Squares.java new file mode 100644 index 000000000..89d6dc64c --- /dev/null +++ b/challenge_9/java/jdfurlan/src/Squares.java @@ -0,0 +1,50 @@ + +import java.util.ArrayList; + +public class Squares { + + public static void main(String[] args) { + int[] arr = {-9,4,9}; + + ArrayList myList = orderSquares(arr); + for (int i : myList) { + System.out.print(i + " "); + } + + } + + private static ArrayList orderSquares(int[] arr) { + ArrayList result = new ArrayList(); + ArrayList negatives = new ArrayList(); + ArrayList positives = new ArrayList(); + + for (int i : arr) { + if (i < 0) { + negatives.add(0, i * i); + } else { + positives.add(i * i); + } + } + int posIdx = 0; + int negIdx = 0; + + while (posIdx < positives.size() || negIdx < negatives.size()) { + + int p = (posIdx < positives.size()) ? positives.get(posIdx) : Integer.MAX_VALUE; + int n = (negIdx < negatives.size()) ? negatives.get(negIdx) : Integer.MAX_VALUE; + + if (p < n) { + result.add(p); + posIdx++; + + } else { + result.add(n); + negIdx++; + + } + } + + return result; + } + +} From b8cde7575f52d1f2f7ab3b26b174aebfc0ea2ba1 Mon Sep 17 00:00:00 2001 From: Jesse Furlan Date: Tue, 10 Jan 2017 11:12:38 -0800 Subject: [PATCH 3/5] Made code corrections after feedback --- challenge_3/java/jdfurlan/README.md | 16 ++++++++-------- .../java/jdfurlan/src/MajorityElement.java | 5 +++-- challenge_4/java/jdfurlan/README.md | 6 +++--- challenge_6/java/jdfurlan/src/Ranges.java | 4 +--- .../java/jdfurlan/src/FindTheMissingNumber.java | 3 +-- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/challenge_3/java/jdfurlan/README.md b/challenge_3/java/jdfurlan/README.md index ca7393b8a..6a4ab4ba5 100644 --- a/challenge_3/java/jdfurlan/README.md +++ b/challenge_3/java/jdfurlan/README.md @@ -1,22 +1,22 @@ -# InvertBinarTree +# MajorityElement ###### Java 8 ### 1. Approach to Solving the problem -Trees suck if you haven't messed with them in a while! -Swapping the values is relatively easy using an A-B-C swap approach. -The approach to print was to do a [Breadth-First Traversal](https://www.cs.bu.edu/teaching/c/tree/breadth-first/) +I used a map to make sure all equivalent elements were mapped to the same key +and tracked their frequencies with their value ### 2. How to compile and run this code Compile the Node class first ``` -javac InvertBinarTree.java -java InvertBinarTree +javac MajorityElement.java +java MajorityElement ``` ### 3. How this program works -Iterate through the array, if the key is new add it and frequency of 1 -Otherwise increment its frequency. After incrementing, check if that frequency is > n/2 +As we iterate through the list, if the map has never seen this element before, +place it in the map and initialize its frequency at 1. If we have seen it before, increment +its frequency, check if > n.length/2 and print if true, else update the frequency. diff --git a/challenge_3/java/jdfurlan/src/MajorityElement.java b/challenge_3/java/jdfurlan/src/MajorityElement.java index d34e1b634..16c9e8150 100644 --- a/challenge_3/java/jdfurlan/src/MajorityElement.java +++ b/challenge_3/java/jdfurlan/src/MajorityElement.java @@ -9,11 +9,12 @@ public static void main(String[] args) { HashMap map = new HashMap(); for (int n : nums) { if (map.containsKey(n)) { - map.put(n, map.get(n) + 1); - if (map.get(n) > (nums.length / 2)) { + int f = map.get(n) + 1; + if (f > (nums.length / 2)) { System.out.println(n); System.exit(0); } + map.put(n, f); } else { map.put(n, 1); } diff --git a/challenge_4/java/jdfurlan/README.md b/challenge_4/java/jdfurlan/README.md index 850adc4ce..9a1b59601 100644 --- a/challenge_4/java/jdfurlan/README.md +++ b/challenge_4/java/jdfurlan/README.md @@ -1,4 +1,4 @@ -# InvertBinarTree +# InvertBinaryTree ###### Java 8 ### 1. Approach to Solving the problem @@ -11,8 +11,8 @@ The approach to print was to do a [Breadth-First Traversal](https://www.cs.bu.ed ``` -javac InvertBinarTree.java -java InvertBinarTree +javac InvertBinaryTree.java +java InvertBinaryTree ``` ### 3. How this program works diff --git a/challenge_6/java/jdfurlan/src/Ranges.java b/challenge_6/java/jdfurlan/src/Ranges.java index 762ab2c93..413acbee3 100644 --- a/challenge_6/java/jdfurlan/src/Ranges.java +++ b/challenge_6/java/jdfurlan/src/Ranges.java @@ -17,11 +17,9 @@ private static ArrayList findRanges(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { first = arr[i]; last = 0; - while (arr[i + 1] == arr[i] + 1) { + while (i < arr.length-1 && arr[i + 1] == arr[i] + 1) { i++; last = arr[i]; - if (i == arr.length - 1) - break; } if (last != 0) res.add(first + "->" + last); diff --git a/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java b/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java index 43ae36e73..c66590dd8 100644 --- a/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java +++ b/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java @@ -11,10 +11,9 @@ private static int findMissingNum(int[] arr) { int thisSum = 0; int i; for (i = 0; i < arr.length; i++) { - actualSum += i; + actualSum += i+1; thisSum += arr[i]; } - actualSum += i; return actualSum - thisSum; } From c9a37261cae9f9c49a68d406cb0ba6fbfd188a9a Mon Sep 17 00:00:00 2001 From: Jesse Furlan Date: Wed, 11 Jan 2017 15:05:22 -0800 Subject: [PATCH 4/5] [Java] Challenge_10 (Unreviewed) --- challenge_10/java/jdfurlan/README.md | 32 +++++++++ .../java/jdfurlan/src/ValidClosers.java | 68 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 challenge_10/java/jdfurlan/README.md create mode 100644 challenge_10/java/jdfurlan/src/ValidClosers.java diff --git a/challenge_10/java/jdfurlan/README.md b/challenge_10/java/jdfurlan/README.md new file mode 100644 index 000000000..b4a4e45d5 --- /dev/null +++ b/challenge_10/java/jdfurlan/README.md @@ -0,0 +1,32 @@ +# ValidClosers +###### Java 8 + +### 1. Approach to Solving the problem + +Struggled for a while and looked at zmiller91's approach and derived something very similar +Once again his logic helped me learn much on this one! + +### 2. How to compile and run this code + + +``` +javac ValidClosers.java +java ValidClosers +``` + +### 3. How this program works + +Track openers "(" "{" or "[" in one list, and closers in another. +This problem pretty much requires a stack, which is last in first out. +Put all the openers on the stack, which is the same but reverse order that closers +should be in the rest of the string. + +eg. if we actually used two stacks, they would look like this: +``` +openers = (, (, ( +closers = ), ), ) +``` + +So we continue iterating through the closers from "first to last", and compare "last to first" in the openers stack. +If we have a match, it's safe to pop it off the stack and check the next pair. If the stack's empty we know they all matched. + \ No newline at end of file diff --git a/challenge_10/java/jdfurlan/src/ValidClosers.java b/challenge_10/java/jdfurlan/src/ValidClosers.java new file mode 100644 index 000000000..c338cc226 --- /dev/null +++ b/challenge_10/java/jdfurlan/src/ValidClosers.java @@ -0,0 +1,68 @@ + +import java.util.List; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Scanner; +import java.util.Stack; +/** + * + * @author jdfurlan but zmiller91's comments and solution helped immensely! + * + */ +public class ValidClosers { + + private static boolean checkClosers(String nextLine) { + if (nextLine.length() == 0) + return true; + List openers = Arrays.asList('{', '[', '('); + List closers = Arrays.asList('}', ']', ')'); + Stack stack = new Stack(); + for (char c : nextLine.toCharArray()) { + // if we found an opener just put it at the top of the stack + if (openers.contains(c)) { + stack.push(c); + } + if (closers.contains(c)) { + // the stack can't be empty when looking at a closer, otherwise + // they don't match + if (!stack.isEmpty()) { + int closer_idx = closers.indexOf(c);// get the index of the + // closer, it matches + // the opener's index + // if the top value in the stack doesn't match closer's + // counterpart + // return false can't be a match + if (stack.peek() != openers.get(closer_idx)) { + return false; + } else { + // otherwise we pop off the stack cause it was a match + stack.pop(); + } + + } else { + // if we have a closer but the stack is already empty + return false; + } + } + } + return stack.isEmpty(); + } + + public static void main(String[] args) { + // my weird way of doing test cases + HashMap map = new HashMap(); + map.put("{{{{{{{{{adfkjaefia}}}}}}}", false); + map.put("{{{{{{{{{[[[[[[kadfa{{{{{{{((({daljfdaf({{{[]}}kaldjfs})})))}}}}}}}]]]]]]}kjfela}}}}}}}}", true); + map.put("{{{[}}}}dafda", false); + map.put("{{{{{{{{{}}}}}}}}}", true); + map.put("[[[[[[[[[kafjalfeianfailfeja;fjai;efa;sfj]]]]]]]]]kjajdain", true); + map.put("", true); + map.put("((((((fjdalfeja((((alefjalisj(())))))))))))d", true); + map.put(")))(((d", false); + map.put("({)}", false); + for (String s : map.keySet()) { + System.out.println("Expected output for:" + s + " is " + map.get(s)); + System.out.println("Actual output is: " + checkClosers(s) + "\n"); + } + } +} From 32d38785a1fd725b18e46bba8fc197601d7f3540 Mon Sep 17 00:00:00 2001 From: jdfurlan Date: Mon, 23 Jan 2017 08:04:19 -0800 Subject: [PATCH 5/5] Update ValidClosers.java --- challenge_10/java/jdfurlan/src/ValidClosers.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge_10/java/jdfurlan/src/ValidClosers.java b/challenge_10/java/jdfurlan/src/ValidClosers.java index c338cc226..c7cca890d 100644 --- a/challenge_10/java/jdfurlan/src/ValidClosers.java +++ b/challenge_10/java/jdfurlan/src/ValidClosers.java @@ -22,7 +22,7 @@ private static boolean checkClosers(String nextLine) { if (openers.contains(c)) { stack.push(c); } - if (closers.contains(c)) { + else if (closers.contains(c)) { // the stack can't be empty when looking at a closer, otherwise // they don't match if (!stack.isEmpty()) {