The Python Challenge – 1

So, my first effort in about 5 minutes gave me the following secret message:

The answer is: ” fone you biblr rp{lsj{re ir zy f{lb. rf{rs wf{r comnureps {pe dop. boilg ir il zy f{lb is ileddicielr {lb rf{r’s wfy rfis revr is so jolg. usilg srpilg.m{kerp{ls() is pecommelbeb. low {nnjy ol rfe upj.”

Not so good. This was better:

The answer is: “i”hope”you”didnt”tr{nsl{te”it”|y”h{nd0″th{ts”wh{t”computers”{re”for0″doing”it”in”|y”h{nd”is”inefficient”{nd”th{t)s”why”this”text”is”so”long0″using”string0m{ketr{ns*+”is”recommended0″now”{pply”on”the”url”

And this was the last wrong one:

The answer is: “i hope you didnt tr{nsl{te it |y h{nd. th{ts wh{t computers {re for. doing it in |y h{nd is inefficient {nd th{t’s why this text is so long. using string.m{ketr{ns() is recommended. now {pply on the url”

So here was the message:

The answer is: “i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that’s why this text is so long. using string.maketrans() is recommended. now apply on the url”

string.maketrans()!? My code looked like this:

question = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
output = 'The answer is: \"'
for x in range (0 , len(question)-1):
# 97 - 122
if ord(question[x]) >= 97 and ord(question[x]) <= 120:
output += chr(ord(question[x]) + 2)
elif ord(question[x]) >= 121 and ord(question[x]) <= 122:
output += chr(ord(question[x]) - 24)
else:
output += question[x]
print output + '\"'

Python 2.7.5 Solution

But maketrans sounded fun, so I quickly did this.

from string import maketrans   # Required to call maketrans function.
question = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
output = 'The answer is: \"'
intab = "abcdefghijklmnopqrstuvwxyz"
outtab = "cdefghijklmnopqrstuvwxyzab"
trantab = maketrans(intab, outtab)
print question.translate(trantab);

Which was a lot faster, cleaner and easier. But didn’t work at all with Python 3.0 which died on “TypeError: maketrans arguments must be bytes objects”.  I found out quickly that I needed to declare the intab and outtab with a “b” onthe front so they became byte objects.  That, and the changed “print” call and I was done.

Python 3.0 Solution

from string import maketrans   # Required to call maketrans function.
question = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
output = 'The answer is: \"'
intab = b"abcdefghijklmnopqrstuvwxyz"
outtab = b"cdefghijklmnopqrstuvwxyzab"
trantab = maketrans(intab, outtab)
print (question.translate(trantab)); 
 

In the answers they use string.ascii_lowercase for b”abcdefghijklmnopqrstuvwxyz” and  string.ascii_lowercase[2:]+string.ascii_lowercase[:2] for b”cdefghijklmnopqrstuvwxyzab”, which is just showing off.

The Python Challenge – Warming Up

My nephew is doing The Python Challenge and my daughter has just started Python at school.  I thought that it might be fun to have a play.  I know nothing about python really, other than there are annoyingly two subtly different versions, version 2.x and 3.x

So I thought I would have a play and try doing the challenge in both languages.

Warming Up

OK, so this looks like you need to replace the 0 in the URL (http://www.pythonchallenge.com/pc/def/0.html) with 2 to the power of 38.  Even I could do this I thought.  It didn’t take long

A quick google on how to launch the browser, and the small problem of changing a LONG in to a STRING and it was done.  This works in both 2.7.5 and 3.0

import webbrowser
new = 2
warmingupurl = "http://www.pythonchallenge.com/pc/def/" + str(2 ** 38) + ".html"
webbrowser.open(warmingupurl,new=new)

Fiddling with python and pyrow

Got pyrow working last night.  Today not so good, so going to document steps here.  It is as good a place as any other.

cstate = results[‘CSAFE_GETSTATUS_CMD’][0]

Throws “TypeError: list indices must be integers, not str” all of a sudden.  I have changed nothing.  I think that this is because there is nothing coming back from the erg so the results array is empty.

CSAFE is some sort of open source means of communicating with gym equipment I think.

I give up for now.  Frustrating that it worked last night!

Bleah.

SOLVED: rPi, samba, usb disk and read only frustrations.

We’re leaving for holiday tomorrow, so *of course* I decided that it was the *ideal* time to fix the album art in our music connection.  Don’t ask.

I found that I couldn’t access the windows (samba) share on my Raspberry Pi without it being read only.  Two hours of testing later I worked out that it was switching to read only when I tried to edit a file on it from the Windows machine.  Much googling later I worked out that it was probably a self defence mechanism of the disk switching to read-only because it had problems.

I knew it had problems but had ignored them.

A little googling later and I had downloaded dosfstools, unmounted the disk and run

sudo dosfsck -r /dev/sdsa1

repeatedly until it had no errors.  Then it worked perfectly.