CS414 Lab 3: Lists

September 15th, 2017

Due: September 16th, 2017, before midnight

Late penalties: Fri -5%, Sat -10%, Sun 20%.

For this lab, you will work with single file, called lab3.py. I've prepared a starter file for you, available here on the course webpage.

Before you begin

  1. Create a folder for your work.
  2. Go to the lab's webpage, and save the file lab3.py into that folder.
  3. Run IDLE, and open the file lab3.py .

Exercises

Complete the following exercises. They all deal with lists.
  1. Contains: look at the code below, which determines if a list contains the value 3:
                list1 = [1, 2, 'apple', 3, 'banana']
                found_3 = False
                for value in list1:
                    if value == 3:
                        found_3 = True
                if found_3:
                     print 'list1 has a 3'
                else:
                     print 'list1 has no 3s'
              
    Modify the code, so that it determines whether the list contains a string. To check whether value is a string, compare its type to the type of a known string:
                   if type(value) == type('abc'):
                      # it's a string!
              

  2. Counting values: Here is some code that counts how many times the list contains a 1:
                list2 = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
                ones_count = 0
                for value in list2:
                    if value == 1:
                        ones_count += 1
                print 'list2 has', ones_count, 'ones'
              
    Modify the code, so that it counts many values in list2 are odd. To check if a value is odd, get its remainder when divided by 2:
                if value % 2 == 1:
                    # it's odd!
              

     

  3. Repeats: Here is code that checks if a list has two consecutive equal values:
                list3 = [12, 32, 34, 34, 23, 23, 12, 12]
                previous_value = list3[0]
                two_in_a_row = False
                # Start at the SECOND value in the list:
                for value in list3[1:] :
                    if value == previous_value:
                        two_in_a_row = True
                    previous_value = value
                if two_in_a_row:
                    print 'list3 has 2 in a row'
                else:
                    print 'list3 does not have 2 in a row'
              
    Modify the code so that it counts how many times there are two consecutive equal values. In the above list, this occurs 3 times.

  4. Stats: Here is a program that adds up all the values in a list (assuming they're all numbers):
                list4 = [12, 32, 33, 35, 23, 23, 12, 12]
                total = 0
                for value in list4:
                     total += value
                print 'sum:', total
              
    Modify to compute the average of all the odd values in the list. You will need two variables: Of course, these variables should be updated only if you find an odd value!

  5. Palindromes: Here is another way to visit a list's values: use an index . We're looking for odd values:
                list5 = [1, 2, 3, 4, 3, 2, 1]
                for index in range(len(list5)):
                    if list5[index] % 2 == 1:
                        print 'odd value at index',index
              
    Modify this code so that it checks whether a list is palindromic (it reads the same forwards and backwards). list5 is palindromic if, for every index, the following is True:
                list5[index] == list5[-(index + 1)]
              
    As in exercise 1, you will need a boolean variable, which holds the result. Think: should it start True, and possibly become False inside the loop, or the other way around?

     

     

     

     

     

  6. Transitions: This code finds all the runs of consecutive equal values:
                list6 = [1, 1, 1, 3, 3, 1, 1, 1, 0, 2, 2, 2]
                previous_value = list6[0]
                current_run_length = 1
                for value in list6[1:] :
                    if value == previous_value:
                        current_run_length += 1
                    else:
                        # a run just finished!  Report it, and start counting again
                        print 'A run of', current_run_length, previous_value
                        current_run_length = 1
                    previous_value = value
                # There will always be a run left over:
                print 'A run of', current_run_length, previous_value
              
    Modify the code, so that it reports the transitions from one value to a different one. For example, in the above list, there's a transition from 1 to 3, then from 3 to 1, then from 1 to 0, then from 0 to 2.

    For the above list, you should output

                  1 -> 3
                  3 -> 1
                  1 -> 0
                  0 -> 2
                

Turning in your work

At the end of the lab session, go the mycourses.unh.edu, find CS414, find lab #3. Click on the "Submit" button, and upload the file lab3.py .

You can re-submit your work until midnight tonight, with no lateness penalty.