package com.genuineminecraft.bf;
public class Test {
public static String test = "+++++++++[->+++++++++<]>+++.<++++[->++++<]>+.++++++++++++++.+.-----------.+++++.-------.<++++++++[->--------<]>-------.<+++++++++[->+++++++++<]>+++.---------------.++++++++++++++.+.-----------.+++++.-------.<++++++++[->--------<]>-------.<++++[->++++<]>+.<++++[->----<]>-.<++++[->++++<]>++.";
public static void main(String[] args) {
timeTrials();
qualityCheck();
}
public static void qualityCheck() {
String min = encode(test);
String mag = decode(min);
System.out.println("Debug: " + test);
System.out.println("Debug: " + min);
System.out.println("Debug: " + mag);
if (!test.equals(mag))
System.out.println("Error: Test Failed");
else
System.out.println("Success: Test Passed");
}
public static void timeTrials() {
long start, stop;
int count = 1000000;
System.out.println("Debug: Running " + count + " times.");
start = System.currentTimeMillis();
for (int i = 0; i < count; i++)
encode_1(test);
stop = System.currentTimeMillis();
System.out.println("Debug 1: " + (stop - start) + " millseconds.");
start = System.currentTimeMillis();
for (int i = 0; i < count; i++)
encode_2(test);
stop = System.currentTimeMillis();
System.out.println("Debug 2: " + (stop - start) + " millseconds.");
start = System.currentTimeMillis();
for (int i = 0; i < count; i++)
encode_3(test);
stop = System.currentTimeMillis();
System.out.println("Debug 3: " + (stop - start) + " millseconds.");
}
public static String encode(String in) {
return encode_1(in);
}
public static String decode(String in) {
return decode_1(in);
}
private static String encode_1(String in) {
String out = "";
int counter = 0;
for (int place = 0; place < in.length(); place++) {
char compare = in.charAt(place);
if (in.length() > place + 1 && compare == in.charAt(place + 1)) {
counter++;
continue;
}
switch (counter) {
case 0:
out += Character.toString(compare);
break;
case 1:
out += Character.toString(compare) + Character.toString(compare);
break;
default:
out += Integer.toString(counter + 1) + Character.toString(compare);
break;
}
counter = 0;
}
return out;
}
private static String encode_2(String in) {
String out = "";
int counter = 0;
for (int place = 0; place < in.length(); place++) {
char compare = in.charAt(place);
if (in.length() > place + 1 && compare == in.charAt(place + 1)) {
counter++;
continue;
}
out += (counter == 0 ? "" :
counter == 1 ? Character.toString(compare) : Integer.toString(counter + 1)) +
Character.toString(compare);
counter = 0;
}
return out;
}
private static String encode_3(String in) {
String out = "";
while (in.length() > 0) {
int counter = 0;
char compare = in.charAt(0);
while (counter < in.length() && in.charAt(counter) == compare)
counter++;
in = in.substring(counter - (counter == 2 ? 1 : 0));
if (counter > 2)
out += counter;
out += compare;
}
return out;
}
private static String decode_1(String in) {
String out = "";
for (int i = 0; i < in.length(); i++) {
String t = "";
while (Character.isDigit(in.charAt(i)))
t += in.charAt(i++);
int n = 1;
if (!t.isEmpty())
n = Integer.parseInt(t, 10);
for (int j = 0; j < n; j++)
out += in.charAt(i);
}
return out;
}
}
Comments
0 B
|👍
/👎