Sunday 2 December 2012

Assignment #2

For our second assignment, we were given instructions to make adjustments to a code which produced an image between two pictures, that could "dissolve" into either picture.


The most challenging aspect of this assignment was writing the check-expect for a function called interpolate-image. interpolate-image takes two images and a number and produces an image that is an exact intermediate between the two images' colours and opacities. The definition of interpolate-image is as follows:

; interpolate-image : image image number -> image
; Produce image interpolated n/DENOM between
; i1 and i2
; !!! write two check-expect expressions for interpolate-image, one
;     for when n = DENOM, the other for whem n = NUMER


(define (interpolate-image i1 i2 n)
  (local ; f is defined only inside interpolate-image
    [(define (f c1 c2) (interpolate-color (/ n DENOM) c1 c2))]
    (map-images f i1 i2)))



I was initially unfamiliar with the "local" clause and so this created a bit of confusion for me. Luckily, Professor Heap was available during office hours and helped me understand what the function was trying to do:
You should know that interpolate-color takes in a fraction and two colours to produce a colour that is an interpolate of the colours, by whatever fraction was given. For example, (interpolate-color 2/3 (make-color 0 0 0 255) (make-color 6 9 12 255)) will make a colour (make-color 4 6 8 255).
By taking in a number (n), and dividing it by DENOM (20), you have the fraction that the image will be interpolated at. The check-expect tests ask to use n=NUMER=0 and n=DENOM=20. When n=NUMER, the interpolate is at a minimum and will just produce the leftmost image (p1). However, when n=DENOM, the interpolate is at a maximum and will produce the rightmost image (p2).

Therefore, the check-expects can be listed as follows:

(check-expect (interpolate-image p1 p2 DENOM) p2)
(check-expect (interpolate-image p1 p2 NUMER) p1)

Sunday 25 November 2012

Exam Preparation

By far, the most challenging thing about CSC104 is the theoretical portion on the history and applications of computers. Having written the 2nd term test not too long ago, I am now fully aware of my weak points, which are essentially most things besides DrRacket. As the exam approaches, I will be trying my hardest to attend the lectures and make useful notes. I also intend to take advantage of the available office hours and tutorials to get help from Professor Heap and my TAs. Strategies in my other courses that I plan to apply to CSC104 are making a study group with my classmates so that we can reinforce our understandings. As I have previously mentioned, Professor Heap posts extra notes on the CSC104 homepage which have proven to be quite useful when preparing for tests. I hope to combine all these strategies to do my best in the upcoming exam.

Saturday 10 November 2012

Assignment #1 Part 2

After the clarify-image issue, I ran into another problem a week later on the same assignment. This time, it was with check-key, a function that is used to stop the clock when the "down" arrow is pressed on the keyboard. We are asked to define the function check-key after being given two check-expect tests:

; check-key : clocktime string -> clocktime
; Check whether "down" pressed to stop clock

(check-expect (check-key (make-clocktime 1 2 3 GO) "up") (make-clocktime 1 2 3 GO))
(check-expect (check-key (make-clocktime 1 2 3 GO) "down") (make-clocktime 1 2 3 STOP))
; !!! fix check-key
(define (check-key c k) c)


First off, I knew that we had performed a similar function in Week 3 with the DrRacket Timer. Looking back to the notes from that day, we had made a similar check-key function that displayed an image of a STOP sign when the "down" arrow is pressed:

; check-key : image string -> image
; Check whether "down" pressed to pass STOPim

(check-expect (check-key SH "up") SH)
(check-expect (check-key SH "down") STOPim)

(define (check-key im k)
  (cond
    [(equal? k "down") STOPim]
    [else im]))

 

Going from this example, I attempted to modify the function to fit the needs of this assignment. In this case, instead of displaying an image, I would just need to change the "s/g" component of the clocktime struct (previously defined) to "STOP". Originally, I had approached this problem by doing the following (note that "equal?" and "key=?" are synonymous in these cases):
(define (check-key c k)
  (cond
    [(key=? k "down")
     (clocktime-s/g STOP)]
    [else c]))


As you may have suspected, this produces an error. I made several more attempts to express a change in the s/g component:

(define (check-key c k)
  (cond
    [(key=? k "down")
     (set-clock 1 2 3 STOP)]
    [else c]))



(define (check-key c k)
  (cond
    [(key=? k "down")
     (make-clocktime-s/g STOP)]
    [else c]))
 


Eventually, I sought help from Professor Heap in his office hours. He explained that the condition expects a clocktime, represented by c. Similar to the DrRacket Timer example, (define (check-key im k) expects an image, represented by im and the function is successful because STOPim is in fact, an image! In order for the check-key in this assignment to work, I needed to create a condition statement that took the entire clocktime struct into account, instead of the s/g component alone:

; check-key : clocktime string -> clocktime
; Check whether "down" pressed to stop clock

(check-expect (check-key (make-clocktime 1 2 3 GO) "up") (make-clocktime 1 2 3 GO))
(check-expect (check-key (make-clocktime 1 2 3 GO) "down") (make-clocktime 1 2 3 STOP))
; !!! fix check-key
(define (check-key c k)
  (cond
    [(key=? k "down")
     (set-clock (clocktime-hours c) (clocktime-minutes c) (clocktime-seconds c) STOP)]
    [else c]))



This definition of check-key turned out to be functional and both check-expect tests were passed!

For your benefit, the DrRacket Timer example can be found on the CSC104 website under Week 3 at: http://www.cdf.toronto.edu/~heap/104/F12/
Also, if you are struggling with assignments like myself, consulting with the professor is not as scary as it seems. Professor Heap is extremely approachable and helpful and you can find him during office hours 5-6pm on Wednesdays!

Saturday 3 November 2012

Assignment #1

Fortunately for me, I found almost everything to be pretty straightforward this week. The biggest challenge I encountered was from our first project that is due next week. 
In DrRacket we are given instructions to complete the definition of a function, clarify-image that will take an image and make its light colours transparent. We are given the definition of clarify-color which takes the sum of the red, green and blue colour intensities of a pixel and sets their alpha intensity to 0 depending if the sum is greater than 650. Pixels with colour intensities over 650 will become absolutely transparent, otherwise, they remain unchanged. The definition of clarify-color is given as follows:

; clarify-color : color -> color
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is


(define (clarify-color c)
  (make-color
   (color-red c)
   (color-green c)
   (color-blue c)
   (cond
     [(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
     [else (color-alpha c)])))


The next portion of this code asks to write a definition for clarify-image, having been given 2 check-expects:

; clarify-image : image -> image
; Make alpha 0 for colors with r+g+b > 650, leave other colors as-is, in im
(check-expect (clarify-image (circle 10 "solid" (make-color 250 250 250 255)))
              (circle 10 "solid" (make-color 250 250 250 0)))
(check-expect (clarify-image (circle 10 "solid" (make-color 100 100 100 255)))
                             (circle 10 "solid" (make-color 100 100 100 255)))

; !!! fix the definition of clarify-image
(define (clarify-image im) im)


This was the hard part. I initially approached this question thinking "I want clarify-image to do exactly what clarify-color does, except to entire images". Copying the exact code of clarify-color and changing "c" to "im" will not work. I know this because I tried it and it failed. And while most check-expects provide clues on writing a definition, these were not very helpful. However, I recalled that during one of our lectures, Professor Heap took an image and made it less green by first defining a function to reduce the green colour and then using map-image to use that function towards an image. Map-image is important as it applies a function to all pixels of an inputted image. Following the examples Heap gave in class and combining them with my new-found knowledge of condition statements, I was able to devise a definition that carried out the role of clarify-image. At first I had created an additional function (which I later realized was redundant), do-clarify-image that would do what clarify-image is supposed to do on a pixel level. I would then use map-image to apply do-clarify-image to all the pixels in an image:


(define (do-clarify-image c)
(make-color
   (color-red c)
   (color-green c)
   (color-blue c)
   (cond
     [(> (+ (color-red c) (color-green c) (color-blue c)) 650) 0]
     [else (color-alpha c)])))


(define (clarify-image im) 
              map-image do-clarify-image im)

The check-expect tests were ran and they passed using this definition of clarify-image. It then dawned on me that do-clarify-image was uncannily similar to another function I've seen before...
Yes, that's right.
The definitions for clarify-color and do-clarify-image are exactly the same.
Therefore, you can can completely eliminate do-clarify-image and replace it with clarify-color which has already been defined! You will end up with:

(define (clarify-image im) 
              map-image clarify-color im)
Although it may seem confusing, this is the actual thought process I used to arrive at the solution!

Below are some helpful links that helped me with this part of the assignment:

 You can find the code to Professor Heap's mapping lecture on the CSC course website for Week 5 http://www.cdf.toronto.edu/~heap/104/F12/. 

Also, the Picturing Programs Teachpack is useful in explaining the purpose of map-image and make-color:
http://docs.racket-lang.org/picturing-programs/index.html

Monday 29 October 2012

Computer Hardware

This week in CSC104 I was confronted with the realization that I have lost all previous knowledge I ever had of computer hardware. Bus? ALU? Compact discs??? This sounds like a job for good ol' Wikipedia.

Time to review some definitions:

http://en.wikipedia.org/wiki/Arithmetic_logic_unit
http://en.wikipedia.org/wiki/Bus_%28computing%29
http://en.wikipedia.org/wiki/Von_Neumann_architecture


I also discovered that Professor Heap posts additional notes on the CSC website. They are extremely helpful for anyone else who is looking for further clarification!

http://www.cdf.toronto.edu/~heap/104/F12/Lectures/W7/hardware.html


Friday 19 October 2012

Paper Folding Algorithm

This week in CSC104, we were given the task of formulating an algorithm that could predict the direction of the vertices from folding in a strip of paper. The instructions were to "Take a strip of paper and stretch it so that you have one end between your left thumb and index finger and the other between your right thumb and fore finger. Fold the strip so that the left end is on top of the right end. Repeat this several times, each time folding so that the left end is on top of the right end of the strip." The assignment asked to find a sure way to predict the sequence of ups and downs for any number of times you fold the paper.

UNDERSTANDING THE PROBLEM
We want to be able to predict future sequences when the thin strip of paper is folded any number of times.
To further understand the problem, I conducted trials for a strip of paper with up to 5 folds. Their patterns and characteristics are listed below (Tables 1 and 2).

DEVISING A PLAN
A few observations:
-       The number of segments (bordered by the paper edges and vertices) is 2F.
o   S = 2F
-       The total number of vertices is one less than the number of segments.
o   V = 2F-1
-       The number of down vertices is 2(F-1).
o   D = 2(F-1)
-       The number of up vertices increases by 2(F-2) with each successive fold.
o   UF = UF-1 + 2(F-2)
-       The number of up vertices is also 1 less than the number of down vertices.
o   U = D - 1
-       Each fold has a central D.
-       For F number of folds, the sequence of F - 1 forms the right portion of F’s sequence after the central D.
o   For example, with 2 folds, the resulting sequence is UDD. For 3 folds, the resulting sequence is UUDDUDD. The UDD from the sequence before is carried over into the next, forming the right side of the sequence.
-       The left side of the central D is a toggled mirror image of the right side.
o   For example, UUDDUDD has a right side of UDD. Toggling it will replace D’s with U’s and U’s with D’s. Therefore, UDD becomes DUU. Flipped in a mirror image, it will then be UUD. Put together, you have UUDDUDD.


CARRYING OUT THE PLAN
We can now test out this method to determine the sequence of 6 folds, given the sequence for 5 folds!

1.     Carry previous sequence over to right side of central D of target sequence.

DUUDUUDDUUUDDUDDDUUDUUDDDUUDDUDD

2.     Toggle mirror image that sequence!

UUDUUDDUUUDDUDDUUUDUUDDDUUDDUDD

3.     Put it all together!

UUDUUDDUUUDDUDDUUUDUUDDDUUDDUDDDUUDUUDDUUUDDUDDDUUDUUDDDUUDDUDD

LOOKING BACK

We can double-check our prediction with our expected characteristics:
Total # of vertices = 26 – 1 = 64 – 1 = 63 = true!
Total # of downs = 26-1 = 25 = 32 = true!
Totally # of ups = 32 – 1 = 31 = true!

The sequence fits all the criteria as the previous sequences did!
And there you have the sequence for 6 folds!

Saturday 13 October 2012

Boolean Logic

As a Human Biology major with a not-so-strong computers background, the thought of taking a Computer Science course was somewhat intimidating. So far in this course (CSC104 - The How and Why of Computing), I have faced multiple challenges, especially in the programming portions. However, the one lecture topic that had really confused me was Boolean Logic.

"True or false is true and true and false is false."







But why?

To me, "true or false" meant that the answer could be either "true" or "false"- why would it automatically be true? I guess you could say I found Boolean logic to be very illogical, and when you throw NANDs and NORs into the equation it just makes everything even worse.
After hours of reading and re-reading Wikipedia and still not understanding anything, I turned to my TA for help. Amazingly, he was able to explain the whole thing to me in under 15 minutes. I realized the statements were not as horrible as I had originally envisioned them and they were in fact, pretty easy to solve. After that, I was quite pleased to see Boolean logic on the midterm!