From ec99b5824eb8491acff02e0439bbdcbacbe9b145 Mon Sep 17 00:00:00 2001 From: Alex4386 Date: Wed, 15 May 2024 10:54:41 +0900 Subject: [PATCH] chore: done homework --- .../java/me/alex4386/gachon/sw14462/Main.java | 2 +- .../alex4386/gachon/sw14462/day22/Main.java | 18 +++++ .../gachon/sw14462/day22/ex12_1a/Main.java | 7 ++ .../sw14462/day22/ex12_1a/MergeSort.java | 69 +++++++++++++++++++ .../sw14462/day22/ex12_1a/MergeSortDemo.java | 17 +++++ .../gachon/sw14462/day22/ex12_1b/Main.java | 41 +++++++++++ .../gachon/sw14462/day22/ex12_1c/Main.java | 21 ++++++ .../gachon/sw14462/day22/ex12_1d/Main.java | 37 ++++++++++ .../day22/ex12_3/GoldenRatioCalculator.java | 53 ++++++++++++++ .../gachon/sw14462/day22/ex12_3/Main.java | 11 +++ .../gachon/sw14462/day22/ex12_5/Main.java | 18 +++++ 11 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSort.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSortDemo.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1b/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1c/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1d/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/GoldenRatioCalculator.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day22/ex12_5/Main.java diff --git a/src/main/java/me/alex4386/gachon/sw14462/Main.java b/src/main/java/me/alex4386/gachon/sw14462/Main.java index 5d9112d..da2f2d6 100644 --- a/src/main/java/me/alex4386/gachon/sw14462/Main.java +++ b/src/main/java/me/alex4386/gachon/sw14462/Main.java @@ -5,7 +5,7 @@ import java.util.*; public class Main { - public static String currentTarget = "day21"; + public static String currentTarget = "day22"; public static boolean fallbackToLatest = true; public static Map> getAvailableTargetClassNames() { diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day22/Main.java new file mode 100644 index 0000000..4fb49b2 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/Main.java @@ -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; + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/Main.java new file mode 100644 index 0000000..79fa0b2 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day22.ex12_1a; + +public class Main { + public static void main(String[] args) { + MergeSortDemo.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSort.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSort.java new file mode 100644 index 0000000..43fc176 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSort.java @@ -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++; + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSortDemo.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSortDemo.java new file mode 100644 index 0000000..3ebdc20 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1a/MergeSortDemo.java @@ -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(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1b/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1b/Main.java new file mode 100644 index 0000000..9eebbed --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1b/Main.java @@ -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(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1c/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1c/Main.java new file mode 100644 index 0000000..c6493bb --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1c/Main.java @@ -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)); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1d/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1d/Main.java new file mode 100644 index 0000000..da590d2 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_1d/Main.java @@ -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"); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/GoldenRatioCalculator.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/GoldenRatioCalculator.java new file mode 100644 index 0000000..b9caf4f --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/GoldenRatioCalculator.java @@ -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()); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/Main.java new file mode 100644 index 0000000..f84e4d8 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_3/Main.java @@ -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(); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_5/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_5/Main.java new file mode 100644 index 0000000..ba9f2f3 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day22/ex12_5/Main.java @@ -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); + } + } +}