1 print "Think of a number between 1 and 100, but don't tell me what you choose."
2 min_n = 1
3 max_n = 100
4 right_answer = False
5
6 while not right_answer:
7 mid_n = (max_n + min_n + 1)/2
8 answer = raw_input('Is it ' + str(mid_n) + '? ')
9 if answer[0] == 'y':
10 right_answer = True
11 elif answer.startswith('higher'):
12 min_n = mid_n + 1
13 elif answer.startswith('lower'):
14 max_n = mid_n - 1
15 else:
16 print 'Sorry, I don\'t understand your answer.'
17
18 print 'Woohoo! I got it!'
In this exercise, we will explore the sort method available for lists in python.
Below is some code that shows a list of tuples. Tuples are sequences, just like lists except they are not mutable, i.e. you cannot modify them.
tuples_list = [ ('t', 10), ('w', 2), ('d', 8), ('p',3) ]
for (letter, number) in tuples_list:
print letter, number
for pair in tuples_list:
print pair[0], pair[1]
Try running the code above in IDLE. You will notice that both loops print out the exact same thing.
Now let's see how the sort method works. Try running the code below:
tuples_list = [ ('t', 10), ('w', 2), ('d', 8), ('p',3) ]
for (letter, number) in tuples_list:
print letter, number
tuples_list.sort()
for (letter, number) in tuples_list:
print letter, number
You should notice that the second loop prints the tuples sorted in alphabetical order.The default behavior of the sort method is to sort the tuples in the list based on the value of the first element in each tuple. So what can we do to print the list sorted in numerical order? The sort method has an optional parameter that takes a function that compares two objects, x and y, and returns a negative number, 0 or a positive number depending on whether x<y, x==y, or x>y. During the course of the sort the relationships must stay the same for the final list to make sense. Below is an example of how this works.
def numeric_compare(x, y):
if x[1] > y[1]:
return 1
elif x[1] == y[1]:
return 0
else:
return -1
tuples_list = [ ('t', 10), ('w', 2), ('d', 8), ('p',3) ]
for (letter, number) in tuples_list:
print letter, number
tuples_list.sort(numeric_compare)
for (letter, number) in tuples_list:
print letter, number
The function numeric_compare takes two tuples and compares their second elements. The line in red calls the sort method with the numeric_compare function as a parameter. Notice that now the second loop prints the elements sorted numerically, i.e. based on the second element in the tuples.
The Interned Movie Database (IMDB) stores an enormous amount of movie data. There you can find a list of the top 250 most popular movies of all time. Save the file movie_list.py to your problem set 05 directory (right click on the link and click on Save link as, and give the file name movie_list.py). It contains a python list of the top 250 most popular movies. The items in the list are tuples with three elements - title (a string), year made (an integer), number of votes (an integer). Use the code below to print the list:
from movie_list import movie_list
for movie in movie_list:
print movie
The movie_list variable is imported from the module movie_list that you saved to your directory. When you run the code above, you should get the following output:
('The Shawshank Redemption', 1994, 428500)
('The Godfather', 1972, 354504)
('The Godfather: Part II', 1974, 206385)
...
('My Man Godfrey', 1936, 6567)
('Planet of the Apes', 1968, 48691)
('Bringing Up Baby', 1938, 20476)
Your goal is to write a program prints out the following:
Use functions.
# Hint: To create a copy of a list, use the following: list_copy = list[:] # To reverse a list use: list_copy.reverse()
Once you have your program working, you can verify the output here.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps5 movie_stats.py
ALGORITHM-ONE is a for loop that calls ALGORITHM-TWO n times. ALGORITHM-TWO has time complexity O(n2). What order time complexity is ALGORITHM-ONE?
What is the smallest value of n such that an algorithm whose running time is 100n2 runs faster than an algorithm whose running time is 2n on the same machine?
Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64nlog(n) steps. For which values of n does insertion sort beat merge sort?
When you got your admission letter from WTP, we had you fill out a form in which you answered the question: “If you were to write a computer program to do something you would find useful or entertaining, what would it
do?”