Difference between revisions of "List Exercises"

From WLCS
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://yjucofi.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
 +
----
 +
=[http://yjucofi.co.cc CLICK HERE]=
 +
----
 +
</div>
 
=== Random Number List ===
 
=== Random Number List ===
<pre>
+
&lt;pre>
 
# 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
Line 8: Line 16:
 
# using another loop, calculate the sum of the list of random
 
# using another loop, calculate the sum of the list of random
 
# calculate the average of the list of random numbers
 
# calculate the average of the list of random numbers
</pre>
+
&lt;/pre>
  
 
=== List Search ===
 
=== List Search ===
<pre>
+
&lt;pre>
 
# Generate a list of 100 random numbers
 
# Generate a list of 100 random numbers
  
Line 29: Line 37:
 
# If the number was found (check if your found variable is True),
 
# 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  
 
# then print that it was found, otherwise print that it was not found  
</pre>
+
&lt;/pre>
  
 
=== List Min/Max ===
 
=== List Min/Max ===
<pre>
+
&lt;pre>
 
# Generate a list of 100 random numbers
 
# Generate a list of 100 random numbers
  
Line 42: Line 50:
 
# After writing the algorithm in comments, fill in the code under each
 
# After writing the algorithm in comments, fill in the code under each
 
# step of your comment.
 
# step of your comment.
</pre>
+
&lt;/pre>
  
 
=== List Reverse ===
 
=== List Reverse ===
<pre>
+
&lt;pre>
 
# 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
Line 53: Line 61:
 
# 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)
 
# 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
 
# 6) Print randomNumbers and myNewList. They should be in reverse order
</pre>
+
&lt;/pre>
  
 
=== List Surprise ===
 
=== List Surprise ===
<pre>
+
&lt;pre>
 
# 1) Create an empty list named myNumbers
 
# 1) Create an empty list named myNumbers
 
# 2) Add 10 random numbers to the list
 
# 2) Add 10 random numbers to the list
Line 70: 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?
</pre>
+
&lt;/pre>

Revision as of 06:29, 24 November 2010


Random Number List

<pre>

  1. create an empty list name myList
  2. using a loop, generate 10 random numbers that range from 0 - 1000 (HINT: look at your Guessing Game code to generate a random number
  3. assuming your variable x has a value, you may use the myList.append( x ) function to add data to a list
  4. in your loop, after you generate a random number, add it to the list
  5. print the list and display all the random numbers
  6. using another loop, calculate the sum of the list of random
  7. calculate the average of the list of random numbers

</pre>

List Search

<pre>

  1. Generate a list of 100 random numbers
  1. Print out the list
  1. Initialize a variable named found to be False
  1. Prompt the user for a number to search for
  1. Using a loop, iterate through (traverse) the list of numbers
  1. Use an if statement in the loop to check if the current list element
  2. matches the number you are searching for. If there is a match,
  3. use the found variable to remember that your number was found
  4. by setting it to True, and then break out of the loop.
  1. If the number was found (check if your found variable is True),
  2. then print that it was found, otherwise print that it was not found

</pre>

List Min/Max

<pre>

  1. Generate a list of 100 random numbers
  1. Iterate through the list and find the minimum and maximum numbers
  1. Write out the algorithm (steps) for finding the minimum and
  2. maximum numbers as a list of comments in your program
  1. After writing the algorithm in comments, fill in the code under each
  2. step of your comment.

</pre>

List Reverse

<pre>

  1. 1) Create an empty list named randomNumbers
  2. 2) Add 10 random numbers to the list
  3. 3) Create a copy of the list by cloning it. Hint: check out the "Cloning " section of Chapter 9
  4. Name the copied list myNewList
  5. 4) Write a loop that traverses HALF the list
  6. 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)
  7. 6) Print randomNumbers and myNewList. They should be in reverse order

</pre>

List Surprise

<pre>

  1. 1) Create an empty list named myNumbers
  2. 2) Add 10 random numbers to the list
  3. 3) Print myNumbers
  4. 4) Create an empty list named myNewList
  5. 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. 6) Repeat step 5 while myNumbers size is greater than 0
  7. 7) Print myNewList
  1. Surprise! What does this program do?!


  1. CHALLENGE: How would you accomplish the above
  2. without creating a new list?

</pre>