Stephen Thorne ([info]jerub) wrote,
@ 2006-01-24 13:15:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:python

Java^WPython Hacking
Yeah, so Paul Gearon was a little frustrated with java, and was talking about java, and using java to solve a Problem that he was given by a coworker.

Restating the Problem here, it is: Take all the characters in a java.lang.String, and return a string containing a single instance of each of those characters, in sorted order.

I barely read the javaish bits about his post, but I did notice that the length of the solutions was quite long, and being the pythonista that I am, I felt compelled to troll a little.

The python solution to the Problem quite simple - the one I gave was under 10 lines, including docstring and asserts. Later, I was accused of giving a '2 line' solution, and while that's strictly true, wasn't the case at all. In two lines you can barely call a builtin, let alone make sure that you've got the right python version. I also didn't solve the problem correctly - I gave a solution that returned a list, not a sequence.

So I wrote, and tested (with the help of #python/freenode) a complete solution that passes tests on 1.5.2, 2.2.3, 2.3.5 and 2.4.1.

Anyway, I had a little bit of fun doing that. It feels weird, being a person who loves generator expressions and decorators, and can't wait to get his hands on the new generator functions in python2.5 - to write code that would run 7 years ago.

Of course, the python2.4 solution that I'd be using if I didn't want to present a backwards compatible version is:

def sortedUniqueString(seq):
    return ''.join(sorted(set(seq)))


Plus docs and tests, of course!



(Read 4 comments) - (Post a new comment)


[info]nearfar
2006-01-24 02:50 pm UTC (link)
So, when is Python getting interfaces?

until you think that interfaces are _necessarily_ part of the language.

(Reply to this) (Parent)(Thread)


(Anonymous)
2006-01-24 04:31 pm UTC (link)
For some reason that didn't parse correctly for me.

I was specifically asking when they would be necessarily part of the language.

(Reply to this) (Parent)


(Read 4 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…