Home
Stephen Thorne's Journal
 
[Most Recent Entries] [Calendar View] [Friends View]

Tuesday, January 24th, 2006

    Time Event
    1:15p
    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!

    << Previous Day 2006/01/24
    [Calendar]
    Next Day >>

About LiveJournal.com

Advertisement