-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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 @@ | ||
package me.alex4386.gachon.sw14462.day22; | ||
|
||
import me.alex4386.gachon.sw14462.utils.Chainloader; | ||
|
||
public class Main { | ||
public static String chainloadTarget = "ex12_5"; | ||
|
||
public static void main(String[] args) throws Throwable { | ||
String packageName = Main.class.getPackage().getName(); | ||
String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; | ||
|
||
try { | ||
Chainloader.chainloadTarget(chainLoadTargetClass, args); | ||
} catch (Exception e) { | ||
throw e; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/Main.java
This file contains 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,7 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_1a; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
MergeSortDemo.main(args); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSort.java
This file contains 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,69 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_1a; | ||
|
||
/** | ||
Class for sorting an array of integers from smallest to largest using the merge sort algorithm. | ||
*/ | ||
public class MergeSort | ||
{ | ||
/** | ||
Precondition: Every indexed variable of the array a has a value. Postcondition: a[0] <= a[1] <= ... <= a[a. length - 1]. | ||
*/ | ||
public static void sort(int[] a) | ||
{ | ||
if (a.length >= 2) | ||
{ | ||
int halfLength = a.length / 2; | ||
int[] firstHalf = new int[halfLength]; | ||
int[] lastHalf = new int[a.length - halfLength]; | ||
divide(a, firstHalf, lastHalf); | ||
sort(firstHalf); | ||
sort(lastHalf); | ||
merge(a, firstHalf, lastHalf); | ||
} //else do nothing. a.length == 1, so a is sorted. | ||
} | ||
|
||
//Precondition: a.length = firstHalf.length + lastHalf.length. //Postcondition: All the elements of a are divided | ||
//between the arrays firstHalf and lastHalf. | ||
private static void divide(int[] a, int[] firstHalf, | ||
int[] lastHalf) | ||
{ | ||
for (int i = 0; i < firstHalf.length; i++) | ||
firstHalf[i] = a[i]; | ||
for (int i = 0; i < lastHalf.length; i++) | ||
lastHalf[i] = a[firstHalf.length + i]; | ||
} | ||
|
||
private static void merge(int[] a, int[] firstHalf, | ||
int[] lastHalf) | ||
{ | ||
int firstHalfIndex = 0, lastHalfIndex = 0, aIndex = 0; | ||
while ((firstHalfIndex < firstHalf.length) && | ||
(lastHalfIndex < lastHalf.length)) { | ||
if (firstHalf[firstHalfIndex] < lastHalf[lastHalfIndex]) { | ||
a[aIndex] = firstHalf[firstHalfIndex]; | ||
firstHalfIndex++; | ||
} else { | ||
a[aIndex] = lastHalf[firstHalfIndex]; | ||
lastHalfIndex++; | ||
} | ||
aIndex++; | ||
} | ||
|
||
//At least one of firstHalf and lastHalf has been | ||
//completely copied to a. | ||
//Copy rest of firstHalf, if any. | ||
while (firstHalfIndex < firstHalf.length) | ||
{ | ||
a[aIndex] = firstHalf[firstHalfIndex]; | ||
aIndex++; | ||
firstHalfIndex++; | ||
} | ||
//Copy rest of lastHalf, if any. | ||
while (lastHalfIndex < lastHalf.length) | ||
{ | ||
a[aIndex] = lastHalf[lastHalfIndex]; | ||
aIndex++; | ||
lastHalfIndex++; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSortDemo.java
This file contains 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 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_1a; | ||
|
||
public class MergeSortDemo { | ||
public static void main(String[] args) | ||
{ | ||
int[] anArray = {7, 5, 11, 2, 16, 4, 18, 14, 12, 30}; | ||
System.out.println("Array values before sorting:"); | ||
for (int i = 0; i < anArray.length; i++) | ||
System.out.print(anArray[i] + " "); | ||
System.out.println(); | ||
MergeSort.sort(anArray); | ||
System.out.println("Array values after sorting:"); | ||
for (int i = 0; i < anArray.length; i++) | ||
System.out.print(anArray[i] + " "); | ||
System.out.println(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1b/Main.java
This file contains 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,41 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_1b; | ||
|
||
public class Main { | ||
public static String getWordFromDigit(int digit) { | ||
String[] word = new String[] { | ||
"zero", "one", "two", "three", "four", | ||
"five", "six", "seven", "eight", "nine" | ||
}; | ||
|
||
return word[digit]; | ||
} | ||
|
||
|
||
public static void displayAsWords(int number) { | ||
if (number >= 10) { | ||
displayAsWords(number / 10); | ||
System.out.print(" " + getWordFromDigit(number % 10)); | ||
} | ||
else { | ||
System.out.print(getWordFromDigit(number)); | ||
} | ||
} | ||
|
||
// iterative version | ||
public static void displayAsWordsIterative(int number) { | ||
StringBuilder result = new StringBuilder(); | ||
|
||
do { | ||
result.insert(0, getWordFromDigit(number % 10) + " "); | ||
number /= 10; | ||
} while (number != 0); | ||
|
||
System.out.print(result.toString().trim()); | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println("Printing 1234 as words:"); | ||
displayAsWordsIterative(1234); | ||
System.out.println(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1c/Main.java
This file contains 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,21 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_1c; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
String tel = "010-1234-5678"; | ||
System.out.println("Digit count of "+tel+" is "+countDigits(tel)); | ||
} | ||
|
||
public static int countDigits(String str) { | ||
if (str.length() == 0) { | ||
return 0; | ||
} | ||
|
||
char firstChar = str.charAt(0); | ||
if (firstChar >= '0' && firstChar <= '9') { | ||
return 1 + countDigits(str.substring(1)); | ||
} | ||
|
||
return countDigits(str.substring(1)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1d/Main.java
This file contains 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,37 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_1d; | ||
|
||
public class Main { | ||
// check if string is palindrome, ignoring case and spaces, punctuation marks and cases utilizing recursion | ||
public static boolean checkPalindrome(String s) { | ||
if (s.length() <= 1) { | ||
return true; | ||
} | ||
|
||
char firstChar = s.charAt(0); | ||
char lastChar = s.charAt(s.length() - 1); | ||
|
||
if (!Character.isLetterOrDigit(firstChar)) { | ||
return checkPalindrome(s.substring(1)); | ||
} else if (!Character.isLetterOrDigit(lastChar)) { | ||
return checkPalindrome(s.substring(0, s.length() - 1)); | ||
} else if (Character.toLowerCase(firstChar) != Character.toLowerCase(lastChar)) { | ||
return false; | ||
} | ||
|
||
return checkPalindrome(s.substring(1, s.length() - 1)); | ||
} | ||
|
||
public static void palindromeChecker(String s) { | ||
if (checkPalindrome(s)) { | ||
System.out.println(s + " is a palindrome."); | ||
} else { | ||
System.out.println(s + " is not a palindrome."); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
palindromeChecker("radar"); | ||
palindromeChecker("\"Straw? No, too stupid a fad, I put soot on warts.\""); | ||
palindromeChecker("sensor"); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/GoldenRatioCalculator.java
This file contains 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,53 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_3; | ||
|
||
public class GoldenRatioCalculator { | ||
private int current = 0; | ||
private int prevValue = Integer.MIN_VALUE; | ||
private int thisValue = Integer.MIN_VALUE; | ||
|
||
public void next() { | ||
this.current++; | ||
if (this.thisValue == Integer.MIN_VALUE) { | ||
this.thisValue = 0; | ||
} else if (this.prevValue == Integer.MIN_VALUE) { | ||
this.thisValue = 1; | ||
this.prevValue = 0; | ||
} else { | ||
int bakPrev = this.prevValue; | ||
this.prevValue = this.thisValue; | ||
this.thisValue = bakPrev + this.prevValue; | ||
} | ||
} | ||
|
||
public boolean doCalculation() { | ||
return thisValue != Integer.MIN_VALUE && prevValue != Integer.MIN_VALUE && this.prevValue != 0; | ||
} | ||
|
||
public String getCalculation() { | ||
return this.doCalculation() ? this.thisValue+"/"+this.prevValue : ""; | ||
} | ||
|
||
public double getCalculationResult() { | ||
return this.doCalculation() ? (double)this.thisValue/(double)this.prevValue : 0.0; | ||
} | ||
|
||
public String getFibonacciCalculationStatus() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("Fibonacci #"); | ||
builder.append(this.current); | ||
builder.append(" = "); | ||
builder.append(this.thisValue); | ||
if (this.doCalculation()) { | ||
builder.append("; "); | ||
builder.append(this.getCalculation()); | ||
builder.append(" = "); | ||
builder.append(this.getCalculationResult()); | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
|
||
public void printFibonacciCalculationStatus() { | ||
System.out.println(this.getFibonacciCalculationStatus()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/Main.java
This file contains 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 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_3; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
GoldenRatioCalculator calc = new GoldenRatioCalculator(); | ||
for (int i = 0; i < 5; i++) { | ||
calc.next(); | ||
calc.printFibonacciCalculationStatus(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/me/alex4386/gachon/sw14462/day22/ex12_5/Main.java
This file contains 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 @@ | ||
package me.alex4386.gachon.sw14462.day22.ex12_5; | ||
|
||
public class Main { | ||
public static long getTotalGrains(int k, long grains) { | ||
if (k == 1) { | ||
return grains; | ||
} else { | ||
return grains + getTotalGrains(k - 1, grains * 2); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
for (int i = 1; i <= 10; i++) { | ||
long totalGrains = getTotalGrains(i, 1); | ||
System.out.println("Total grains on " + i + " squares: " + totalGrains); | ||
} | ||
} | ||
} |