Skip to content

Instantly share code, notes, and snippets.

@narma
Created September 2, 2015 00:30
Show Gist options
  • Save narma/94a2d6b5a39ad7ad1065 to your computer and use it in GitHub Desktop.
Save narma/94a2d6b5a39ad7ad1065 to your computer and use it in GitHub Desktop.
(ns hello.core
(:require [clojure.string :as str]))
(defn parse-int [^String n]
(Integer/parseInt n))
(defn parse-cmd
[arg]
(let [splitted (str/split arg #"\s")]
(concat [(first splitted)]
(some->> (next splitted)
(map parse-int)))))
(defn main
[]
(let [N (parse-int (read-line))]
(loop [stack (lazy-seq '()) i 0 length 0]
(when (> i 0)
(let [item (first stack)]
(if (nil? item)
(println "EMPTY")
(println item)))) ; peek
(when (< i N)
(let [cmd (-> (read-line)
parse-cmd)]
(case (first cmd)
"pop"
(recur (rest stack)
(inc i)
(dec length))
"push"
(recur (conj stack (second cmd))
(inc i)
(inc length))
"inc"
(let [depth (second cmd)
x (last cmd)
[a b] (split-at (- length depth) stack)]
(recur (lazy-cat a (map #(+ % x) b))
(inc i)
length))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment