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!



(4 comments) - (Post a new comment)


[info]sbw
2006-01-24 03:21 am UTC (link)
Python is so sexy! It's my swiss-army-knife ;)

(Reply to this)


[info]crackmonkey
2006-01-24 05:51 am UTC (link)
Since this largely about writing concise code, I'll contribute this :
public class StringSort {
public static void main(String [] args) {
String sortMe = "all I want for christmas is my two front teeth";

Set set = new TreeSet();
set.addAll(Arrays.asList(sortMe.split("")));

System.out.print(set);
}
}

If it's just the function, then it becomes:
public Set createSortedSet(String sortMe) {
Set set = new TreeSet();
set.addAll(Arrays.asList(sortMe.split("")));
return set;
}

I dunno. Java suffers in many areas, and primitive data management is one of those places. I wish I had the article, but I remember alot of big name OO guys who sat around saying that, specifically, String is *not* an array of Strings/chars/Characters. Which is fine, but removes some of the power that you've demonstrated with Python.

So, when is Python getting interfaces?

(Reply to this) (Thread)


[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)


(4 comments) - (Post a new comment)

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