Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial check-in of a few rosettacode solutions

  • Loading branch information...
commit d8e91b692159e55da18106e36b21118c71182860 0 parents
@purcell authored
9 echo_server.clj
@@ -0,0 +1,9 @@
+;; http://rosettacode.org/wiki/Echo_Server#Clojure
+
+(use '[clojure.contrib.server-socket :only (create-server)])
+(use '[clojure.contrib.duck-streams :only (read-lines write-lines)])
+
+(defn echo [input output]
+ (write-lines (java.io.PrintWriter. output true) (read-lines input)))
+
+;;(create-server 12321 echo)
4 https_request.clj
@@ -0,0 +1,4 @@
+;; http://rosettacode.org/wiki/HTTPS_Request#Clojure
+
+(use '[clojure.contrib.duck-streams :only (slurp*)])
+(slurp* "https://sourceforge.net")
5 input_and_output.clj
@@ -0,0 +1,5 @@
+;; http://rosettacode.org/wiki/File/Input_and_Output#Clojure
+
+(use '[clojure.contrib.duck-streams :only (copy)])
+
+(copy (java.io.File. "input.txt") (java.io.File. "output.txt"))
29 long_multiplication.clj
@@ -0,0 +1,29 @@
+;; http://rosettacode.org/wiki/Long_Multiplication#Clojure
+
+;; INCOMPLETE
+
+(defn digits [n]
+ (if (> n 9)
+ (conj (digits (int (/ n 10))) (mod n 10))
+ [n]))
+
+(defn mult_table [xs ys]
+ (for [y ys]
+ (for [x xs] (* x y))))
+
+(defn enumerate [s]
+ (map vector (iterate inc 0) s))
+
+(defn polymul [xs ys]
+ (apply map (fn [& vs] (reduce + (filter identity vs)))
+ (map (fn [i zs] (concat (repeat i 0) zs))
+ (enumerate (mult_table xs ys)))))
+
+(defn longmult [x y]
+ (reduce (fn [result v]
+ (+ v (* 10 result)))
+ 0
+ (polymul (digits x) (digits y))))
+
+(println (format "%s" (longmult 18446744073709551616
+ 18446744073709551616)))
7 md5.clj
@@ -0,0 +1,7 @@
+;; http://rosettacode.org/wiki/MD5#Clojure
+
+(apply str
+ (map (partial format "%02x")
+ (.digest (doto (java.security.MessageDigest/getInstance "MD5")
+ .reset
+ (.update (.getBytes "The quick brown fox jumps over the lazy dog"))))))
58 non_continuous_subsequences.clj
@@ -0,0 +1,58 @@
+;; http://rosettacode.org/wiki/Non_Continuous_Subsequences#Clojure
+
+
+(use '[clojure.contrib.combinatorics :only (subsets)])
+
+(defn of-min-length [min-length]
+ (fn [s] (>= (count s) min-length)))
+
+
+(defn non-continuous-subsequences [s]
+ (let [enumerated (map vector (iterate inc 0) s)
+ continuous? (fn [c] (= (take (count c) (iterate inc (ffirst c)))
+ (map first c)))]
+ (->> enumerated
+ subsets
+ (filter (complement continuous?))
+ (map (partial map second)))))
+
+(filter (of-min-length 2) (non-continuous-subsequences [:a :b :c :d]))
+
+
+;; Naive subseq test
+
+(defn of-min-length [min-length]
+ (fn [s] (>= (count s) min-length)))
+
+(defn runs [c l]
+ (map (partial take l) (take-while not-empty (iterate rest c))))
+
+(defn is-subseq? [c sub]
+ (some identity (map = (runs c (count sub)) (repeat sub))))
+
+(defn non-continuous-subsequences-2 [s]
+ (filter (complement (partial is-subseq? s)) (subsets s)))
+
+
+(filter (of-min-length 2) (non-continuous-subsequences-2 [:a :b :c :d]))
+
+(is-subseq? [:a :b :c :d] [:a :b])
+(is-subseq? [:a :b :c :d] [:b :d])
+
+
+;; Translation of Python -- broken
+
+(defn ncsub
+ ([seq]
+ (ncsub seq 0))
+ ([seq s]
+ (if (not-empty seq)
+ (let [x (butlast seq)
+ xs (rest seq)
+ p2 (int (/ s 2))
+ p1 (if (zero? p2) 1 0)]
+ (lazy-seq
+ (concat (for [ys (ncsub xs (+ s p1))]
+ (concat x ys))
+ (ncsub xs (+ s p2)))))
+ (if (>= s 3) [[]] []))))
6 permutation_sort.clj
@@ -0,0 +1,6 @@
+;; http://rosettacode.org/wiki/Permutation_Sort#Clojure
+
+(use '[clojure.contrib.combinatorics :only (permutations)])
+
+(defn permutation-sort [s]
+ (first (filter (partial apply <=) (permutations s))))
Please sign in to comment.
Something went wrong with that request. Please try again.