From cc21edafca293ce14b378956671f7ccaed0a894e Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 20 Feb 2020 05:44:14 -0600 Subject: [PATCH] feb 2015 problem 9 and 10 --- uil/aplus-february-2015/10/Radians.MD | 9 ++++++ uil/aplus-february-2015/10/Radians.java | 23 ++++++++++++++ uil/aplus-february-2015/9/Play.MD | 15 +++++++++ uil/aplus-february-2015/9/Play.java | 41 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 uil/aplus-february-2015/10/Radians.MD create mode 100644 uil/aplus-february-2015/10/Radians.java create mode 100644 uil/aplus-february-2015/9/Play.MD create mode 100644 uil/aplus-february-2015/9/Play.java diff --git a/uil/aplus-february-2015/10/Radians.MD b/uil/aplus-february-2015/10/Radians.MD new file mode 100644 index 0000000..e35b904 --- /dev/null +++ b/uil/aplus-february-2015/10/Radians.MD @@ -0,0 +1,9 @@ +# Radians + +Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-Radians)!** + +This problem was rather simple to implement, although, it did trip me up when it specified that everything `must be in terms of PI`. I didn't get that essentially you have to divide the converted radians to PI, so it helps to remember the meaning of that sentence (but I did later notice and correct this). + +Otherwise, implementation is self explanatory and requires no extraneous thought. For those not aware, the method `Math.toRadians` can be used to convert degrees to radians easily, which is very important to remember should one be programming in JCreator, or an IDE with no built-in/enabled code completion/code intelligence plugins. + +I recommend using `String.format` for this problem deeply, as it helps with rounding off the decimal radians measure quickly. \ No newline at end of file diff --git a/uil/aplus-february-2015/10/Radians.java b/uil/aplus-february-2015/10/Radians.java new file mode 100644 index 0000000..3908a00 --- /dev/null +++ b/uil/aplus-february-2015/10/Radians.java @@ -0,0 +1,23 @@ +import static java.lang.System.out; +import java.util.Scanner; +import java.util.Arrays; +import java.io.File; +import java.io.IOException; + +class Main { + public static void main(String[] args) throws IOException { + Scanner s = new Scanner(new File("radians.dat")); + while(s.hasNextInt()) { + int degrees = s.nextInt(); + double rad = Math.toRadians(degrees) / Math.PI; + String radians; + if(rad == 1) + radians = ""; + else if(rad % 1 == 0) + radians = Integer.toString((int) rad); + else + radians = String.format("%.2f", rad); + out.println(String.format("%s degrees = %sPI radians", degrees, radians)); + } + } +} \ No newline at end of file diff --git a/uil/aplus-february-2015/9/Play.MD b/uil/aplus-february-2015/9/Play.MD new file mode 100644 index 0000000..f33e3e0 --- /dev/null +++ b/uil/aplus-february-2015/9/Play.MD @@ -0,0 +1,15 @@ +# Play + +Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-Play)!** + +This solution took me way longer than it EVER should have taken to implement. I decided to take my sweet time creating a ultra-perfect solution that would be more accurate, faster, and analytical to how the problem actually functioned (which ended up being a good refresher on how absolute value parent functions can be determined from a graph or table). + +I tried really hard to understand how to program, but I always came up with solutions I really hated, where a while loop would be used, a for loop twice, where printing would occur more than once, where multiple string concatenations would occur... It's rather idiotic to derail too much on why I hated this problem so much, and I understand that I should have simply created a solution worthy of the trash can, even if it ended up helping me win a real competition. + +Never the less, I will detail on how I solved it. After a while of staring at it, I decided to create a table based on the length of the stars produced in the original string, alongside the value used to create it (one I called a "magnitude"). After observing it for a while, I discerned that the value started at a specific point based on the magnitude, and decreased in both directions. This variable ended up being equal to `n = 2m - 1` (where `m` is `magnitude`, `n` representing this mystery value). + +After figuring out that the equation is definitely an absolute equation, I started typing in equations until I could figure out one that matched the table [I created in Desmos](https://www.desmos.com/calculator/0gszz81jpx). I eventually created my final equation: `y = n - 2|x - (n + 1) / 2|`, which can be expanded to `y = (2m - 1) - 2|x - ((2m - 1) + 1) / 2|`, and then simplified to `y = 2(m - |x - m|) - 1` (not sure why I just typed all those out). + +Following me finding the correct equation, I used it to develop my solution. I created a integer array to store my table of values, with a length equal to the magnitude (and then later used that length to substitute in my equation - yeah I'm so optimized). A string sequence is then filled with a string repeat method against the now filled integer array. I then print the output of `String.join` fed a newline and the primitive String array. + +Overall, I don't know specifics on other ways of solving this, but brute force analysis and typing "solutions" works, this isn't a complex problem, so deep understanding of the patterns that the output contains is not required. \ No newline at end of file diff --git a/uil/aplus-february-2015/9/Play.java b/uil/aplus-february-2015/9/Play.java new file mode 100644 index 0000000..50d2e57 --- /dev/null +++ b/uil/aplus-february-2015/9/Play.java @@ -0,0 +1,41 @@ +import static java.lang.System.out; +import java.util.Scanner; +import java.io.File; +import java.io.IOException; + +class Main { + public static void main(String[] args) throws IOException { + Scanner s = new Scanner(new File("play.dat")); + while(s.hasNextInt()) { + int magnitude = s.nextInt(); + out.println(play(magnitude)); + if(s.hasNextInt()) + out.println(); + } + } + + // 3 => 1 3 5 3 1 (5) + // 4 => 1 3 5 7 5 3 1 (7) + // 5 => 1 3 5 7 9 7 5 3 1 (9) + // https://www.desmos.com/calculator/0gszz81jpx + // m = 2x - 1 (top) + // y = m - 2 * abs(x - ((m + 1) / 2)) + public static String play(int mag) { + int[] counts = new int[(mag * 2) - 1]; + String[] sequence = new String[counts.length]; + + for(int i = 1; i <= counts.length; i++) + counts[i - 1] = counts.length - (2 * Math.abs(i - (counts.length + 1) / 2)); + for(int i = 0; i < counts.length; i++) + sequence[i] = repeat("*", counts[i]); + + return String.join("\n", sequence); + } + + public static String repeat(String sub, int n) { + String result = ""; + for(int i = 0; i < n; i++) + result += sub; + return result; + } +} \ No newline at end of file