Difference between revisions of "Advanced Python List Exercises"

From WLCS
(Rubric)
Line 84: Line 84:
 
|-
 
|-
 
| Attempted  
 
| Attempted  
| 1 (per exercise)
+
| 1 pt per exercise
 
|-
 
|-
 
| Works (with minor bugs)
 
| Works (with minor bugs)
| 2 ("")
+
| 2 ""
 
|-
 
|-
 
| Works perfectly
 
| Works perfectly
| 3 ("")
+
| 3 ""
 
|-
 
|-
 
| Bonus: Good variable names
 
| Bonus: Good variable names

Revision as of 12:53, 23 February 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

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

Rubric

Criteria Pts
Attempted 1 pt per exercise
Works (with minor bugs) 2 ""
Works perfectly 3 ""
Bonus: Good variable names +1
Bonus: Simple, clear code +1
Maximum points 12pts