Chapter 7: Repeating Things (Solutions to Even-Numbered Exercises)

Question 2

a)

>>> if rat_1[0] > rat_2[0]:
...     print "Rat 1 weighed more than Rat 2 on Day 1."
... else:
...     print "Rat 1 weighed less than Rat 2 on Day 1."
...

b)

>>> if rat_1[0] > rat_2[0] and rat_1[-1] > rat_2[-1]:
...     print "Rat 1 remained heaver than Rat 2."
... else:
...     print "Rat 2 became heavier than Rat 1."

c)

>>> if rat_1[0] > rat_2[0]:
...     if rat_1[-1] > rat_2[-1]:
...         print "Rat 1 remained heaver than Rat 2."
...     else:
...         print "Rat 2 became heavier than Rat 1."

Question 4

You could do it this way:

>>> for i in range(10, 0, -1):
...     print i,
... 
10 9 8 7 6 5 4 3 2 1

Or this:

>>> i = 10
>>> while i != 0:
...     print i,
...     i -= 1
... 
10 9 8 7 6 5 4 3 2 1

Question 6

def remove_neg(num_list):
    '''Remove the negative numbers from the list num_list.'''

    i = 0
    while i < len(num_list):
        if num_list[i] < 0:
            num_list.pop(i)
        else:
            i += 1

Question 8

for i in range(7):
    blanks = ''
    ts = ''
    
    for j in range(7 - i):
        blanks += ' '
    
    for j in range(i + 1):
        ts += 'T'
    
    print blanks + ts

Question 10

a)

rat_1_weight = 10
rat_2_weight = 9
rat_1_rate = 0.04
rat_2_rate = 0.04

rat_1_target = 1.25 + rat_1_weight
weeks = 0
while rat_1_weight < rat_1_target:
    rat_1_weight += rat_1_rate * rat_1_weight
    weeks += 1
print weeks

b)

rat_1_weight = 10
rat_2_weight = 10
rat_1_rate = 0.07
rat_2_rate = 0.04

weeks = 0
while rat_1_weight < rat_2_weight * 1.10:
    rat_1_weight += rat_1_rate * rat_1_weight
    rat_2_weight += rat_2_rate * rat_2_weight
    weeks += 1
print weeks

Question 12

a)

import media

def swap_pixels(pic, x1, y1, x2, y2):
    '''Exchange the colors of the pixel (x2, y2) and the color of pixel
    (x1, y1) in picture pic.'''
    
    pixel_1 = media.get_pixel(pic, x1, y1)
    pixel_2 = media.get_pixel(pic, x2, y2)
    color_1 = media.get_color(pixel_1)
    color_2 = media.get_color(pixel_2)
    media.set_color(pixel_2, color_1)
    media.set_color(pixel_1, color_2)

def mirror(pic):
    '''Reflect the pixels in Picture pic as if it were held in front of a
    mirror.'''

    width = media.get_width(pic)
    height = media.get_height(pic)
    middle = width / 2

    # Only go halfway so that we don't swap each pair of pixels twice.
    for x in range(middle):
        for y in range(height):
            swap_pixels(pic, x, y, width - 1 - x, y)


if __name__ == '__main__':
    pic = media.load_picture(media.choose_file())
    media.show(pic)
    mirror(pic)
    media.show(pic)

b)

import media

def swap_pixels(pic, x1, y1, x2, y2):
    '''Exchange the colours of the pixel (x2, y2) and the colour of pixel
    (x1, y1) in picture pic.'''
    
    pixel_1 = media.get_pixel(pic, x1, y1)
    pixel_2 = media.get_pixel(pic, x2, y2)
    color_1 = media.get_color(pixel_1)
    color_2 = media.get_color(pixel_2)
    media.set_color(pixel_2, color_1)
    media.set_color(pixel_1, color_2)

def mirror(pic):
    '''Reflect the pixels in Picture pic as if it were held in front of a
    mirror.'''

    width = media.get_width(pic)
    height = media.get_height(pic)
    middle = height / 2

    # Only go halfway so that we don't swap each pair of pixels twice.
    for x in range(width):
        for y in range(middle):
            swap_pixels(pic, x, y, x, height - 1 - y)


if __name__ == '__main__':
    pic = media.load_picture(media.choose_file())
    media.show(pic)
    mirror(pic)
    media.show(pic)

Question 14

import media

def set_average_square_color(pic, x, y):
    '''Set the colors of all pixels of the 10 by 10 square with (x, y) as the
    upper-left corner to the average of the colors of those pixels.'''

    red_total = 0
    green_total = 0
    blue_total = 0

    for width in range(10):
        for height in range(10):
            pixel = media.get_pixel(pic, x + width, y + height)
            red_total += media.get_red(pixel)
            green_total += media.get_green(pixel)
            blue_total += media.get_blue(pixel)

    red_avg = red_total / 100
    green_avg = green_total / 100
    blue_avg = blue_total / 100
    avg_color = media.Color(red_avg, green_avg, blue_avg)

    for width in range(10):
        for height in range(10):
            pixel = media.get_pixel(pic, x + width, y + height)
            media.set_color(pixel, avg_color)

def mosaic(pic):
    '''Set the colors of each 10 by 10 block of pixels to the average color
    of that block.'''
    
    x = 0

    while x < media.get_width(pic):
        y = 0
        while y < media.get_height(pic):
            set_average_square_color(pic, x, y)
            y += 10

        x += 10
        media.update(pic)

if __name__ == '__main__':
    pic = media.load_picture(media.choose_file())
    media.show(pic)
    mosaic(pic)
    media.show(pic)