public
Created — forked from isa/gist:2571012

Convert in less than 30 lines

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Question: Convert following into the latter data structure in less than 30 lines:
 
List:
A, B, C
A, C, E
E, F, D
D, A, J
E, D, J
 
List
A, B, 1 (frequency)
A, C, 2
A, D, 1
A, E, 1
A, J, 1
B, C, 1
C, E, 1
D, E, 2
D, F, 1
D, J, 2
E, F, 1
E, J, 1
sort_count.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(def l [["A" "B" "C"] ["A" "C" "E"] ["D" "A" "J"] ["E" "D" "J"] ["E" "F" "D"]])
 
(defn combos [x]
(loop [s x c []]
(if (empty? s)
c
(recur (rest s)
(concat c (map #(conj [(first s)] %) (rest s)))))))
 
(map #(list (first %) (count (last %)))
(sort-by first
(group-by identity
(reduce concat (map combos l)))))
 
((["A" "B"] 1) (["A" "C"] 2) (["A" "E"] 1) (["A" "J"] 1) (["B" "C"] 1) (["C" "E"] 1) (["D" "A"] 1) (["D" "J"] 2) (["E" "D"] 2) (["E" "F"] 1) (["E" "J"] 1) (["F" "D"] 1))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.