Wednesday 5 December 2012

Week 11-The beautiful snowflack


The best part that I like the course is to manipulate pictures. Based on the define of Koch, we can define Koch-snowflake as:
(define (koch-snowflake d)
  (above
   (beside
    (rotate 60 (koch d))
    (rotate -60 (koch d))
    (rotate 180 (koch d))))


Week 9-Palindrome


I found it difficult to understand the idea of palindrome. To checkout whether a string is a palindrome, we can simply write the define as below:

(define (palindrome? s)
  (cond
    [(equal? (string-length s) 1) true]
    [else
     (and (equal? (substring s 0 1)
                  (substring s (sub1 (string-length s))))
          (palindrome? (substring s 1 (sub1 (string-length s)))))]))

However, I cannot understand the “define” at first. I understand the first condition [(equal? (string-length s) 1) true] is to check if the string has only one character. One character is always a palindrome. For longer string, we should check the second condition. It is basically check the first and last character, second and second last and etc. is the same. However, it has another (palindrome? (substring s 1 (sub1 (string-length s)))) in the second condition.
I then realized that the palindrome inside the second condition is to check the second and second last characters in the string ect.  

Week 8-ROT13 on DrRocket


There are many codes that exist, some are well known and hard to decode, like Morse code, and some is relatively easy, like rot13. Rot 13 is to convert a character to another. For example, A can be converted to N. The definition can be written in DrRocket below:

(define (rot13 c)
  (cond
    [(char<=? #\A c #\M) (integer->char (+ 13 (char->integer c)))]
    [(char<=? #\a c #\m) (integer->char (+ 13 (char->integer c)))]
    [(char<=? #\N c #\Z) (integer->char (- (char->integer c) 13))]
    [(char<=? #\n c #\z) (integer->char (- (char->integer c) 13))]
    [else c]))

Therefore, “I Love You!” can be translated into rot13 language "V Ybir Lbh!"

Week 7-GCD on DrRocket


GCD is the faster way to find the common demonstrator. Greatest Common Denominator (GCD) is developed by Euclid, and it can be written in DrRocket as

(define (GCD m n)
  (cond
    [(zero? n) m]
    [else (GCD n (remainder m n))]))

For example, (GCD 35 15)-> (GCD 15 5)->(GCD 5 0) then the common denominator is 5.

Week 6-Algorithm and Problem-solving


Algorithm is to use simple sequence of feasible steps to solve a problem. It can be used to deal with many problems in programming area. For example, we can use algorithm to deal with the problem of the clock that we made on earlier classes.
First, we should understand the problem. The clock is not working properly because the second hand is moving slight faster than normal. Therefore, we should develop a plan before really solving it. For example, make the second hand speed to be (/ 360 60). Third, correct the second hand on DrRocket. Finally, run the program and check out if the program works properly.