Skip to content

Instantly share code, notes, and snippets.

@roman
Created November 27, 2011 18:56
Show Gist options
  • Save roman/1397984 to your computer and use it in GitHub Desktop.
Save roman/1397984 to your computer and use it in GitHub Desktop.
Keep the important parameter first!
(ns main
(:require [clojure.string :as string]))
; this code is to solve the following algorithmic problem:
; http://www.spoj.pl/problems/ADDREV/
(def sum (partial reduce + 0))
(defn main [& args]
(let [n (Integer/parseInt (read-line))]
(repeatedly n reverse-sum)))
(defn flip [g] (fn [a b] (g b a)))
(defn reverse-sum []
(let [rmap (flip map)]
(-> (read-line)
(string/split #"\s+")
(rmap string/reverse)
(rmap #(Integer/parseInt %))
sum
str
string/reverse
(string/replace #"^0+" "")
(rmap println))))
@roman
Copy link
Author

roman commented Nov 27, 2011

Interesting, I was wondering why there wasn't a drop-while, take-while functions with Strings in Clojure (Whenever I tried to used them with Strings I got back a coll of characters rather than another String). It makes sense now given what you said that performance is the main issue.

The problem with minor annoyances is that they add up little by little. Hopefully this list of small annoyances won't grow to much while I'm getting deeper into the rabbit hole of Clojure :-).

Thanks for your feedback @swannodette.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment