Difference between revisions of "Advanced Python List Exercises"
From WLCS
(→List Surprise (optional challenge)) |
|||
| (14 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
# create an empty list name myList | # 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 | # 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 | + | # in your loop, after you generate a random number, add it to the list |
# print the list and display all the random numbers | # print the list and display all the random numbers | ||
| − | # using another loop, calculate the sum of the list of random | + | # using another loop, calculate the sum of the list of random numbers |
# calculate the average of the list of random numbers | # calculate the average of the list of random numbers | ||
</pre> | </pre> | ||
| Line 12: | Line 12: | ||
=== List Search === | === List Search === | ||
<pre> | <pre> | ||
| − | # Generate a list of 100 random numbers | + | # Generate a list of 100 random numbers (you choose the range) |
# Print out the list | # Print out the list | ||
| − | # Initialize a variable named found to be False | + | # Initialize a variable named found to be False (found=False) |
# Prompt the user for a number to search for | # Prompt the user for a number to search for | ||
| Line 38: | Line 38: | ||
# Initialize a counter variable x to the value 0 | # Initialize a counter variable x to the value 0 | ||
| − | # Using a | + | # 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 | # Inside the loop, if the current element at location x (myList[x]) is less than minNum, then | ||
| Line 52: | Line 52: | ||
# 1) Create an empty list named randomNumbers | # 1) Create an empty list named randomNumbers | ||
# 2) Add 10 random numbers to the list | # 2) Add 10 random numbers to the list | ||
| − | # 3) Create a copy of the list by cloning it. Hint: | + | # 3) Create a copy of the list by cloning it. Hint: Google it |
# Name the copied list myNewList | # Name the copied list myNewList | ||
| − | # 4) Write a loop that traverses | + | # 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 | # 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 = myNewList[a] | ||
| + | # myNewList[a] = myNewList[b] | ||
| + | # myNewList[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> | ||
| − | === List Surprise === | + | === List Surprise (optional challenge) === |
<pre> | <pre> | ||
# 1) Create an empty list named myNumbers | # 1) Create an empty list named myNumbers | ||
| Line 75: | Line 78: | ||
# CHALLENGE: How would you accomplish the above | # CHALLENGE: How would you accomplish the above | ||
| − | # without creating a new list? | + | # without creating a new list and without adding or removing elements? |
</pre> | </pre> | ||
| − | ==Rubric== | + | ===Grading Rubric=== |
{| class="wikitable" | {| class="wikitable" | ||
| Line 89: | Line 92: | ||
|- | |- | ||
| Works with minor bugs | | Works with minor bugs | ||
| − | | 2 | + | | 2 |
|- | |- | ||
| Works perfectly | | Works perfectly | ||
| − | | 3 | + | | 3 |
|- | |- | ||
| Bonus: Good variable names | | Bonus: Good variable names | ||
Latest revision as of 12:03, 6 March 2018
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 |