Compsci 101, Fall 2017, Lab 3

NOTE: Labs are to be done during lab with a partner. Do not start the lab until you are in lab.

The lab

In this lab you will:

There are no files to snarf for this lab.

Getting Credit for Lab 3

To get credit for lab, you will need to do the following by Sunday night:


Part 1 : String Slicing and Dicing

Assume you've typed these three string values and associated variables into your Python interpreter/console window:


a = "computational thinking"
b = "duke university"
c = "python code"

You may want to actually type these into a console window, though you should first try to do the problems below using paper, pencil, discussion, and thinking (no computer)

Using these strings only, you should use the slicing operator, string concatenation, string multiplication (by an int, as in "abc" * 3), or indexing to form each of the words below. See the examples for details. Each use of any operator (slicing, indexing, concatenation, and multiplication) has a cost of one. Indicate the cost of your construction.

You'll put each answer on the google form. The first two examples show you how to slice an index, you'll answer starting from question 3 below. The strings and indexes are shown below to help you think and reason about slicing and indexing.

a = "computational thinking"
     0123456789012345678901

b = "duke university"
     012345678901234

c = "python code"
     01234567890

  1. "honk" formed from c[3:6]+b[2]

    This has a cost of three: one slice, one catenation, one index.

    The value of c[3:6] is hon because the slice starts at the character whose index is 3 which is 'h' and goes up to, but doesn't include the character whose index is 6.

    the value of b[2] is k because it's the single character whose index is 2. Adding or concatenating these strings together makes "honk".

  2. "puking" formed from a[3:5]+a[18:], again the cost is three: two slices and one catenation with +.

    Note that the value of a[18:] starts at the index-18 character and goes until the end of the string, i.e., is king. You can also multiply strings by a number at a cost of 1, i.e., a[3:5]*2 is "pupu" at a cost of 2: one slice and one multiply.

  3. Create "docking" and provide its cost.

  4. Create "thin king" and provide its cost.

  5. Create "computer code" and provide its cost.

  6. Create "ratatatat" and provide its cost.

  7. Create "duke is cool" and provide its cost.

  8. Create "diversity nation" and provide its cost.

  9. Create "money honey" and provide its cost.


Part 2: Reasoning about Conditionals

  1. In the following three examples, a higher fee is suppose to be assigned to a higher speed. For each of the following three choices, decide if the higher fee is assigned to the higher speed and if not explain why. For example, if the speed is 85, is the fee assigned always 60? The fees are 0, 20, 40, and 60 --- think of these as speeding fines, for example. Each example is supposed to work regardless of the value of variable speed.

    
    # Example A    
        speed = 85
        fee = 0
        if speed > 35: 
            fee = 20
        if speed > 50: 
            fee = 40
        if speed > 75: 
            fee = 60
        print fee
        
    # Example B    
        speed = 85
        fee = 0
        if speed > 75: 
            fee = 60
        if speed > 50: 
            fee = 40
        if speed > 35: 
            fee = 20
        print fee    
        
    
    # Example C
        speed = 85
        if 35 < speed and speed <= 50: 
            fee = 20
        if 50 < speed and speed <= 75: 
            fee = 40
        if 75 < speed: 
            fee = 60
        print fee
    
  2. To which of the following is this expression equivalent:

    not ((x > y) and (y <= 3))
    1. (x > y) and (y <= 3)
    2. (x > y) or (y <= 3)
    3. (x < y) or (y >= 3)
    4. (x <= y) or (y > 3)
    5. (x <= y) and (y > 3)
  3. A year is a leap year when it has 366 days instead of 365 days. In the international Gregorian calendar a year is a leap year according to the following, correct but perhaps poorly worded, rules as obtained from http://www.timeanddate.com/date/leapyear.html
    1. Every year divisible by 4 is a leap year
    2. But every year divisible by 100 is NOT a leap year
    3. Unless the year is also divisible by 400, then it is still a leap year

    This means 1800, 1900, and 2100 are not leap years but 2000 and 2004 are leap years.

    Which of the following implementations of a method is_leap returns true if year is a leap year and false otherwise. For each one list whether or not it is correct. If a method is not correct, provide a value for year for which it returns the wrong value.

    A.
    def is_leap1 (year):
      if year % 400 == 0: return True
      if year % 100 == 0: return False
      if year %   4 == 0: return True
      return False
    
    B.
    def is_leap2 (year):
      return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)
    
    C.
    def is_leap3 (year):
      if year % 100 == 0:                  return False
      if year % 400 == 0 or year % 4 == 0: return True 
      return False
          
  4. Complete the function all_same that returns true only if all three of the given values are the same. For example, a call to all_same with the values 3, 128, 255 should return False; while the values 128, 128, 128 should return True.
  5. def all_same (value1, value2, value3):
    
    
    
    


Part 3: APT Acronym

A list can be used with a for loop to have the body of the loop repeat the number of times equal to the number of items in the list.

Here is an example:

words = ["phone","book","baby","punch","fox","elf","country","sun"]
for w in words:
    print w,pluralize(w)

You'll learn much more about the type list, a powerful Python type, a list is a collection of values between brackets [ and ]. The String method .split() creates a list by splitting a string into the values separated by whitespace. For example:

val = "this is the way the world ends"
words = val.split()
print len(val), len(words), words

Will generate the output:

30 7 ['this', 'is', 'the', 'way', 'the', 'world', 'ends'] 

  1. Discuss the following code in your group to see what it does, then type it in and run it to see if you were correct.
    phrase = "Do Re Me Fa So La Ti Do"
    phraseList = phrase.split()    
    # note that  phraseList is equal to ['Do', 'Re', 'Me', 'Fa', 'So', 'La', 'Ti', 'Do']
    answer = ""
    for word in phraseList:
        if word[1] != 'a':
            answer = answer + word
    print answer
    

    Explain what this code does.

  2. Read the Acronym APT statement. Then with your group come up with a general plan for how to form the acronym of the parameter phrase --- your plan should be in English, not in code.

    Describe briefly your plan in words for solving this APT.

  3. Using Eclipse create a new python project (or use one project for all your APTs for this week.). Then create a python module with the name CreateAcronym.py (the .py extension will be automatically added). In that module you should create a function named acronym with the parameter indicated by the wording in the APT, reproduced below:

    filename: CreateAcronym.py 
    
    
    def acronym(phrase): 
        """ 
        return a string that is an acronym
        of the string parameter phrase 
        """ 
        # you write code here
    
    

    Write code to create a list variable that is the words in parameter phrase. Name the list variable words. What would this line be?

  4. What is a for loop that will loop over each element of the list of words in phrase (use variable words from above) -- don't write the loop body yet, just the loop.

  5. Use the loop, explain how to index each word in the body of the loop to access the first character of the word, i.e., what's the Python code to do that and append the first character to the end of a string as shown below in the blank
         acro = ""
         for-loop-you-wrote-above:
             acro = acro + ______________
         return acro
    

  6. Cut and paste your APT Acronym code (how much of it you have solved, does not have to be complete here).

    Reminder, even though we are discussing this APT in lab, you still need to complete and submit this APT as part of the APT problem set.