Hett’s Problem 1.28
August 9, 2011
Over at PrologSite, Werner Hett gives a list of Ninety-Nine Prolog Problems “to give you the opportunity to practice your skills in logic programming.” Today’s exercise is number 1.28 on the list. Hett gives the problem two stars, meaning that he expects a skilled Prolog programmer to spend thirty to ninety minutes to solve it. Here is Hett’s statement of the problem:
1.28 (**) Sorting a list of lists according to length of sublists
a) We suppose that a list (InList) contains elements that are lists themselves. The objective is to sort the elements of InList according to their length. E.g. short lists first, longer lists later, or vice versa.Example:
?- lsort([[a,b,c],[d,e],[f,g,h],[d,e],[i,j,k,l],[m,n],[o]],L).
L = [[o], [d, e], [d, e], [m, n], [a, b, c], [f, g, h], [i, j, k, l]]b) Again, we suppose that a list (InList) contains elements that are lists themselves. But this time the objective is to sort the elements of InList according to their length frequency; i.e. in the default, where sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length come later.
Example:
?- lfsort([[a,b,c],[d,e],[f,g,h],[d,e],[i,j,k,l],[m,n],[o]],L).
L = [[i, j, k, l], [o], [a, b, c], [f, g, h], [d, e], [d, e], [m, n]]Note that in the above example, the first two lists in the result L have length 4 and 1, both lengths appear just once. The third and forth list have length 3; there are two list of this length. And finally, the last three lists have length 2. This is the most frequent length.
Your task is to solve Hett’s problem 1.28; you need not use Prolog. Be sure to follow Hett’s advice:
Your goal should be to find the most elegant solution of the given problems. Efficiency is important, but logical clarity is even more crucial.
When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
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} } @_;
}