tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
24
Task/Range-expansion/Clojure/range-expansion.clj
Normal file
24
Task/Range-expansion/Clojure/range-expansion.clj
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
(defn split [s sep]
|
||||
(defn skipFirst [[x & xs :as s]]
|
||||
(cond (empty? s) [nil nil]
|
||||
(= x sep) [x xs]
|
||||
true [nil s]))
|
||||
(loop [lst '(), s s]
|
||||
(if (empty? s) (reverse lst)
|
||||
(let [[hd trunc] (skipFirst s)
|
||||
[word news] (split-with #(not= % sep) trunc)
|
||||
cWord (cons hd word)]
|
||||
(recur (cons (apply str cWord) lst)
|
||||
(apply str (rest news)))))))
|
||||
|
||||
(defn parseRange [[x & xs :as s]]
|
||||
(if (some #(= % \-) xs)
|
||||
(let [[r0 r1] (split s \-)]
|
||||
(range (read-string r0) (inc (read-string r1))))
|
||||
(list (read-string (str s))))))
|
||||
|
||||
(defn rangeexpand [s]
|
||||
(flatten (map parseRange (split s \,))))
|
||||
|
||||
> (rangeexpand "-6,-3--1,3-5,7-11,14,15,17-20")
|
||||
(-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20)
|
||||
Loading…
Add table
Add a link
Reference in a new issue