code-reviews

raspasov 2021-05-09T05:47:10.017300Z

Another solution for the socks (using frequencies, a bit higher level, avoiding explicit reduce):

(->> 
 [1 2 1 2 1 3 2] 
 (frequencies)
 (map
  (fn [[sock-color cnt]]
   ;determine if we have an odd or even number of socks for each color
   (let [cnt' (if (odd? cnt) (dec cnt) cnt)]
    ;number of pairs of socks for a color
    (/ cnt' 2))))
 ;total of all pairs
 (apply +))

raspasov 2021-05-09T05:51:13.017400Z

I like this solution. Occasionally, I fall in the trap of always looking for the “higher level” solution where a more “procedural” solution would be OK.

raspasov 2021-05-09T06:31:33.019600Z

Second one, less imperative (using a library for dedupe-by):

(count
 (sequence
  (comp
   ;dedupe by below sea level
   (medley.core/dedupe-by #(neg? %))
   ;at sea level
   (filter zero?))
  (reductions + [+1 -1 -1 -1 +1 -1 +1 +1])))

raspasov 2021-05-09T06:33:31.019800Z

@ps ^^

zendevil 2021-05-09T10:06:09.020700Z

https://prit.substack.com/p/hackerrank-3