Advertisement
Guest User

Untitled

a guest
Apr 27th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; Sock Merchant problem.
  2. (defn match_socks
  3.   "Given a list of integers, return the number of distinct pairs that could be made."
  4.   [socks]
  5.   ; Build a map of the counts for each number
  6.   (def totals (reduce #(assoc %1 %2 (inc (%1 %2 0))) {} socks))
  7.   ; Reduce the counts map by counting the pairs for each number
  8.   (reduce (fn [total socks] (+ total (int (/ (last socks) 2)))) 0 totals)
  9. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement