Paul Mouzas

home

Programming Challenge

04 Apr 2014

This was reddit's Daily Programmer challenge on April first.

The goal is to decode this message:

Etvmp$Jsspw%%%%
[e}$xs$ks%$]sy$lezi$wspzih$xli$lmhhir$qiwweki2$Rs{$mx$mw$}syv$xyvr$xs$nsmr
mr$sr$xlmw$tvero2$Hs$rsx$tswx$er}xlmrk$xlex${mpp$kmzi$e{e}$xlmw$qiwweki2$Pix
tistpi$higshi$xli$qiwweki$sr$xlimv$s{r$erh$vieh$xlmw$qiwweki2$]sy$ger$tpe}$epsrk
f}$RSX$tswxmrk$ls{$}sy$higshih$xlmw$qiwweki2$Mrwxieh$tswx$}syv$wspyxmsr$xs$fi$}syv
jezsvmxi$Lipps${svph$tvskveq$mr$sri$perkyeki$sj$}syv$glsmgi2$
Qeoi$wyvi$}syv$tvskveq$we}w$&Lipps$[svph%%%&${mxl$7$%$ex$xli$irh2$Xlmw${e}
tistpi$fvs{wmrk$xli$gleppirki${mpp$xlmro${i$lezi$epp$pswx$syv$qmrhw2$Xlswi${ls$tswx$lipps
{svph$wspyxmsrw${mxlsyx$xli$xlvii$%%%${mpp$lezi$rsx$higshih$xli$qiwweki$erh$ws$}sy$ger$
tspmxip}$tsmrx$syx$xlimv$wspyxmsr$mw$mr$ivvsv$,xli}$evi$nywx$jspps{mrk$epsrk${mxlsyx$ors{mrk-
Irns}$xlmw$jyr2$Xli$xvyxl${mpp$fi$liph$f}$xlswi${ls$ger$higshi$xli$qiwweki2$>-

Each character's ASCII value has been shifted up by 4.

All we have to do is use the ord() function to get the ASCII value of each character, minus it by 4, and use chr() to turn it back into a character.

With x being a single character string:

chr(ord(x)-4)

If the message is in a text file, we can open it, read it, decode each character, and add it to a string.

f = open('code.txt', 'r')
code = f.read()
ans = ''
for let in code:
    x = chr(ord(let)-4)
    ans += x
print ans

Or we can do it more concisely this way.

with open('code.txt', 'r') as f:
    code = f.read()
    print ''.join(map(lambda x: chr(ord(x)-4), code))

I prefer this way but it may not be as readable using iteration.