views:

1261

answers:

5

Decode the following string and encode it back.

Any programming language is welcome, the point is to create a solution with the minimum amount of characters.

Formatting and new lines does not count toward the char count.

Here's the message:


1010100 1101000 1100101 1110010 1100101 1100001 1110010 1100101 110001 110000 1110100 1111001 1110000 1100101 1110011 1101111 1100110 1110000 1100101 1101111 1110000 1101100 1100101 101110 1000100 1100101 1100011 1101111 1100100 1100101 1110100 1101000 1101001 1110011 1110011 1110100 1110010 1101001 1101110 1100111 1100001 1101110 1100100 1100101 1101110 1100011 1101111 1100100 1100101 1101001 1110100 1100010 1100001 1100011 1101011


EDIT

Sigh .... anyway, just for the record:

I create this code-golf after reading google's first message on Twitter: http://twitter.com/google

Yes, probably most of you will find that initial message pointless too, obscure or stupid, but I find it amusing.

Since I wanted to know what it mean, I get to code the simplest binary coder / decoder I could think of:

public static String encode( String s ) { 
    char [] ca = s.toCharArray();
    StringBuilder sb = new StringBuilder();
    for( char c : ca ) { 
        sb.append( c == ' ' ? c : Integer.toString(c,2 ) );
        sb.append( ' ' );
    }
    return sb.toString();


}
public static String decode( String  b ) { 
    String [] p = b.split(" ");
    StringBuilder sb = new StringBuilder();
    for( String s : p ) { 
        sb.append( "".equals(s) ? " ": ( char ) Integer.parseInt( s , 2 ) );
    }
    return sb.toString();
}

Simple, yet verbose, which obviously led me to think: I bet code-golf on SO will show tons of shorter/elegants solutions for this in perl/python/ruby/c# etc

+1  A: 

Unclear problem spec. But here's just the decode:

perl -e 'map {print chr oct "0b$_"} @ARGV' 1010100 1101000 ....

42 from decode, waiting for the encode part.
OscarRyz
+1  A: 

With a description it's a bit less pointless... :)

Here's some C# 3 code:

using System;
using System.Linq;

class P {

 static void Main() {
  string c =
   "1010100 1101000 1100101 1110010 1100101 1100001 1110010 1100101 110001 110000 " +
   "1110100 1111001 1110000 1100101 1110011 1101111 1100110 1110000 1100101 1101111 " +
   "1110000 1101100 1100101 101110 1000100 1100101 1100011 1101111 1100100 1100101 " +
   "1110100 1101000 1101001 1110011 1110011 1110100 1110010 1101001 1101110 1100111 " +
   "1100001 1101110 1100100 1100101 1101110 1100011 1101111 1100100 1100101 1101001 " +
   "1110100 1100010 1100001 1100011 1101011";

  string d = new String(c.Split().Select(n => (char)Convert.ToInt32(n, 2)).ToArray());
  string e = string.Join(" ", d.Select(n => Convert.ToString((int)n, 2)).ToArray());
 }

}

Edit: Takes the input from the command line, as Oscar suggested. If we take the input from the command line, then the result should just go to stdout?

using System;
using System.Linq;

class P {

 static void Main(string[] a) {
  Console.Write(
   string.Join(" ",
   new String(a.Select(n => (char)Convert.ToInt32(n, 2)).ToArray())
   .Select(n => Convert.ToString((int)n, 2)).ToArray())
  );
 }

}
Guffa
Wow!!, You can actually drop the input and have it like arg[0]. About the description, yes.. well.. people felt offended by the lack of it, so I have to put write it down.
OscarRyz
245 chars ( aprox )
OscarRyz
I added a version that takes input from the command line. What does the rules say about where the result goes?
Guffa
+2  A: 

Python

I think probably the obvious way is the shortest

>>> s="""1010100 1101000 1100101 1110010 1100101 1100001 1110010 1100101 110001 110000 1110100 1111001 1110000 1100101 1110011 1101111 1100110 1110000 1100101 1101111 1110000 1101100 1100101 101110 1000100 1100101 1100011 1101111 1100100 1100101 1110100 1101000 1101001 1110011 1110011 1110100 1110010 1101001 1101110 1100111 1100001 1101110 1100100 1100101 1101110 1100011 1101111 1100100 1100101 1101001 1110100 1100010 1100001 1100011 1101011"""
>>> d="".join(chr(int(x,2))for x in s.split())
>>> d
'Thereare10typesofpeople.Decodethisstringandencodeitback'
>>> e=" ".join([bin(ord(x))[2:]for x in d])
>>> e
'1010100 1101000 1100101 1110010 1100101 1100001 1110010 1100101 110001 110000 1110100 1111001 1110000 1100101 1110011 1101111 1100110 1110000 1100101 1101111 1110000 1101100 1100101 101110 1000100 1100101 1100011 1101111 1100100 1100101 1110100 1101000 1101001 1110011 1110011 1110100 1110010 1101001 1101110 1100111 1100001 1101110 1100100 1100101 1101110 1100011 1101111 1100100 1100101 1101001 1110100 1100010 1100001 1100011 1101011'

another decoder

>>> d=eval("chr(0b"+s.replace(" ",")+chr(0b")+")")
>>> d
'Thereare10typesofpeople.Decodethisstringandencodeitback'
gnibbler
+3  A: 

Golfscript - 21 byte decoder, 14 byte encoder

Decoder
Reads from stdin, writes to stdout

" "/{1/{~}%2base}%""+

Encoder
Reads from stdin, writes to stdout

1/{(2base" "}%
gnibbler
+1  A: 

Perl 39 char encoder

perl -naF"" -E'say unpack"B*",$_ for@F'

Perl 32 char decoder

perl -naE'say pack"B*",$_ for@F'


Example usage:

perl -E'say a..z' |                       # abcd...\n
perl -naF"" -E'say unpack"B*",$_ for@F' | # 01100001\n01100010\n01100011 ...
perl -naE'say pack"B*",$_ for@F'          # a\nb\nc\nd\n ...


If you want it to work without printing extra \n, or you need it to work on older Perls:

46 char encode

perl -naF"" -e'print unpack("B*",$_)," "for@F'

34 char decode

perl -naE'print pack"B*",$_ for@F'
perl -e'print a..z,"\n"' |
perl -naF"" -e'print unpack("B*",$_)," "for@F' |
perl -naE'print pack"B*",$_ for@F'
Brad Gilbert