Converting a String to Unicode Codes In Python

Written by Dan Sackett on September 5, 2014

Just yesterday I was reading about Python again when I came across Pydanny's website.

Pydanny is in fact Daniel Greenfield, an influential member of the Python and Django community. He's the author of the awesome Two Scoops of Django book and overall a down to earth guy.

I mention him because he was looking for developers to work with him at Eventbrite. He asks developers to Contact him and simply provides the following means of contact.

104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 115, 116, 46, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 112, 121, 100, 97, 110, 110, 121, 47, 97, 56, 55, 100, 57, 54, 49, 54, 102, 49, 50, 55, 52, 48, 48, 97, 57, 55, 97, 52

At first glance, it makes no sense but he gives some hints:

chr, problem solving, list comprehension

Although I'm happy where I currently am working, I wanted to see where this went so I went to solving the problem. The hints pretty much explained it all to me very quickly.

chr() is a function in the Python Standard Library which coverts ordinal ASCII codes to text characters. So I set out to use a list comprehension.

>>> string = [104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 115, 116, 46, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 112, 121, 100, 97, 110, 110, 121, 47, 97, 56, 55, 100, 57, 54, 49, 54, 102, 49, 50, 55, 52, 48, 48, 97, 57, 55, 97, 52]

>>> ''.join([chr(x) for x in string])
'https://gist.github.com/pydanny/a87d9616f127400a97a4'

Just like that, we have a link. Now this gist points to a Python script which you need to debug. I don't want to go through that script because I think it's fun for you to figure out yourself, but this made me smile.

What a genius way to test people. Yes, it is somewhat trivial when you realize what's going on, but it's an entry point and a good one at that. After debugging the gist you can send an email and from there you are sure that you have applicants that have solved a problem before making an inquiry. It's a locked front door with a dangling key.

Want to make your own word problems like this?

Well I did.

the chr() function and ord() function are actually inverses of one another. So to create our own numbered code we can do the following:

>>> my_string = 'Hello, my name is dan'
>>> encoded_string = ', '.join([str(ord(x)) for x in my_string])
>>> print encoded_string
'72, 101, 108, 108, 111, 44, 32, 109, 121, 32, 110, 97, 109, 101, 32, 105, 115, 32, 100, 97, 110'

With that list comprehension, we can create a string of numbers that represent the characters we want to encode. Now to decode that we can do the following:

>>> decoded_string = ''.join([chr(int(x)) for x in encoded_string.split(', ')])
>>> print decoded_string
'Hello, my name is dan'

I love list comprehensions for reasons like this. Look at that code. So beautiful in my mind and so simple.

Anyways, go ahead and write your own entry problems. It sure seems like a good way to pre-process applicants.


python

comments powered by Disqus