List Exercises

From WLCS

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

# 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.

# 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

# Iterate through the list and find the minimum and maximum numbers

# Write out the algorithm (steps) for finding the minimum and
# maximum numbers as a list of comments in your program

# After writing the algorithm in comments, fill in the code under each
# step of your comment.

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 HALF the list
# 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)
# 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?