Advanced Python List Exercises
From WLCS
Contents
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
# use the myList.append() function to add the 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 numbers
# calculate the average of the list of random numbers
List Search
# Generate a list of 100 random numbers (you choose the range)
# 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 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: Google it # Name the copied list myNewList # 4) Write a loop that traverses (travels through) half of myNewList. HINT: len(myNewList)/2 # 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 = myNewList[a] # myNewList[a] = myNewList[b] # myNewList[b] = temp # 6) Print randomNumbers and myNewList. They should be in reverse order
List Surprise (optional challenge)
# 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 and without adding or removing elements?
Grading 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 |