Friday, February 26, 2010

Books everyone should read not just geeks or hackers

It has been a while since I did not write anything on my blog. I have been busy with work and reading among other things. But two books I read recently made me to want to blog about them. One is Paul Graham's Hackers and Painters, the other is Pomodoro Technique Illustrated by Staffan Noteberg.
On Hackers and Painters:
It is a great book especially for programmers. The breadth of common sense and not so common sense knowledge is breathtaking. It offers unique perspective on programming languages. It is pretty much biased towards Lisp, which is understandable considering his success and expertise on it. However, his advice of not learning Lisp for people over the age of 25 does not do justice to such a great language that every programmer should learn even if they do not use it. I am recent Lisp convert and I can already see its benefit to my daily work. I think earlier Lisp Dialects did not catch on because of its "strange" syntax, which made it more implementer friendly but not user friendly. But a recent dialect called Clojure is poised to make Lisp very very popular. Because the creator of Clojure is making very very nice middle road approach between user and implementer of the language. Anyway, Paul Graham's collection of essays in his book is really eye opening. There is chapter on how America's public school system works, why it is works the way it is and how adults should help kids between the age of 11 and 17. There is also a chapter about how to create wealth. His take on start ups are really thoughtful.

On Pomodoro Technique Illustrated:
Pomodoro means tomato in Italian. It is a focus training method that helps every goal oriented person by allowing them to get things done. What is unique about it is:
1. It is simple
2. It is easy
3. It is effective

Here is what you need to do:
1. Pick a to do item now
2. Set 25 minutes timer and start doing it now
3. Repeat above with new to do item if you finish or continue working after a short break.

All you need is a Timer, a sheet of paper, and your attention. In this memory overflow day and age, everyone needs to stay focused to get anything done. Countless interruption and stimulus is making people not even be able to concentrate for 25 minutes. Try it and you will be more productive.

Friday, January 8, 2010

To roman implementation in Clojure

Here is my take of to roman implementation in Clojure:
I posted this in http://rosettacode.org/wiki/Roman_Numerals#Clojure

1:  (def arabic-roman-map
2: {1 "I", 5 "V",
3: 10 "X", 50 "L",
4: 100 "C", 500 "D",
5: 1000 "M",
6: 4 "IV", 9 "IX",
7: 40 "XL", 90 "XC",
8: 400 "CD", 900 "CM" })
9: (def arabic-roman-map-sorted-keys
10: (sort (keys arabic-roman-map)))
11: (defn find-value-in-coll
12: [coll k]
13: (let [aval (find coll k)]
14: (if (nil? aval) "" (val aval))))
15: (defn to-roman
16: [result n]
17: (let
18: [closest-key-for-n (last (filter #(> n %) arabic-roman-map-sorted-keys))
19: roman-value-for-n (find-value-in-coll arabic-roman-map n)
20: roman-value-for-closet-to-n (find-value-in-coll arabic-roman-map
21: closest-key-for-n)]
22: (if (or (<= n 0) (contains? arabic-roman-map n))
23: (conj result roman-value-for-n)
24: (recur (conj result roman-value-for-closet-to-n)
25: (- n closest-key-for-n)))))
26: Usage: >(to-roman [] 1999)
27: result: ["M" "CM" "XC" "IX"]

Thursday, October 15, 2009

P12: Decode run-length encoding

Following is the direct solution:

1:  def decode(list:List[(Int,Any)]):List[Any] = {  
2: val b = new ListBuffer[Any]()
3: var temp = list
4: while(!temp.isEmpty){
5: for(i<- 1 to temp.head._1){
6: b+=temp.head._2 }
7: temp = temp.tail
8: }
9: b.toList
10: }
11: println("input List = "+dlist)
12: println(decode(dlist))

The input:
List((6,'a), (2,'c), (4,'e))
The output:
List('a, 'a, 'a, 'a, 'a, 'a, 'c, 'c, 'e, 'e, 'e, 'e)

P11: Modified run-length encoding that includes only repeated elements

Use unflatten method from P9 and filter it before mapping the sublist as a tuple:
println(ulist.filter(_.length > 1).map(e=>(e.length,e.head)))

Output:
List((6,'a), (2,'c), (4,'e))

Wednesday, October 14, 2009

Installing Eclipse, Scala and Groovy in Windows 7

There are many ways to install them on Windows 7. I am just listing following simple ways that worked for me:
1. Installing Eclipse 3.5
The latest 3.5 version works just fine on Windows 7. download it then unzip it to a location of your choice.
2. Installing Scala
Download IzPack installer, Just follow the wizard and you are done.
3. Installing Groovy
Download the Windows-Installer package. After installation, you need to do following:
1. Open Control Panel->System and Security->System and click on Advanced system settings.(Shortcut: Win+Pause/Break combination is your friend)
2. Click on Environment Variables
3. Make sure to create User variables such as GROOVY_HOME as name, installation location as value. (Note: Creating System variables did not work for me)
That is all there is to it. I hope someone find this post helpful.

P10: Run-length encoding of a list.

The idea is to use above unflatten function to group consecutive duplicates into a list and them map it to length, head pair. Here is the example:
val x = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)
val ulist = unflatten(x)
println(ulist)
println(ulist.map(e=>(e.length,e.head)))

Output:
List(List('a, 'a, 'a, 'a, 'a, 'a), List('b), List('c, 'c), List('d), List('e, 'e, 'e,'e))
List((6,'a), (1,'b), (2,'c), (1,'d), (4,'e))

Tuesday, October 13, 2009

P09: Pack consecutive duplicates of list elements into sublists.

This might be a good candidate for recursive solution. But much faster iterative solution is shown below:


import scala.collection.mutable.ListBuffer
def unflatten[T](list:List[T]):List[List[T]]={
val uniq = list.removeDuplicates
val c = new ListBuffer[List[T]]()
var uniqv = uniq
while(!uniqv.isEmpty){
c.append(list.filter(_==uniqv.head))
uniqv=uniqv.tail
}
c.toList
}
val x = List(1,2,3,1,2,3,1,2,3,1,2,3)
println(unflatten(x))

Output:
List(List(1, 1, 1, 1), List(2, 2, 2, 2), List(3, 3, 3, 3))