Lists exercises 1
posted by: Ms. Martin
3 May 2010
No Comment
Make sure you have read through the tutorial and tried things out on your own to make sure you understand the idea of lists before jumping into these. I didn’t show everything you need to know in class, so you do need to go through it!
Activity 1: Days of the week
Write a program that creates a list of all the days of the week, prints them out, removes Saturday and Sunday then prints that out. You should then sort the days of the week alphabetically. Finally, you should reverse that sorting and print the third item in the resulting list.
Activity 2: Student names
1. Create a list that contains the names of 5 students of this class. (Do not ask for input to do that, simply create the list.)
2. Print the list.
3. Ask the user to input one more name and append it to the list.
4. Print the list.
5. Ask a user to input a number.
6. Print the name that has that number as index or “Error, too big!” if the index is beyond the end of the list.
7. Add “John Smith” and “Mary Miller” at the beginning of the list (by using “+”).
8. Print the list.
13. Create a for loop that prints for each student “hello student_name, how are you?” where student_name is replaced by the name of the student
14. Extra challenge (optional): Create a for loop that asks the user for every name whether they would like to keep the name or delete it. Delete the names which the user no longer wants. Hint: you cannot go through a list using a for loop and delete elements from the same list simultaneously because in that way the for loop will not reach all elements. You can either use a second copy of the list for the loop condition or you can use a second empty list to which you append the elements that the user does not want to delete.
Activity 3: Sorting words
Prompt the user for words until they type in some exit command such as “exit” or “-1.” Print back all of the words the user typed in sorted order.
Extra awesomeness
Implement the Sieve of Eratosthenes algorithm for finding prime numbers. Your program should prompt the user for a number and then print out all prime numbers up to and including that bound.