Permalink
Browse files

stanford algorithm design course algorithms, problem set 1 and 2.

  • Loading branch information...
1 parent 20d4d7c commit 7f783c5a625f5ba643a579ad819234ffed1ab897 Eric Schoonover committed Apr 3, 2012
@@ -10,7 +10,7 @@
merged []
[fl & rl :as l] left
[fr & rr :as r] right]
- (cond
+ (ccond
(or (nil? fl) (nil? fr)) (let [rest (or (seq l) (seq r))
rest-merged (into merged rest)]
[inversions rest-merged])
View
@@ -0,0 +1,8 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
View
@@ -0,0 +1,8 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
View
@@ -0,0 +1,13 @@
+# clj
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright (C) 2012 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,3 @@
+(defproject clj "1.0.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :dependencies [[org.clojure/clojure "1.3.0"]])
@@ -0,0 +1 @@
+(ns clj.core)
@@ -0,0 +1,6 @@
+(ns clj.test.core
+ (:use [clj.core])
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))
View
@@ -0,0 +1,52 @@
+#! /usr/bin/python
+
+import sys
+import math
+from random import shuffle
+
+def swap(a, fi, ti):
+ to,fr = a[ti],a[fi]
+ a[ti],a[fi] = fr,to
+
+def pivot1(a, s, l):
+ return s
+
+def pivot2(a, s, l):
+ return l - 1
+
+def pivot3(a, s, l):
+ fi = s
+ li = l - 1
+ mi = fi + int(math.floor((li - fi) / 2))
+
+ tmp = sorted([ [a[fi], fi], [a[li], li], [a[mi], mi] ], key=lambda t: t[0])
+
+ return tmp[1][1]
+
+def quicksort(pf, a, s, l):
+ if (l - s) <= 1:
+ return 0
+
+ c = l - s - 1
+
+ p = pf(a, s, l)
+ swap(a, p, s)
+
+ i = s + 1
+
+ for j in range(s,l):
+ if a[j] < a[s]:
+ swap(a, j, i)
+ i = i + 1
+
+ pe = i - 1
+ swap(a, pe, s)
+
+ c = c + quicksort(pf, a, s, pe)
+ c = c + quicksort(pf, a, i, l)
+
+ return c
+
+#lines = [int(line.strip()) for line in open('/Users/eric/Desktop/QuickSort.txt')]
+#quicksort(pivot3, lines, 0, len(lines))
+
View
@@ -1,16 +0,0 @@
-(defn my-reverse
- "reverse a sequence"
- [lst]
- (loop [l lst, acc nil] ; each iteration will remove an item from the beginning of the
- (if (seq l) ; current sequence `l` and add it to the beginning of `acc`
- (recur (rest l) (cons (first l) acc))
- acc)))
-
-(defn reverse-string
- "reverse a string"
- [src]
- (apply str (my-reverse src))) ; apply will convert the character sequence returned by
- ; (my-reverse) into a string
-
-; (load-file "/Users/eric/src/algorithms/reverse-string/clojure")
-; (reverse-string "!dekrow esrever")
View
@@ -1,30 +0,0 @@
-#! /usr/bin/env python
-
-import math
-
-# this is what candidates ussually start with
-def reverseString1(src):
- l = list(src) # convert input string to list of character
- r = list()
- for i in range(len(src)): # navigate input characters and add them to front of result character list
- r.insert(0, l[i])
- print(''.join(r)) # joins each character into the list with a blank seperator (result is a string, instead of a list of chars)
-
-# this is what people like to see
-def reverseString2(src):
- l = list(src)
- cnt = int(math.floor(len(l) / 2)) # iteration count ends up being half or half - 1 the total length
- for i in range(cnt):
- j = len(l) - 1 - i
- l[i], l[j] = l[j], l[i]
- print(''.join(l))
-
-# this is what you should really do
-def reverseString0(src):
- print(src[::-1])
-
-if __name__ == "__main__":
- str = "!dekrow esrever"
- reverseString0(str)
- reverseString1(str)
- reverseString2(str)

0 comments on commit 7f783c5

Please sign in to comment.