Difference between revisions of "Advanced Python List Exercises"
From WLCS
(→List Reverse) |
|||
Line 54: | Line 54: | ||
# 3) Create a copy of the list by cloning it. Hint: check out the "Cloning " section of Chapter 9 | # 3) Create a copy of the list by cloning it. Hint: check out the "Cloning " section of Chapter 9 | ||
# Name the copied list myNewList | # Name the copied list myNewList | ||
− | # 4) Write a loop that traverses | + | # 4) Write a loop that traverses myNewList |
# 5) Inside your loop, swap the current element with its corresponding end element | # 5) Inside your loop, swap the current element with its corresponding end element | ||
# (e.g. the first element is swapped with the last element, and the second element is swapped with the second to last element) | # (e.g. the first element is swapped with the last element, and the second element is swapped with the second to last element) | ||
+ | # temp = a | ||
+ | # a = b | ||
+ | # b = temp | ||
# 6) Print randomNumbers and myNewList. They should be in reverse order | # 6) Print randomNumbers and myNewList. They should be in reverse order | ||
</pre> | </pre> |
Revision as of 08:14, 11 April 2011
Random Number List
# create an empty list name myList # using a loop, generate 10 random numbers that range from 0 - 1000 (HINT: look at your Guessing Game code to generate a random number # assuming your variable x has a value, you may use the myList.append( x ) function to add data to a list # in your loop, after you generate a random number, add it to the list # print the list and display all the random numbers # using another loop, calculate the sum of the list of random # calculate the average of the list of random numbers
List Search
# Generate a list of 100 random numbers # Print out the list # Initialize a variable named found to be False (found=False) # Prompt the user for a number to search for # Using a loop, iterate through (traverse) the list of numbers # Use an if statement in the loop to check if the current list element # matches the number you are searching for. If there is a match, # use the found variable to remember that your number was found # by setting it to True, and then break out of the loop. # Outside and after the loop, if the number was found (check if your found variable is True), # then print that it was found, otherwise print that it was not found
List Min/Max
# Generate a list of 100 random numbers # Create a variable named minNum and store the first element of your list # Initialize a counter variable x to the value 0 # Using a while loop, iterate through the list to find the minimum # Inside the loop, if the current element at location x (myList[x]) is less than minNum, then # store the current element (myList[x]) into minNum # After the loop, print out minNum # Now, recreate the above and find the maximum number
List Reverse
# 1) Create an empty list named randomNumbers # 2) Add 10 random numbers to the list # 3) Create a copy of the list by cloning it. Hint: check out the "Cloning " section of Chapter 9 # Name the copied list myNewList # 4) Write a loop that traverses myNewList # 5) Inside your loop, swap the current element with its corresponding end element # (e.g. the first element is swapped with the last element, and the second element is swapped with the second to last element) # temp = a # a = b # b = temp # 6) Print randomNumbers and myNewList. They should be in reverse order
List Surprise
# 1) Create an empty list named myNumbers # 2) Add 10 random numbers to the list # 3) Print myNumbers # 4) Create an empty list named myNewList # 5) Write a loop that traverses through myNumbers and finds the LOCATION of the minimum number. # After you find the min, add it to myNewList and delete it from myNumbers. # 6) Repeat step 5 while myNumbers size is greater than 0 # 7) Print myNewList # Surprise! What does this program do?! # CHALLENGE: How would you accomplish the above # without creating a new list?
Rubric
Criteria | Pts |
---|---|
Attempted | 1 pt per exercise |
Works with minor bugs | 2 "" |
Works perfectly | 3 "" |
Bonus: Good variable names | +3 |
Bonus: Simple, clear code | +3 |
Maximum points | 15 pts |