Wednesday, 20 February 2013

Pattern


Assignment Patterns

The art of writing programs lies in the ability to recognize and use patterns that have appeared since the very first programs were written.

The Transfer Pattern

The transfer pattern is used to change the value of a variable based upon the value of another variable. Suppose we have a variable named alpha which is initialized to 3 and a variable beta which is initialized to 10:
    alpha = 3
    beta = 10
Now consider the statement:
    alpha = beta
This statement is read like this: make the new value of alpha equal to the value of beta, throwing away the old value of alpha. What is the value of alpha after that statement is executed? Highlight the following line to see the answer:
The new value of alpha is 10.
The transfer pattern tells us that value of beta is imprinted on alpha at the moment of assignment but in no case are alpha and beta conjoined in anyway in the future. 
A graphical way to see this is to use the idea of a pointer. When a variable is assigned a value, we will say that variable points to the value. For the variables:
   alpha = 3
   beta = 10
Now when we assign alpha the value of beta:
    alpha = beta
we cross out alpha's pointer and replace it with a pointer to whatever beta points:
Note that we never point a variable to another variable; we only point variables to values.
    alpha = 4
    beta = 13
    alpha = beta
    beta = 5
What are the final values of alpha and beta? Highlight the following line to see the answer:
The value of alpha is 3 and the value of beta is 5.

Lets check what happens if the following code is executed:
    alpha = 4
    beta = 13
    alpha = beta
    alpha = 42
The final values of alpha and beta  are
The value of alpha is 42and the value of beta is 13.

The Update Pattern

The update pattern is used to change the value of a variable based upon the original value of the variable. Suppose we have a variable named counter which is initialized to zero:
    counter = 0
Now consider the statement:
    counter = counter + 1
This statement is read like this: make the new value of counter equal to the old value of counter plus one. Since the old value is zero, the new value is one. Consider this sequence:
    counter = 0
    counter = counter + 1
    counter = counter + 1
    counter = counter + 1
    counter = counter + 1
    counter = counter + 1
The value of counter after the following code is executed:
The value of counter is 5.
There is another form of this update:
    counter = 0
    counter += 5
The operator += says to update the variable on the left by adding in the value on the right to the current value of the variable.
The update pattern can be used to sum a number of variables. Suppose we wish to compute the sum of the variables abcd, and e. The obvious way to do this is with one statement:
    sum = a + b + c + d + e
However, we can use the update pattern as well:
    sum = 0
    sum = sum + a
    sum = sum + b
    sum = sum + c
    sum = sum + d
    sum = sum + e
If a is 1, b is 2, c is 3, d is 4, and e is 5, then the value of sum in both cases is 15.  we use the update pattern for computing a sum when the first version is so much more compact and readable because we'll have to wait until we cover a programming concept called a loop. With loops, the update pattern is almost always used to compute sums, products, etc.

The Throw-away Pattern

The throw-away pattern is a mistaken attempt to use the update pattern. In the update pattern, we use the original value of the variable to compute the new value of the variable. Here again is the classic example of incrementing a counter:
    counter = counter + 1
In the throw-away pattern, the new value is computed but it the variable is not reassigned, nor is the new value stored anywhere. Many novice programmers attempt to update a counter simply by computing the new value:
    count + 1       # throw-away!
Python does all the work to compute the new value, but since the new value is not assigned to any variable, the new value is thrown away.

The Throw-away Pattern and Functions

The throw-away pattern applies to function calls as well. We haven't discussed functions much, but the following example is easy enough to understand. First we define a function that computes some value:
    def inc(x):
        return x + 1
This function returns a value one greater than the value given to it, but what the function actually does is irrelevant to this discussion. That said, we want to start indoctrinating you on the use of functions. Repeat this ten times:
We always do four things with functions: define themcall themreturn something, and save the return value.
To call the function, we use the function name followed by a set of parentheses. Inside the parentheses, we place the value we wish to send to the function. Consider this code, which includes a call to the function inc:
    y = 4
    y = inc(y)
    print("y is",y)
If we were to run this code, we would see the following output:
    y is 5
The value of y, 4, is sent to the function which adds one to the given value and returns this new value. This new value, 5, is assigned back to y. Thus we see thaty has a new value of 5.
Suppose, we run the following code instead:
    y = 4
    inc(y)
    print("y is",y)
Note that the return value of the function inc is not assigned to any variable. Therefore, the return value is thrown away and the output becomes:
    y is 4
The variable y is unchanged because it was never reassigned.

No comments:

Post a Comment