76
3 Loops and Branching
Exercise 3.9: Simple Search: Verification
Check your understanding of the search procedure in ball_max_height.py from
Sect. 3.3.3 by comparing what you get “by hand” to printouts from the code. Work
on a copy of ball_max_height.py. Comment out what you do not need, and use
an array y of just 4 elements (or so). Fill that array with integers, so that you place
a maximum value in a certain location. Then, run through that code by hand for
every iteration of the loop, writing down the numbers in largest_height. Finally,
place a print command in the loop, so that largest_height gets printed with every
iteration. Run the program and compare to what you found by hand.
Filename: simple_search_verify.py.
Exercise 3.10: Sort Array with Numbers
Write a script that uses the uniform function from the random module to generate
an array of 6 random numbers between 0 and 10.
The program should then sort the array so that numbers appear in increasing
order. Let the program make a formatted print of the array to screen both before and
after sorting. Confirm that the array has been sorted correctly.
Filename: sort_numbers.py.
Exercise 3.11: Compute π
Up through history, great minds have developed different computational schemes
for the number π. We will here consider two such schemes, one by Leibniz (1646–
1716), and one by Euler (1707–1783).
The scheme by Leibniz may be written
π = 8
∞
k=0
1
(4k + 1)(4k + 3)
,
while one form of the Euler scheme may appear as
π =
6
∞
k=1
1
k 2 .
If only the first N terms of each sum are used as an approximation to π, each
modified scheme will have computed π with some error.
Write a program that takes N as input from the user, and plots the error
development with both schemes as the number of iterations approaches N. Your
program should also print out the final error achieved with both schemes, i.e. when
the number of terms is N. Run the program with N = 100 and explain briefly what
the graphs show.
Filename: compute_pi.py.
3 Loops and Branching
Exercise 3.9: Simple Search: Verification
Check your understanding of the search procedure in ball_max_height.py from
Sect. 3.3.3 by comparing what you get “by hand” to printouts from the code. Work
on a copy of ball_max_height.py. Comment out what you do not need, and use
an array y of just 4 elements (or so). Fill that array with integers, so that you place
a maximum value in a certain location. Then, run through that code by hand for
every iteration of the loop, writing down the numbers in largest_height. Finally,
place a print command in the loop, so that largest_height gets printed with every
iteration. Run the program and compare to what you found by hand.
Filename: simple_search_verify.py.
Exercise 3.10: Sort Array with Numbers
Write a script that uses the uniform function from the random module to generate
an array of 6 random numbers between 0 and 10.
The program should then sort the array so that numbers appear in increasing
order. Let the program make a formatted print of the array to screen both before and
after sorting. Confirm that the array has been sorted correctly.
Filename: sort_numbers.py.
Exercise 3.11: Compute π
Up through history, great minds have developed different computational schemes
for the number π. We will here consider two such schemes, one by Leibniz (1646–
1716), and one by Euler (1707–1783).
The scheme by Leibniz may be written
π = 8
∞
k=0
1
(4k + 1)(4k + 3)
,
while one form of the Euler scheme may appear as
π =
6
∞
k=1
1
k 2 .
If only the first N terms of each sum are used as an approximation to π, each
modified scheme will have computed π with some error.
Write a program that takes N as input from the user, and plots the error
development with both schemes as the number of iterations approaches N. Your
program should also print out the final error achieved with both schemes, i.e. when
the number of terms is N. Run the program with N = 100 and explain briefly what
the graphs show.
Filename: compute_pi.py.
