-
Notifications
You must be signed in to change notification settings - Fork 160
[Java] Challenge 0 to 10 (Unreviewed) #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdfurlan
wants to merge
10
commits into
YearOfProgramming:master
Choose a base branch
from
jdfurlan:java_10
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af8ca70
[Java] Challenges 0-7
jdfurlan 0d4999c
[Java] Challenge_9
jdfurlan b8cde75
Made code corrections after feedback
jdfurlan e911801
Test commit
jdfurlan 5a2c0f3
[Java] Unfinished Challenge_8
jdfurlan 09bddff
Changed swap file
jdfurlan c9a3726
[Java] Challenge_10 (Unreviewed)
jdfurlan b39970d
[Java] Challenge_10 (Unreviewed)
jdfurlan 9f1ad05
[Java] Challenge_10 (Unreviewed)
jdfurlan 32d3878
Update ValidClosers.java
jdfurlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Character> openers = Arrays.asList('{', '[', '('); | ||
List<Character> closers = Arrays.asList('}', ']', ')'); | ||
Stack<Character> stack = new Stack<Character>(); | ||
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<String, Boolean> map = new HashMap<String, Boolean>(); | ||
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"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Integer> map = new HashMap<String, Integer>(); | ||
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); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# MajorityElement | ||
###### Java 8 | ||
|
||
### 1. Approach to Solving the problem | ||
|
||
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 MajorityElement.java | ||
java MajorityElement | ||
``` | ||
|
||
### 3. How this program works | ||
|
||
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
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<Integer, Integer> map = new HashMap<Integer, Integer>(); | ||
for (int n : nums) { | ||
if (map.containsKey(n)) { | ||
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); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# InvertBinaryTree | ||
###### 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 InvertBinaryTree.java | ||
java InvertBinaryTree | ||
``` | ||
|
||
### 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Node> children = new LinkedList<Node>(); | ||
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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
public class Node { | ||
int data; | ||
Node left; | ||
Node right; | ||
public Node(int data){ | ||
this.data = data; | ||
left = null; | ||
right = null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch thanks!