- "We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of the list according to their length. E.g. short lists first, longer lists later, or vice versa."
- "Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements according to their length frequency; i.e. in the default, sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length come later."
Example:
(deftest p28-sublist-sort (is (= '((o) (d e) (d e) (m n) (a b c) (f g h) (i j k l)) (lsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o)))))) (deftest p28-sublist-freq-sort (is (= '((i j k l) (o) (a b c) (f g h) (d e) (d e) (m n)) (lsort-freq '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o))))))
"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."
Solution:
The solution to a) is trivial if we use Clojure's sort-by and base the key function on the count of the sublists.
The solution to b) is based on the frequencies of the sublists. Clojure comes with the frequencies function that we use on the lengths of the sublists. All that is left is to call sort on the list of lists and use the frequencies map to determine the order of the sublists.
No comments:
Post a Comment