# Concrete Abstractions: Chapter 10

Exercise 10.2: Even when a category is directly testable by Scheme, using EBNF to express it at a more primitive level can help you appreciate the expressive power of EBNF. In this exercise you will use EBNF to describe certain kinds of numbers—a small subset of those allowed by Scheme.
a. Write a production for <unsigned-integer>. You can use the productions for <digit> given above.
b. Next write productions for <integer>; an <integer> may start with a – sign, a + sign, or neither.
c. Finally, write productions for <real-number>, which are (possibly) signed numbers that may have a decimal point. Note that if the real number has a decimal point, there must be at least one digit to the left or to the right (or both) of the decimal point. Thus, -43., .43, 43, 43.21, and 43.0 are all valid real numbers.

Solution: a. <unsigned integer> -> <digit>+
b. <integer> -> -<unsigned integer> | +<unsigned integer> | <unsigned integer>
c. <real-number> -> <integer>. | <integer>.<unsigned integer>* | .<unsigned integer>+ | <integer>

Exercise 10.3: In Section 8.3 we considered expression trees for simple arithmetic expressions. All such expressions are either numbers or lists having an operator (one of +, -, *, or /) and two operands. Actually, there are three important variants, depending on where the operator occurs: in the first position (prefix or Scheme notation), the second position (infix or standard notation), or the third position (postfix, also known as Reverse Polish notation, or RPN). Let’s consider how such expressions can be specified using EBNF.
a. Write productions for <arithmetic-prefix-expression>.

b. Write productions for <arithmetic-infix-expression>.

c. Write productions for <arithmetic-postfix-expression>.

d. As noted in Section 8.3, a postorder traversal of an expression tree re-
sults in a list of the nodes that is identical to the language specified by <arithmetic-postfix-expression>, except that subexpressions are not parenthesized. Revise the productions for <arithmetic-postfix-expression> so that subexpressions are not parenthesized. (The overall top-level expression needn’t be parenthesized either.)

Solution:  <arithmetic-expression> -> + | – | * | /
a. <arithmetic-prefix-expression> -> (<arithmetic-expression> <real-number> <real-number>)
b. <aritmethic-infix-expression> -> (<real-number> <arithmetic-expression> <real-number>)
c. <arithmetic-postfix-expression> -> (<real-number> <real-number> <arithmetic-expression>)

Exercise 10.4: Let’s consider two possible additions to our Micro-Scheme grammar involving regular Scheme expressions.
a. Write a production for let expressions. Remember that let expressions allow zero or more bindings (i.e., parenthesized name/expression pairs), and the body of the let contains one or more expressions. You should define a separate syntactic category for <binding>.
b. Write productions for cond expressions. Remember that cond expressions allow one or more branches, the last of which may be an else, and each branch has one or more expressions following the test condition.

Solution: a. <binding> –> (let ((<name> <expression>)*) <expression>+>
b.
<condition> –> (cond ((<expression>+ <expression>))+ (else <expression>+ )*)

Exercise 10.6:

Solution: a. (if 3 1 5) is a <conditional>. Let’s check if 3, 1, and 5 are <expression>s. 3, 1 and 5  are <number>s which are <literal>s which are <constant>s which are <expression>s. So that’s valid.
b. (lambda x (+ x 2)) that isn’t valid because a <abstraction> needs parenthisis after lambda.
c. (((a ((b))) c)) that looks like an <application> let’s check if ((a ((b))) c) is an <expression>. Because an <application> is an <expression> we have to check if <em>(a ((b)))</em> and <em>c</em> is an <expression>. c is an <name> and therefore an <expression>. <em>(a ((b)))</em> is an <application> which have to consists of one or more expressions. So there’s <em>a ((b))</em> left to check. a is an <expression>. ((b)) is an <application> of an <application> of a <name> which is an <expression>. So that’s valid.
d. (lambda (lambda) 3). We have to check if lambda ist a <name> which is false. Not valid.
e. (lambda () lambda). We have to check if lambda is an <expression> which it isn’t.
f. (lambda (x) (if (> x 0) x (- x) 0)). Let’s check if <em>if</em> is an <conditional>. It isn’t because it has more than three <expression>s in its body.
g. (lambda () x). This is valid if x is an <expression>. A <name> is an <expression>, so this is valid.
h. (lambda () ). Not valid, because there’s no <expression> in the body.
i. (/). Not valid, because it needs at least two <arithmetic-expression>s in the body.
j. (#t #f). This is an <application> of two expressions. Are #f and #t <expression>s?  They aren’t definied, so this is not valid.

Exercise 10.31: Use EBNF to write a grammar for the language of all strings of one or more digits that simultaneously meet both of the following requirements:
a. The digits alternate between even and odd, starting with either.

b. The string of digits is the same backward as forward (i.e., is palindromic).

Solution:

```<even> -> 0 | 2 | 4 | 6 | 8
<odd>  -> 1 | 3 | 5 | 7 | 9
<palindrome-even> -> 0<palindrome-odd>0 | 2<palindrome-odd>2 | 4<palindrome-odd>4 | 6<palindrome-odd>6 |
8<palindrome-odd>8 | <even>
<palindrome-odd> ->  1<palindrome-even>1 | 3<palindrome-even>3 | 5<palindrome-even>5 | 7<palindrome-even>7 |
9<palindrome-even>9 | <odd>
<legal> -> <palindrome-even> | <palindrome-odd>```

# SPOJ: 8132. Street Trees

A group of trees is planted along a straight line.

KOI is planning to plant more trees so that the distance between two adjacent trees is equal for all trees.

For simplicity, each tree can only be planted on an integer coordinate.

Solution:
To illustrate this problem I drew this small graphic:

The large trees are given and we have to find the small ones. You can see pretty fast that the maximum difference between trees is the minimum difference between already planted trees. But we’re not only looking for one arbitrary solution (which would be zero difference), we’re looking for the greatest difference or more generally the Greatest common divisor.

` Python |  copy code |? 01def gcd(a, b):02    if b == 0:03        return a04    else:05        return gcd(b, a % b)06 07 08def gcdList(lst):09    myGcd = gcd(lst[0], lst[1])10    for c in lst[1:]:11        myGcd = gcd(myGcd, c)12 13    return myGcd14 15N = int(raw_input())16diff = []17lastTree = int(raw_input())18for i in xrange(1, N):19    newTree = int(raw_input())20    diff.append(newTree - lastTree)21    lastTree = newTree22 23myGcd = gcdList(diff)24 25trees = map(lambda x: (x / myGcd) - 1, diff)26print sum(trees)27`

# Concrete Abstractions: Chapter 9

Exercise 9.2: The sequences we just described are restricted to consecutive increasing sequences of integers (more precisely, to increasing arithmetic sequences where consecutive elements differ by 1). We can easily imagine similar but more general sequences such as <6, 4, 3, 2> or <5, 5.1, 5.2, 5.3, 5.4, 5.5>—in other words, general arithmetic sequences of a given length, starting value, and increment (with decreasing sequences having a negative increment value).
a. Write a procedure sequence-with-from-by that takes as arguments a length, a starting value, and an increment and returns the corresponding arithmetic sequence. Thus, (sequence-with-from-by 5 6 -1) would return the first and (sequence-with-from-by 6 5 .1) would return the second of the two pre- ceding sequences. Remember that sequences are represented as procedures, so your new sequence constructor will need to produce a procedural result.
b. The procedure sequence-from-to can now be rewritten as a simple call to sequence-with-from-by. The original sequence-from-to procedure made an empty sequence if its first argument was greater than its second, but you should make the new version so that you can get both increasing and de- creasing sequences of consecutive integers. Thus, (sequence-from-to 3 8) should return <3, 4, 5, 6, 7, 8>, whereas (sequence-from-to 5 1) should return <5, 4, 3, 2, 1>.
c. Write a procedure sequence-from-to-with that takes a starting value, an ending value, and a length and returns the corresponding arithmetic sequence. For example, (sequence-from-to-with 5 11 4) should return <5, 7, 9, 11>.

Solution:

` Scheme |  copy code |? 01; a.02(define sequence-with-from-by03  (lambda (start len inc)04    (lambda (op)05      (cond ((equal? op 'empty-sequence?)06            (= len 0))07            ((equal? op 'head)08             start)09            ((equal? op 'tail)10             (sequence-with-from-by (+ start inc) (- len 1) inc))11            ((equal? op 'sequence-length)12             len)13            (else14             (error "illegal operation" op))))))15 16; b.17(define sequence-from-to18  (lambda (a b)19    (if (< a b)20        (sequence-with-from-by a (+ (- b a) 1)  1)21        (sequence-with-from-by a (+ (- a b) 1) (- 1)))))22 23; c.24(define sequence-from-to-with25  (lambda (start end len)26    (sequence-with-from-by start len (/ (+ (- end start) 2) len))))`

Exercise 9.6: Write the sequence constructor sequence-map, that outwardly acts like the list procedure map. However unlike map, which applies the procedural argument to all the list elements, sequence-map should not apply the procedural argument at all yet. Instead, when an element of the resulting sequence (such as its head) is accessed, that is when the procedural argument should be applied.

Solution:

` Scheme |  copy code |? 01(define sequence-map02  (lambda (sequence f)03      (lambda (op)04        (cond05         ((equal? op 'empty-sequence?)06          (empty-sequence? sequence)) 07         ((equal? op 'head)08          (f (head sequence))) 09         ((equal? op 'tail)10          (sequence-map (tail sequence) f)) 11         ((equal? op 'sequence-length)12          new-length)13         ((equal? op 'sequence-ref) 14          (lambda (n)15           (if (= n 0) 16            head17            (sequence-ref tail (- n 1))))) 18         (else (error "illegal sequence operation" op))))))`

Exercise 9.: One way we can represent a set is as a predicate (i.e., a procedure that returns true or false). The idea is that to test whether a particular item is in the set, we pass it to the procedure, which provides the answer. For example, using this representation, the built-in procedure number? could be used to represent the (infinite) set of all numbers.
a. Implement element-of-set? for this representation. It takes two arguments, an element and a set, and returns true or false depending on whether the element is in the set or not.
b. Implement add-to-set for this representation. It takes two arguments, an ele- ment and a set, and returns a new set that contains the specified element as well as everything the specified set contained. Hint: Remember that a set is represented as a procedure.
c. Implement union-set for this representation. It takes two arguments—two sets— and returns a new set that contains anything that either of the provided sets contains.

Solution:

` Scheme |  copy code |? 01; a02(define element-of-set?03  (lambda (element set)04    (set element)))05 06; b.07(define add-to-set08  (lambda (element set)09    (lambda (ele)10      (if (equal? ele element)11          #t12          (set ele)))))13 14; c.15(define union-set16  (lambda (set1 set2)17    (lambda (ele)18      (or (set1 ele) (set2 ele)))))19`

Exercise 9.: Assume that infinity has been defined as a special number that is greater than all normal (finite) numbers and that when added to any finite number or to itself, it yields itself. Now there is no reason why sequences need to be of finite length. Write a constructor for some interesting kind of infinite sequence.

Solution:

` Scheme |  copy code |? 01; Ex 9.2202(define sequence-from-to-inf03  (lambda (from)04    (lambda (op)05      (cond ((equal? op 'head)06             from)07            ((equal? op 'tail)08             (sequence-from-to-inf (+ 1 from)))09            ((equal? op 'sequence-length)10             +inf.0)11            (else12             (error "operation not found" op))))))13 14 15 16; Interesting application17(define display-even-sequence18  (lambda (seq)19    (if (even? (head seq))20        (display (head seq))21        (display " "))22    (display-even-sequence (tail seq))))23 24 25(define display-f-sequence26  (lambda (seq f)27    (display (f (head seq)))28    (display " ")29    (display-f-sequence (tail seq) f)))`

# Concrete Abstractions: Chapter 8

Exercise 8.1: Write a procedure called minimum that will find the smallest element in a nonempty binary search tree of numbers.

Solution:

` Scheme |  copy code |? 1(define minimum2  (lambda (tree)3    (if (empty-tree? (left-subtree tree))4        (root tree)5        (minimum (left-subtree tree)))))`

Exercise 8.2: Write a procedure called number-of-nodes that will count the number of elements in a binary search tree.

Solution:

` Scheme |  copy code |? 1(define number-of-nodes2  (lambda (tree)3    (if (empty-tree? tree)4        05        (+ 1 6           (number-of-nodes (left-subtree tree))7           (number-of-nodes (right-subtree tree))))))`

Exercise 8.6: Suppose we want to create a new binary search tree by adding another element to an already existing binary search tree. Where is the easiest place to add such an element? Write a procedure called insert that takes a number and a binary search tree of numbers and returns a new binary search tree whose elements consist of the given number together with all of the elements of the binary search tree. You may assume that the given number isn’t already in the tree.

Solution:

` Scheme |  copy code |? 01(define insert02  (lambda (tree x)03    (if (empty-tree? tree)04        (make-nonempty-tree x '() '())05    (cond ((< x (root tree))06           (make-nonempty-tree 07            (root tree) (insert (left-subtree tree) x) (right-subtree tree)))08          ((> x (root tree))09           (make-nonempty-tree10            (root tree ) (left-subtree tree) (insert (right-subtree tree) x)))))))`

Exercise 8.7: Using the procedure insert, write a procedure called list->bstree that takes a list of numbers and returns a binary tree whose elements are those numbers. Try this on several different lists and draw the corresponding tree diagrams. What kind of list gives you a short bushy tree? What kind of list gives a tall skinny tree?

Solution:

` Scheme |  copy code |? 1(define list->bstree2  (lambda (lst)3    (if (null? lst)4        (make-empty-tree)5        (insert (list->bstree (cdr lst)) (car lst)))))`

Exercise 8.11: In many applications, binary trees aren’t sufficient because we need more than two subtrees. An m-ary tree is a tree that is either empty or has a root and m subtrees, each of which is an m-ary tree. Generalize the previous results to m-ary trees.

Solution:
Ok. So let’s see if we can find a formular for 3-ary trees first.

So we can see that a tree with an height of two has $3^2 + 3^1 + 3^0$ nodes. Or more general: A 3-ary tree with an height of n has $sum_{i=0}^{n} 3^i$ nodes. This can be simplified as $frac{1 - 3^{n+1}}{1-3}.$ So is this true? Let’s see. The height should be n. What’s happening if we increase the height by one? Each leave will get three children. We assumed that there are $3^n$ leaves. Therefore there will be $3^n * 3 = 3^{n+1}$ children. That is, there will be at most $text{nodes}(n+1) = text{nodes}(n) + 3^{n+1}$ nodes which is equivalent to $sum_{i=0}^{n} 3^i + 3^{n+1} = sum_{i=0}^{n+1} 3^i.$
Therefore there are $sum_{i=0}^{n} m^i$ nodes in a m-ary of height n.

Exercise 8.12: In Exercise 8.7, you wrote a procedure list->bstree that created a binary search tree from a list by successively inserting the elements into the tree. This procedure can lead to trees that are far from minimum height—surprisingly, the worst case occurs if the list is in sorted order. However, if you know the list is already in sorted order, you can do much better: Write a procedure sorted-list->min-height-bstree that creates a minimum height binary search tree from a sorted list of numbers. Hint: If the list has more than one element, split it into three parts: the middle element, the elements before the middle element, and the elements after. Construct the whole tree by making the appropriate recursive calls on these sublists and combining the results.

Solution:

` Scheme |  copy code |? 01(define sorted-list->min-height-bstree02  (lambda (lst)03    (if (null? lst)04        (make-empty-tree)05        (if (not (list? lst))06            lst07            (make-nonempty-tree (middle-item lst) 08                                (sorted-list->min-height-bstree (left-items lst)) 09                                (sorted-list->min-height-bstree (right-items lst)))))))10 11 12(define list-element-range13  (lambda (lst start end i)14    (if (null? lst)15        '()16        (cond ((> i end)17               '())18              ((= i end)19               (cons (car lst) (list-element-range '() start end (+ i 1))))20              ((or (= i start) (> i start))21               (cons (car lst) (list-element-range (cdr lst) start end (+ i 1))))22              (else23               (list-element-range (cdr lst) start end (+ i 1)))))))24 25(define middle-item26  (lambda (lst)27    (let ((len (length lst)))28      (if (even? len)29        (list-element-range lst (/ len 2) (/ len 2) 0)30        (list-element-range lst (- (/ (+ len 1) 2) 1) (- (/ (+ len 1) 2) 1) 0)))))31 32(define left-items33  (lambda (lst)34    (let ((len (length lst)))35          (if (even? len)36              (list-element-range lst 0 (- (/ len 2) 1) 0)37              (list-element-range lst 0 (- (/ (+ len 1) 2) 2) 0)))))38 39(define right-items40  (lambda (lst)41    (let ((len (length lst)))42          (if (even? len)43              (list-element-range lst (+ 1 (/ len 2)) (+ len 1) 0)44              (list-element-range lst (/ (+ len 1) 2) len 0)))))`

Exercise 8.31: Write a procedure that takes as arguments a binary search tree of numbers, a lower bound, and an upper bound and counts how many elements of the tree are greater than or equal to the lower bound and less than or equal to the upper bound. Assume that the tree may contain duplicate elements. Make sure your procedure doesn’t examine more of the tree than is necessary.

Solution:

` Scheme |  copy code |? 01(define count-elements-in-boundary02   (lambda (tree lower upper)03     (if (empty-tree? tree)04         005         (+ 06          (if (or (< (root tree) lower) (> (root tree) upper))07             008             1) 09          (count-elements-in-boundary (left-subtree tree) lower upper)10          (count-elements-in-boundary (right-subtree tree) lower upper)))))11     `

# #85/111: Machine Learning

The book

This is probably one of the standard intro texts into machine learning. Tom Mitchell covers most of the basic techniques in machine learning (ToC) but doesn’t cover all of them, e.g. SVMs. I got a bit of background in statistics so it was rather easy to dive into machine learning although their terminology is a mostly different from statistics.

If you don’t have a background in statistics but solid basics in calculus then it should be rather easy to understand the contents of this book. There are lots of exercises which help you to strengthen your understanding. I think it’s an ideal theoretical basis for Programming Collective Intelligence. All in all, a really nice book if you are interested in machine learning.

# SPOJ: 1728. Common Permutation

Given two strings of lowercase letters, a and b, print the longest string x of lowercase letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.

Solution: That’s short and nice problem. You have to find all letters which are in both strings. I’d actually like to see some other implementations of this. I bet there are some languages which handle this in a smart way.

` Python |  copy code |? 01def intersection(lst1, lst2):02    same = []03    if len(lst1) > len(lst2):04        for k in lst1:05            try: # checks if element k is in lst206                lst2.remove(k)07                same.append(k)08            except:09                pass10    else:11        for k in lst2:12            try:13                lst1.remove(k)14                same.append(k)15            except:16                pass17    same.sort()18    return same19 20 21while True:22    try:23        inp1 = raw_input()24        inp2 = raw_input()25 26        common = intersection(list(inp1), list(inp2))27        print "".join(common)28    except:29        break30`