From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Thu, 16 Sep 2010 18:28:12 +0000 (+0530)
Subject: solution to 2.70
X-Git-Url: https://git.rkrishnan.org/simplejson/components/com_hotproperty/frontends/%22doc.html/index.php?a=commitdiff_plain;h=cb6abc68114fa8abb2613833d241a7b6293cb8f3;p=sicp.git

solution to 2.70
---

diff --git a/src/sicp/ex2_70.rkt b/src/sicp/ex2_70.rkt
new file mode 100644
index 0000000..98135ad
--- /dev/null
+++ b/src/sicp/ex2_70.rkt
@@ -0,0 +1,28 @@
+#lang racket
+(require rackunit
+         "ex2_69.rkt"
+         "ex2_68.rkt")
+
+(define symbol-pairs '((A 2) (BOOM 1) (GET 2) (JOB 2) (NA 16) (SHA 3) (YIP 9) (WAH 1)))
+
+(define huffman-tree (generate-huffman-tree symbol-pairs))
+
+(define test-message '(GET A JOB
+                       SHA NA NA NA NA NA NA NA NA
+                       GET A JOB
+                       SHA NA NA NA NA NA NA NA NA
+                       WAH YIP YIP YIP YIP YIP YIP YIP YIP YIP
+                       SHA BOOM))
+
+(check = 
+       (length (encode test-message 
+                       huffman-tree))
+       84)
+
+#|
+a. Encoding requires 84 bits.
+b. There are 8 symbols, so we use 3 bits/symbol.
+   (length test-message) is 36. So total length of the encoded
+   song, had it been fixed length code, will be (* 36 3) = 108
+|#
+