Welcome back to week four of the weekly programming contests! As always, you can find the rules and information on the contests Posted Here
This weeks challenge is a relatively simpler one. Your job is to implement the method: public static String binary(String s) (THE METHOD NAME CANNOT BE RENAMED). Here’s what the method does in a list of steps:
Converts each of the characters in the s String to their corresponding ASCII code (“Hi” -> 72 105)
Converts all of the ASCII codes to binary (72 105 -> 1001000 1101001)
Inverts the resulting Binary Strings (1001000 1101001 -> 0110111 0010110)
And finally returns everything slapped together as one big String (0110111 0010110 -> “01101110010110”)
Your code doesn’t have to follow these steps exactly. Any implementation which yields the expected result is fine.
public static String binary(String a){String b="";for(char c:a.toCharArray()){String d="";for(int e=128;e>0;e/=2)d+=((c%=e*2)/e+1)%2;b+=d.substring(d.indexOf("0"));}return b;}
Readable version:
public static String binary(String a) {
String b = "";
for (char c : a.toCharArray()) {
String d = "";
for (int e = 128; e > 0; e /= 2) {
d+=((c%=e*2)/e+1)%2;
}
b += d.substring(d.indexOf("0"));
}
return b;
}
public static String binary(String s){return s.chars().mapToObj(i->Integer.toString(~i&255,2).split("^1+")[1]).reduce("",(t,u)->t+u);}
Longified
public static String binary(String s) {
// Int stream representing the chars
return s.chars()
// Flip bits of int, zero out everything all but the last byte.
//Then get a binary string
.mapToObj(i -> Integer.toString(~i & 0xFF, 2)
// Remove leading 1s as they are not needed
// Since ASCII only uses 7 bit, the 8th is always 0, so
// the flipped string always starts with one or more
// 1s, thus no Out of Bound errors occur for this
.split("^1+")[1])
// join the strings one by one
.reduce("", (t, u) -> t + u);
}
[spoiler=Original Version]
140 chars
public static String binary(String s){return s.chars().mapToObj(i->Integer.toString(~i&0xFF,2).replaceAll("^1+","")).reduce("",(t,u)->t+u);}
Longified
public static String binary(String s) {
// Int stream representing the chars
return s.chars()
// Flip bits of int, zero out everything all but the last byte.
//Then get a binary string
.mapToObj(i -> Integer.toString(~i & 0xFF, 2)
// Remove leading 1s as they are not needed
.replaceAll("^1+", ""))
// join the strings one by one
.reduce("", (t, u) -> t + u);
}
public static String binary(String s){String t="";for(int c:s.toCharArray())for(int i=64;i>0;i/=2)t+=(c&i)>0?"0":"1";return t;}
public static String binary(String s) {
String t = "";
for (int c : s.toCharArray())
for (int i = 64; i > 0; i /= 2)
t += (c & i) > 0 ? "0" : "1";
return t;
}
Here’s a little template in which to test your method. Just plop in your method, compile, and run.
Code
```java
public class BinaryTest {
// binary(String) goes here
</details>
And with that, I think your examples involving a '!' may be wrong. According to http://www.asciitable.com, '!' is 21 in hex, so 010 0001 in binary and 101 1110 negated.