mirror of
https://github.com/Xevion/contest.git
synced 2025-12-06 03:14:40 -06:00
feb 2015 problem 8
This commit is contained in:
16
uil/aplus-february-2015/8/Name.MD
Normal file
16
uil/aplus-february-2015/8/Name.MD
Normal file
@@ -0,0 +1,16 @@
|
||||
# Name
|
||||
|
||||
Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-Name)!**
|
||||
|
||||
This one is very simple, and it's also one of those where there aren't any "gotchas" (sub-problems designed to slip one up, make mistakes and spend precious time testing and re-implementing better solutions), despite it seemingly radiating many.
|
||||
|
||||
I fabricated my own "gotcha" to waste time and get confused, which ended up being that I should have "reversed" the right or left circle additions before concatenating/moving them to the opposing side. My brain didn't predict that they would actually just "move" fluidly, that instead they would come out reversed (I believe I was visualizing a reflection of some sorts instead).
|
||||
|
||||
I noticed that the way they explained the input data, the two integers provided, was rather lackluster, one had to read carefully to pick up on the fact that they were in fact denominators of fractions representing the length of the substrings that one would move around using `rightCircle` and `leftCircle` methods.
|
||||
|
||||
I implemented the solution using basic methods like `reverse`, `rightCircle`, and `leftCircle` methods, which vastly simplified it for me.
|
||||
I think it's important to note that it would be very difficult to do this all in main, and it's why I believe that one should split up stuff into as many separate, simpler operations to make narrowing things down easier.
|
||||
|
||||
The way the output is formatted seems weird to me, the lack of spaces between the arrow delimiter seems weird. I would have appreciated one space on each side being included, but that's just my OCD side kicking in.
|
||||
|
||||
Otherwise, a fun problem to implement. I hope there are more of these, even if I'm not particularly quick at implementing them.
|
||||
49
uil/aplus-february-2015/8/Name.java
Normal file
49
uil/aplus-february-2015/8/Name.java
Normal file
@@ -0,0 +1,49 @@
|
||||
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("name.dat"));
|
||||
while(s.hasNextLine()) {
|
||||
String operation = s.next();
|
||||
String input = s.next();
|
||||
int right = s.nextInt();
|
||||
int left = s.nextInt();
|
||||
|
||||
out.println(String.format(
|
||||
"%s==>%s", input,
|
||||
operation.equals("E") ? encrypt(input, right, left) : decrypt(input, right, left)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public static String encrypt(String input, int right, int left) {
|
||||
right = input.length() / right;
|
||||
left = input.length() / left;
|
||||
return leftCircle(rightCircle(reverse(input), right), left);
|
||||
}
|
||||
|
||||
public static String decrypt(String input, int right, int left) {
|
||||
right = input.length() - (input.length() / right);
|
||||
left = input.length() - (input.length() / left);
|
||||
return reverse(rightCircle(leftCircle(input, left), right));
|
||||
}
|
||||
|
||||
public static String rightCircle(String input, int right) {
|
||||
return input.substring(input.length() - right) + input.substring(0, input.length() - right);
|
||||
}
|
||||
|
||||
public static String leftCircle(String input, int left) {
|
||||
return input.substring(left, input.length()) + input.substring(0, left);
|
||||
}
|
||||
|
||||
public static String reverse(String input) {
|
||||
String result = "";
|
||||
char[] arr = input.toCharArray();
|
||||
for(int i = arr.length - 1; i >= 0; i--)
|
||||
result += arr[i];
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user