I've been messing around with a site called www.codeingame.com and I'm having a blast working out the rust in my old programming skills while building new skills in python 3 which I never really pushed beyond the basic learning. I want to thank my old buddy Milo (labrat) for encouraging me in this.
Anyway, This entry is about a puzzle I completed called Chuck Norris.
The instructions were relatively easy, even if it took me a couple tries to figure it out.
The input message consists of ASCII characters (7-bit)
The encoded output message consists of blocks of 0
A block is separated from another block by a space
Two consecutive blocks are used to produce a series of same value bits (only 1 or 0 values):
Example
Let’s take a simple example with a message which consists of only one character: Capital C. C in binary is represented as 1000011, so with Chuck Norris’ technique this gives:
0 0 (the first series consists of only a single 1)
00 0000 (the second series consists of four 0)
0 00 (the third consists of two 1)
So C is coded as: 0 0 00 0000 0 00
Second example, we want to encode the message CC (i.e. the 14 bits 10000111000011) :
0 0 (one single 1)
00 0000 (four 0)
0 000 (three 1)
00 0000 (four 0)
0 00 (two 1)
So CC is coded as: 0 0 00 0000 0 000 00 0000 0 00
The instructions were relatively easy, even if it took me a couple tries to figure it out.
Here is the encoding principle:The input message consists of ASCII characters (7-bit)
The encoded output message consists of blocks of 0
A block is separated from another block by a space
Two consecutive blocks are used to produce a series of same value bits (only 1 or 0 values):
- First block: it is always 0 or 00. If it is 0, then the series contains 1, if not, it contains 0
- Second block: the number of 0 in this block is the number of bits in the series
Example
Let’s take a simple example with a message which consists of only one character: Capital C. C in binary is represented as 1000011, so with Chuck Norris’ technique this gives:
0 0 (the first series consists of only a single 1)
00 0000 (the second series consists of four 0)
0 00 (the third consists of two 1)
So C is coded as: 0 0 00 0000 0 00
Second example, we want to encode the message CC (i.e. the 14 bits 10000111000011) :
0 0 (one single 1)
00 0000 (four 0)
0 000 (three 1)
00 0000 (four 0)
0 00 (two 1)
So CC is coded as: 0 0 00 0000 0 000 00 0000 0 00
Here's the code I submitted for the win:
import sys
import math
message = input()
# Convert to binary phrases and rejoin as string
bin_message = ' '.join(format(ord(x), 'b') for x in message)
# debugging examples
#print("from input() = %s" % message, file=sys.stderr)
#print("bin_message from input() = %s" % bin_message, file=sys.stderr)
####################### copy and paste between here only ##################################
## this is just a note for me when going back and forth to IDLE
## they thought they could get me by throwing in ASCII characters with leading 0's
def fix_short_bin(raw_ord):
#temp = raw_ord.split(" ")
buffered = []
for item in raw_ord.split(" "):
buffered += item.rjust(7, "0") + " "
return buffered[:-1]
## meat of the app, scan through entire string and count instances of each "stringed" bit
def encode_msg(binary_string):
first_block = { 1 : "0 ", 0 : "00 " }
count = 0
last_read = ""
half_encoded = ""
for byte_char in binary_string:
for bit_char in byte_char:
# another debugging example
# print("count = %d last_read = %s" % (count, last_read))
if last_read == "": ## if this is the first character count it and keep going
last_read = bit_char
count += 1
elif last_read.isspace():
continue
## ignore the spaces between byte_char's
## only left them in for debugging asthetics
elif bit_char == last_read: ## keep counting if they're the same
count += 1
elif (bit_char == "1" or bit_char == "0") and (bit_char != last_read):
## I had an issue with this code counting spaces as well
half_encoded += (first_block[int(last_read)] + ("0" * count) + " ")
count = 1 ## restart the count for the next ord
last_read = bit_char
## since the append is driven by the next char, last char needs to be added at eol
half_encoded += (first_block[int(last_read)] + ("0" * count))
# print(half_encoded, file=sys.stderr)
#print(half_encoded)
return half_encoded
####################### copy and paste between here only ##################################
print(encode_msg(fix_short_bin(bin_message)))
#print("returned = %s" % encode_msg(bin_message), file=sys.stderr)
import math
message = input()
# Convert to binary phrases and rejoin as string
bin_message = ' '.join(format(ord(x), 'b') for x in message)
# debugging examples
#print("from input() = %s" % message, file=sys.stderr)
#print("bin_message from input() = %s" % bin_message, file=sys.stderr)
####################### copy and paste between here only ##################################
## this is just a note for me when going back and forth to IDLE
## they thought they could get me by throwing in ASCII characters with leading 0's
def fix_short_bin(raw_ord):
#temp = raw_ord.split(" ")
buffered = []
for item in raw_ord.split(" "):
buffered += item.rjust(7, "0") + " "
return buffered[:-1]
## meat of the app, scan through entire string and count instances of each "stringed" bit
def encode_msg(binary_string):
first_block = { 1 : "0 ", 0 : "00 " }
count = 0
last_read = ""
half_encoded = ""
for byte_char in binary_string:
for bit_char in byte_char:
# another debugging example
# print("count = %d last_read = %s" % (count, last_read))
if last_read == "": ## if this is the first character count it and keep going
last_read = bit_char
count += 1
elif last_read.isspace():
continue
## ignore the spaces between byte_char's
## only left them in for debugging asthetics
elif bit_char == last_read: ## keep counting if they're the same
count += 1
elif (bit_char == "1" or bit_char == "0") and (bit_char != last_read):
## I had an issue with this code counting spaces as well
half_encoded += (first_block[int(last_read)] + ("0" * count) + " ")
count = 1 ## restart the count for the next ord
last_read = bit_char
## since the append is driven by the next char, last char needs to be added at eol
half_encoded += (first_block[int(last_read)] + ("0" * count))
# print(half_encoded, file=sys.stderr)
#print(half_encoded)
return half_encoded
####################### copy and paste between here only ##################################
print(encode_msg(fix_short_bin(bin_message)))
#print("returned = %s" % encode_msg(bin_message), file=sys.stderr)
If you have any questions about this puzzle then by all means, go to CodeInGame and try it out yourself. If you want to discuss my code specifically, then just comment below.
~ Friar Greg
No comments:
Post a Comment