Monday, October 3, 2011

Exile

Originally posted 2008-04-17.

Author: Richard North Patterson
Category: Fiction
Ready by: Dennis Boutsikaris
CDs: 17

A very objective look at the Israeli-Palestinian conflict. You will listen to many disturbing stories. You will hear from people living in that divided strip of land, talking about their rationales for living there, occupation, suicide bombings, hatred of others. Few will talk about their hopes for life in peace.

The author, Richard North Patterson prepared very well. He visited Israel and Palestine. He talked to influential people.

The fictional story is about David Wolfe and Hana Arif.  He is a US citizen and a Jew. She is a Palestinian. They studied together law at Harward, but their ways parted afterwards. They meet again after 13 years and that's all I can say without  giving up the plot.

The book is very captivating, especially towards the end. I did not mind traffic jams at all while listening to the story.

The narrator, Dennis Boutsikaris is very skilled allowing you to immerse in the story and almost forget that all voices come from the same person. Female voices sound a bit harsh, but maybe it was intentional.

Danny Deckchair

Originally posted 2009-12-20.


Category: Romantic Comedy
Actors: Rhys Ifans, Miranda Otto, Justine Clarke
Screenplay: Jeff Balsmeyer
Director: Jeff Balsmeyer
Year: 2003

Running Time: 1 hour 40 minutes

Rhys Ifans plays Danny Morgan, a cement man in Sydney who desparately needs his vacation. Unfortunately, his partner Trudy Dunphy, played by Justine Clarke, does not share his love for rustic camping. She would rather stay in the city and pursue her career and a more important man.

Danny Deckchair is a refreshingly light, intriguing, story of finding one's place and mate. Highly recommended, especially when feeling down.

The PG-13 rating in the U.S. is nuts. This is a PG movie.

Clockwise

Originally posted 2008-04-19.


Category: Comedy
Actors: John Cleese
Screenplay: Michael Frayn
Year: 1986

Running Time: 1 hour 36 minutes

John Cleese plays Brian Stimpson - a headmaster obsessed with punctuality. His school resembles a workcamp with principal Stimpson keeping an eye on everyone from his watch-tower.

His few vices include not paying attention to what people say and overusing the word "right", right?

This light comedy is a pleasure to watch. Not overdone. Not boring. Not irritating. Just right.

Playing for Pizza


Originally posted 2008-06-18.

Author: John Grisham
Category: Fiction
Read By: Christopher Evan Welch

I do not know much about American football, but the story of an extremely unlucky NFL player Rick Dockery is still captivating.

Playing for Cleveland Browns, Rick makes 3 mistakes in a matter of minutes, and lands unconscious in a hospital. He's fired. Browns fans want to kill him...  

John Grisham likes Italy. This is his second book I listened to, that takes place mostly in Italy. He likes the food, the people, the clothes, the way of life. I like it too. It is hard not to. 

Predictably Irrational - The Hidden Forces That Shape Our Decisions


Originally posted 2010-03-07.

Author: Dan Ariely
Category: Popular Science
Read By: Simon Jones
CDs: 6

Refreshing, if not groundbreaking, look at human behavior in the context of economics.
Dan Ariely writes about results of real, clever and meaningful research experiments in a very approachable way.
Even though this is a truly scientific book, it has very practical implications for regular consumers like you and me. Make sure you read it before making your next big purchase. An eye opener.

For all chapter overviews go to wikipedia: Predictably Irrational

Highly recommended for adults. Contains details of reasearch about human sexuality that may not be appropriate for young readers.

Huffman's compression algorithm implemented in JavaScript

Originally published 2009-06-02, on tom-ash.net

The other night I have implemented Huffman's compression algorithm in JavaScript. Here is a wiki link describing what Huffman coding is:
Huffman coding
Here is my JavaScript implementation:
<html>
<head>
<title>Huffman's compression algorithm implemented in JavaScript 
by Tomasz Andraszek</title>
<script type="text/javascript">
   
function compress() {
 var input = document.getElementById("input").value;
 document.getElementById("inputlength").innerHTML =input.length*8;
 var probabilities = getProbabilities(input);
 var codes = getCodes(probabilities);
 var output = compressHuffman(input, codes);
 
 var temp = "";
 for (var elem in probabilities) {
   temp += elem + " = " + probabilities[elem] + "<br/>";
 }
 document.getElementById("probabilities").innerHTML = temp;
 
 temp = "";
 for (var elem in codes) {
   temp += elem + " = " + codes[elem] + "<br/>";
 }
 document.getElementById("codes").innerHTML = temp;
 document.getElementById("output").innerHTML = output;
 document.getElementById("outputlength").innerHTML =output.length;
}

function sortNumberAsc(a, b) {
 return a[1] - b[1];
}

function getCodes(prob) {
 var tree = new Array();
 var secondTree = new Array();
 
 this.getNext = function() {
 if (tree.length > 0 && secondTree.length > 0 
               && tree[0].prob < secondTree[0].prob)
   return tree.shift();
 
 if (tree.length > 0 && secondTree.length > 0 
                && tree[0].prob > secondTree[0].prob)
   return secondTree.shift();
 
 if (tree.length > 0)
   return tree.shift();
 
 return secondTree.shift();
 }
 var sortedProb = new Array();
 var codes = new Array();
 
 var x = 0;
 for (var elem in prob) {
   sortedProb[x] = new Array(elem, prob[elem]);
   x = x + 1;
 }
 
 sortedProb = sortedProb.sort(sortNumberAsc);
 x = 0;
 
 for (var elem in sortedProb) {
   tree[x] = new node();
   tree[x].prob = sortedProb[elem][1];
   tree[x].value = sortedProb[elem][0];
   x = x + 1;
 }
 while (tree.length + secondTree.length > 1) {
  var left = getNext();
  var right = getNext();
  var newnode = new node();
  newnode.left = left;
  newnode.right = right;
  newnode.prob = left.prob + right.prob;
  newnode.left.parent = newnode;
  newnode.right.parent = newnode;
  secondTree.push(newnode);
 }

 var currentnode = secondTree[0];
 var code = "";
 while (currentnode) {
  if (currentnode.value) {
   codes[currentnode.value] = code;
   code = code.substr(0, code.length - 1);
   currentnode.visited = true;
   currentnode = currentnode.parent;
  }
  else if (!currentnode.left.visited) {
   currentnode = currentnode.left;
   code += "0";
  }
  else if (!currentnode.right.visited) {
   currentnode = currentnode.right;
   code += "1";
  }
  else {
   currentnode.visited = true;
   currentnode = currentnode.parent;
   code = code.substr(0, code.length - 1);
  }
 }
 return codes;
}

function node() {
  this.left = null;
  this.right = null;
  this.prob = null;
  this.value = null;
  this.code = "";
  this.parent = null;
  this.visited = false;
}

function compressHuffman(input, codes) {
  var output = input.split("");
  for (var elem in output) {
   output[elem] = codes[output[elem]];
  }
  return output.join("");
}

function getProbabilities(input) {
  var prob = new Array();
  var x = 0;
  var len = input.length;
  while (x < len) {
   var chr = input.charAt(x);
   if (prob[chr]) {
    prob[chr] = prob[chr] + 1;
   }
   else {
    prob[chr] = 1;
   }
   x++;
  }

  for (var elem in prob) {
   prob[elem] = prob[elem] / len;
  }
  return prob;
}
</script>

</head>
<body>
Type in the text to compress here:<br />
<textarea id="input" rows="5" cols="80">aaabcc</textarea><br/>
<input type="button" onclick="compress()" value="Compress" /><br/>
Text's length (8 bits per character): <span id="inputlength"></span><br/>
Probabilities of all distinct characters in the text:<br />
<span id="probabilities"></span><br/>
Binary codes assigned to characters: <br /><span id="codes"></span><br/>
Binary output: <span id="output"></span><br/>
Output's length in bits: <span id="outputlength"></span><br/>
</body>
</html>

Enums in JavaScript

Originally published 2008-12-27, on tom-ash.net

JavaScript does not support enums like these in C#:
        enum SpriteSize
        {
            Small = 100,
            Big = 200
        }
        
        void Render(SpriteSize spriteSize)
        {
            if (spriteSize == SpriteSize.Small)
            {
            // TODO: implement
            }
        }


The simplest way to get enum functionality in JavaScript is to create a variable using JSON:
<script type="text/javascript">
            var SpriteSize = 
            {
                Small: 100,
                Big: 200
            }

            function Render(spriteSize)
            {
                if (spriteSize == SpriteSize.Small)
                {
                // TODO: implement
                }
            }
</script>