Skip to content
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

[Java] Challenge 0-9, 12 (Unreviewed) #394

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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();
}
}
19 changes: 19 additions & 0 deletions challenge_12/java/jdfurlan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Compression and Decompression 1
###### Java 8

### 1. Approach to Solving the problem

My first approach involved tracking the count of equal characters found in a row
and once characters changed, checking the number of equal characters and compressing if over the threshold

### 2. How to compile and run this code

```
javac CompAndDecompOne.java
java CompAndDecompOne
```

### 3. How this program works

Walk through strings and compress based on the threshold. Decompress based
on integer value of character used during compression.
133 changes: 133 additions & 0 deletions challenge_12/java/jdfurlan/src/CompAndDecompOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

import java.util.List;
import java.util.Arrays;

public class CompAndDecompOne {
// the compression threshold used to reduce string size. If too low it can
// actually increase the size of the string. Compression fails if threshold < 3
// since the sequence `aa` would end up larger at `a#2`
private static final int THRESHOLD = 4;

/**
* decompress walks through a compressed string and checks if it has found a
* '#' character, otherwise it just adds the character to the StringBuffer.
* If a '#' is found, it was determine the integer value following it, which
* could be 1->10 digits. Once the number is determined, that number - 1 of
* the last character will be added to the StringBuffer. The hardest part of
* this method was just checking for edge cases and indexOutOfBound errors.
*
* @param s
* the string to be decompressed
* @return decompressed version of s
*/
private static String decompress(String s) {
StringBuffer decompressed = new StringBuffer();
StringBuffer subString = new StringBuffer();
char last = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '#') {
i++;
int num = Character.getNumericValue(s.charAt(i));
if (i != s.length() - 1 && Character.isDigit(s.charAt(i + 1))) {
while (Character.isDigit(s.charAt(i)) && i < s.length() - 1) {
num = num * 10 + Character.getNumericValue(s.charAt(i + 1));
i++;
}
}
for (int j = 1; j < num; j++) {
decompressed.append(last);
}
} else {
last = c;
decompressed.append(last);

}
}

return decompressed.toString();
}

/**
* compress walks through an uncompressed string and adds every character to
* a subString buffer until the character changes. Once we see a new
* character, if the size of our buffer is >= THRESHOLD, we must compress,
* else just append the buffer to our final compressedBuffer.
*
* @param s
* string to compress
* @return compressed version of s
*/
private static String compress(String s) {
StringBuffer compressedBuffer = new StringBuffer();
StringBuffer subString = new StringBuffer();
char last = s.charAt(0);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c != last || i == s.length() - 1) {
if (i == s.length() - 1)
subString.append(c);
if (subString.length() >= THRESHOLD) {
compressedBuffer.append(last + "#" + subString.length());
} else {
compressedBuffer.append(subString.toString());
}
subString = new StringBuffer();
subString.append(c);
} else {
subString.append(c);
}
last = c;
}
return compressedBuffer.toString();

}

/**
* Just an alternate version of the other compression that uses slightly
* less space.
*
* @param s
* string to compress
* @return compressed version of s
*/
private static String compressAlternate(String s) {
StringBuffer sb = new StringBuffer();
char lastChar = s.charAt(0);
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != lastChar || i == s.length() - 1) {
if (i == s.length() - 1)
count++;
if (count >= THRESHOLD) {
sb.append(lastChar + "#" + count);
lastChar = s.charAt(i);
count = 1;
} else {
for (int j = 0; j < count; j++) {
sb.append(lastChar);
}
lastChar = s.charAt(i);
count = 1;
}
} else {
lastChar = s.charAt(i);
count++;
}
}
return sb.toString();
}

public static void main(String[] args) {
List<String> list = Arrays.asList("aaaabbbcccaaaaccccccc", "abc",
"bbbfhdjwuwuwuufffffkdkkkkkddddiiiiiqqooodllal", "hhheeelllppp", "jjjjhhhhssssiiiiqqqqllllzzzzppppoooo",
"iiiiiiiiiiiiiiiiiiiiiiiiiiiii", "ggggtttkkkklllppppooouuuuuuuuuuuu");
for (String s : list) {
String comp = compress(s);
System.out.println("Uncompressed: " + s);
System.out.println("Compressed: " + comp);
System.out.println("Decompressed: " + decompress(comp) + "\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.
Loading