Hett’s Problem 1.28
August 9, 2011
The first problem isn’t very hard. We use the standard system sort, and define a sorting algorithm that compares sublists by their length:
(define (lsort xss)
(define (lt? a b) (< (length a) (length b)))
(sort lt? xss))
Here is Hett’s example:
> (lsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o)))
((o) (d e) (d e) (m n) (a b c) (f g h) (i j k l))
The second problem is a bit more involved. Hett’s solution adds a length field to each sub-list, sorts the sub-lists by length, packs sub-lists with equal lengths into sub-sub-lists, sorts those sub-sub-lists by their lengths using the solution to the first problem, flattens the sub-sub-lists back into their sub-lists, then removes the length fields. Our Standard Prelude lets us do the same:
(define (lfsort xss)
(map cdr (mappend identity (lsort
(group-by (lambda (a b) (= (car a) (car b)))
(sort (lambda (a b) (< (car a) (car b)))
(map (lambda (x) (cons (length x) x)) xss)))))))
Working from the inside out: The inner map
adds a length to the beginning of each sub-list, then sort
orders the sub-lists by length. The group-by
brings together sub-lists of equal length into sub-sub-lists, lsort
sorts those sub-sub-lists as in the first problem, and mappend identity
converts the sub-sub-lists back into sub-lists (it’s similar to flatten except that it strips only one layer of nesting instead of all the layers of nesting). Finally, the outer map
removes the length that the inner map
added.
Our function returns the same solution as Hett’s, except that the two initial lists, each with length frequency of 1, are reversed; Hett did not specify what to do in that case, so our solution is as good as his.
> (lfsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o)))
((o) (i j k l) (a b c) (f g h) (d e) (d e) (m n))
We used mappend, identity, and group-by from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/fQUYFP6u.
Python 3 (Should work in 2.7 also).
Basically the same as Carr’s above.
[…] today’s Programming Praxis, our goal is to sort a list of lists by length and by length frequency. […]
My Haskell solution (see http://bonsaicode.wordpress.com/2011/08/09/programming-praxis-hett%E2%80%99s-problem-1-28/ for a version with tests and comments):
Since there’s already a couple Python solutions, here’s my try in Common Lisp:
I used
stable-sort
to make sure order was preserved between elementsthat are the same by the sort’s comparison. My
lfsort
basically uses thesame logic as the Python solutions above. I tried to wrap it all up in a
loop
,but was unable to get things returned properly; if there are any CL gurus out there who
could take the time to improve my
lfsort
, I’d appreciate the help!A ruby version …
Forget my function for lfsort shown above. I don’t know what I was thinking–it recreates the Counter() for every element in the input list. O(n^2) or something like that.
A better lfsort is a variation on F. Carr’s:
Or if you like one-liners, you could do it thus:
Which, I noticed, is the same as Remco’s much more elegant Haskell code.
I mixed HTML and sourcecode option in the above post, and trying to repost didn’t show anything… I try again…
@Graham: maybe something like this:
Oh, f2 didn’t do what was asked for…
Correct answer is
but loop is in fact quite useless here, and the code amounts to what you wrote…
Thanks @Axio! I was trying to make it all self-contained, with the hash
table one of the local “for” variables in the loop, but could never get it
working. Perhaps I’m overly fond of the loop macro, and shouldn’t get
so fixated on using it for every problem :-)
Perl 5 solution, making use of the Schwartzian transform.
And a link to codepad with the original list sorted.
http://codepad.org/BRG0YHIR
Here’s my clojure solution. More details at my blog.
i did this in perl 5 also, with a minor variation on the lfsort posted by boyo:
sub lfsort {
my $length_freq;
return map { $_->[1] }
sort { $length_freq->{ $a->[0] } $length_freq->{ $b->[0] } }
map { [ scalar @$_, $_ ] }
map { $length_freq->{ scalar @$_ }++; $_ } @_
}
oops, forgot to mention that the perl5 solution does not require schwartzian transformation on the lsort:
sub lsort {
return sort { scalar @{$a} scalar @{$b} } @_;
}