Exercises from chapter 3 – Think Python

Let us play with the concepts from chapter 3.

Exercise 3.3) p. 29

Python provides a built-in function called len that returns the length of a string, so the value of len(‘allen’) is 5.
Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.

>>> right_justify('allen')
                                                   allen
Here is the answer:
>>> def right_justify(s):
…     print (70 – len(s)) * ” ” + s

>>> right_justify(‘allen’)
allen
>>>
Logic:

I knew that:
70 = padding (blank characters) + s
Following the same logic, the padding would be equal to 70 – s.

****

Exercise 3.4) p. 29

A function object is a value you can assign to a variable or pass as an argument.

You can go back to the first part of Chapter 3 in order to recall the concept.

Exercise 3.4) p. 29

A function object is a value you can assign to a variable or pass as an argument. For example, do_twice is a function that takes a function object as an argument and calls it twice:

def do_twice(f):        # The function 'do_twice' uses the parameter 'f'.
    f()                 # If parameter 'f' happens to be a function, then it will be called twice. 
    f()

Here’s an example that uses do_twice to call a function named print_spam twice.

def print_spam():
    print 'spam'

do_twice(print_spam)
Remember about “nested functions” and the metaphor of the Matryoshka, here is the perfect example:
Questions:
  1. Type this example into a script and test it:
    >>>
    def do_twice(f):

    …     f()
    …     f()

    >>> def print_spam():
    …     print ‘spam’

    >>> do_twice(print_spam)
    spam
    spam
    >>>

  2. Modify do_twice so that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument.

    >>>def do_twice (f, bingo):        # The function ‘do_twice’ has two parameters. ‘f’ is a function object and ‘bingo’ is a parameter referring to a particular value.
    …    f (bingo)                                    # Here we will call function ‘f’ twice with the variable ‘bingo’ as an argument.
    …    f (bingo)

  3. Write a more general version of print_spam, called print_twice, that takes a string as a parameter and prints it twice.
    >>> def print_twice(banana):
    …     print banana
    …     print banana

    >>>
  4. Use the modified version of do_twice to call print_twice twice, passing ‘spam’ as an argument.
    >>> def do_twice (f, bingo):
    …     f(bingo)
    …     f(bingo)

    >>>
    >>> def print_twice (banana):
    …     print banana
    …     print banana

    >>>
    >>> do_twice (print_twice,’spam’)
    spam
    spam
    spam
    spam
    >>>
  5. Define a new function called do_four that takes a function object and a value and calls the function four times, passing the value as a parameter. There should be only two statements in the body of this function, not four.
    Working on what we already defined as a function, I will use:
    >>> def print_twice(banana):
    …     print banana
    …     print banana

    >>>
    >>> def do_four (g, cat):
    …     g(cat)
    …     g(cat)

    >>>
    >>> do_four(print_twice, ‘Eureka’)
    Eureka
    Eureka
    Eureka
    Eureka
    >>>
    NOTE: My answer does not match the solution provided by the link below. So, I still have to double-check it.

Solution: http://thinkpython.com/code/do_four.py

Exercise 3.5) p.29-30

This exercise can be done using only the statements and other features we have learned so far.

1. Write a function that draws a grid like the following:

Exercise 3-5 Think Python

Exercise 3-5 Think Python

Hint: to print more than one value on a line, you can print a comma-separated sequence:

print '+', '-'

If the sequence ends with a comma, Python leaves the line unfinished, so the value printed next appears on the same line.

print '+',
print '-'

The output of these statements is ‘+ -‘.
A print statement all by itself ends the current line and goes to the next line.

Write a function that draws a similar grid with four rows and four columns.

So, here are the early stages of me coding:

Step 1) Write the code in Sublime Text :

x = ‘+’
y = ‘ -‘
w = ‘ ‘
z = ‘|’

f = (x + 4*y + w) * 2 + x
g = (z + 9*w)*2 + z

def print_twice (banana):
print banana
print banana

def do_twice (f, apple):
f(apple)
f(apple)

def box ():
print f
do_twice(print_twice,g)
print f
do_twice(print_twice,g)
print f

box()

Step 2) Save the code under small_box.pyStep 3) Run the program in the Terminal :

Here is the grid:

 

2. Write a function that draws a similar grid with four rows and four columns.

Step 1) Write the code in Sublime Text :

x = ‘+’
y = ‘ -‘
w = ‘ ‘
z = ‘|’

f = (x + 4*y + w) * 4 + x
g = (z + 9*w)*4 + z

def print_twice (banana):
print banana
print banana

def do_twice (f, apple):
f(apple)
f(apple)

def box ():
print f
do_twice(print_twice,g)
print f
do_twice(print_twice,g)

def big_box():
box()
box()
print f

big_box()

Step 2) Save the code under big_box.py

Step 3) Run the program in the Terminal :

Here is the grid:

 

Solution: http: // thinkpython. com/ code/ grid. py.

Credit: This exercise is based on an exercise in Oualline, Practical C Programming, Third Edition, O’Reilly Media, 1997.

Problems and Solutions

Problems and Solutions

One thought on “Exercises from chapter 3 – Think Python

  1. I have this solution is it acceptable ?
    #grid.py
    def one_g():
    print (“+ – – – – “)+(“+ – – – -“),’+’
    def pole():
    print ‘|’,(7*’ ‘),’|’,(7*’ ‘),’|’
    one_g()
    pole()
    pole()
    pole()
    pole()
    one_g()
    pole()
    pole()
    pole()
    pole()
    one_g()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s