]> njoseph.me Git - experiments.git/commitdiff
emp_sort: Sort a list of employees by salary in 4 languages
authorJoseph Nuthalapati <njoseph@thoughtworks.com>
Fri, 7 Dec 2018 14:53:58 +0000 (20:23 +0530)
committerJoseph Nuthalapati <njoseph@thoughtworks.com>
Fri, 7 Dec 2018 14:53:58 +0000 (20:23 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
emp_sort/emp_sort.gist [new file with mode: 0644]

diff --git a/emp_sort/emp_sort.gist b/emp_sort/emp_sort.gist
new file mode 100644 (file)
index 0000000..bcc5116
--- /dev/null
@@ -0,0 +1,22 @@
+Clojure
+
+(def emps (map (fn [x] {:name (str "employee" x) :salary (* 100 x)}) (take 10 (range 1 10))))
+(take (/ (count emps) 10) (reverse (sort-by :salary emps)))
+
+Haskell
+
+import Data.List (sortBy)
+
+data Employee = Employee {name :: String, salary :: Double } deriving (Show)
+let emps = map (\x -> Employee ("emp" ++ show x) (x * x)) [1..10] in take ((length emps) `div` 10) (sortBy (\x y -> compare (salary y) (salary x)) emps)
+
+Scala
+
+case class Employee(name: String, salary: Double)
+val emps = (1 to 10).map(i => Employee("employee" + i, i * 100))
+emps.sortBy(_.salary).reverse.take(math.ceil(emps.size / 10.0))
+
+Python
+
+emps = {"employee"+str(i): i*100 for i in range(1, 11)}
+[(key, value) for key, value in sorted(emps.items(), key=lambda (k,v): (-v,k))][0:len(emps)/10]
\ No newline at end of file