Skip to content

[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
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions challenge_0/java/jdfurlan/README.md
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!"
12 changes: 12 additions & 0 deletions challenge_0/java/jdfurlan/src/HelloWorld.java
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);
}

}

}
20 changes: 20 additions & 0 deletions challenge_1/java/jdfurlan/README.md
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
18 changes: 18 additions & 0 deletions challenge_1/java/jdfurlan/src/ReverseAString.java
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();
}
}
32 changes: 32 additions & 0 deletions challenge_10/java/jdfurlan/README.md
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.

68 changes: 68 additions & 0 deletions challenge_10/java/jdfurlan/src/ValidClosers.java
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);
}
else 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");
}
}
}
22 changes: 22 additions & 0 deletions challenge_2/java/jdfurlan/README.md
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
25 changes: 25 additions & 0 deletions challenge_2/java/jdfurlan/src/SingleNumber.java
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);
}
}
}

}
22 changes: 22 additions & 0 deletions challenge_3/java/jdfurlan/README.md
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.
25 changes: 25 additions & 0 deletions challenge_3/java/jdfurlan/src/MajorityElement.java
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);
}
}

}

}
24 changes: 24 additions & 0 deletions challenge_4/java/jdfurlan/README.md
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.
54 changes: 54 additions & 0 deletions challenge_4/java/jdfurlan/src/InvertBinaryTree.java
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);
}

}
11 changes: 11 additions & 0 deletions challenge_4/java/jdfurlan/src/Node.java
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;
}
}
23 changes: 23 additions & 0 deletions challenge_5/java/jdfurlan/README.md
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!
Loading