Lab Practical 0

Lab Practical 0

Directions On the left is a file named LP0.py. Download it to save (lots) of typing. It is also displayed here on this page. Five minutes prior to the start of class, I will chmod this to 644 so it is accessible.

You have 90 minutes to do this. Please note: I do not expect everyone to get all of the questions; 5 and 6 each harbor some degree of trickiness. A canvas portal will be provided for you to upload your work into.

Allowable Resources You are allowed to use the textbook, your notes, your personal code reservoir and the Google. You cannot use an "intelligent agent". Examples of these include asking other people and posting questions on a forum. Them thar's nonos.

The class website is also a useful resource

Useful Tips You can do these things in advance.

When you start, download the shell code and immediately save it and run it. It shoould run without error right out of the box.

Some students like to put each function in a different file to work on them. This is fine; just reassemble them at the end. You can make a "captain file" and paste your solutions into it as you go along.

Strategy Problems 1 and 2 are very straightforward. Work those first. Also, it's a good idea to do a simple test or two as a check. Note that most of the problems have simple tests in the comments; feel free to use these. You are encouraged to keep test code in your final submission.

If you get the first two relax. You have a B- and are OK.

After you have done 1 and 2, look at 3 and 4. Choose the one you think you can get done most easily. Get that problem and you have a middle B. Get the other and you have an A-/B+. This is the typical performance on a lab practical. Three or four problems of the six is a solid performance.

Each of problems 5 and 6 have their annoyances. When you are here, you are going for the interior of the A range.

Some bonbons I show some useful code snippets without comment.


>>> x = [1,2,3,4]
>>> x[3:3] = [8]
>>> x
[1, 2, 3, 8, 4]
>>> type(x) == list
True
>>> y = "cowabunga"
>>> type(y) == list
False
>>> 

The Shell code


#  Author:
#  Date:
########### The problems come in pairs  ###########
############### Problem 1 #########################
#########  B- level for two ##################
def snip_char(s, ch):
    """prec: s is a string, ch is a character
    if the string does not contain the character the entire string
    is returned
    if it does, that character and everything after it is amputated
    and the remainder  is returned.
    examples:  snip_char("cowpie", "i") -> "cowp"
    snip_char("Moosement, "M") -> ""
    snip_char("Heffalump, "z") -> Heffalump"""
    return ""
############### Problem 2 #########################
#########  B- level for two ##################
def double_speak(s):
    """prec: s is a string
    postc: returns s with its characters doubled.
    examples:
    double_speak("cat") -> "ccaatt"
    double_speak("") -> ""
    """
    return ""
############### Problem 3 #########################
##########  B level for 1, A-/B_ for two###########
def eval_dict(d):
    """prec:  d is a dictionary whose keys are numbers and whose
    values are nonnegative integers.
    postc:  returns the product of the keys raised to their values'
    powers.  
    examples:
    eval_dict({4:2, 5:3, 3:2}) -> 4**2 * 5**3 * 3**2 = 16*125*9 = 18000.
    return 1 if the dictionary is empty.
    """
    return 1
############### Problem 4 #########################
##########  B level for 1, A-/B_ for two###########
def file_in_place(x, new_item):
    """prec:  x is a homogeneous list of sortable items that is sorted.
    (Note: x could be empty)
    postc:  new_item is an object of the list's item type.
    the new items is inserted into the list so the list is still sorted.
    NB: the list is altered in-place. You do not return a copy.
    example:
    x = [2,4,6,8]
    new_item = 7
    file_in_place(x, new_item) changes x  to
    [2,4,6,7,8]
    x = []
    new_item = "cows"
    file_in_place(x, new_item) changes x  to
    ["cows"]
    """
    pass
############### Problem 5 #########################
##########  A level for 1, A+ for two   ###########
def flatten(x):
    """prec: x is a list
    postc: returns a new list which has been "flatted" by removing and extending
    all nexted lists.  
    examples:
    flatten([1, 2, 3]) -> [1, 2, 3]
    flatten([[1,2], [3, [4, 5], 6], 7, 8]) -> [1, 2, 3, 4, 5, 6, 7, 8]
    """
    return []
############### Problem 6 #########################
##########  A level for 1, A+ for two   ###########
def push_me_pull_you(file_name, ch):
    """prec:  file_name is the name of an existing file
    ch is a character.
    postc:  prints all lines containing a word that begins and ends
    with the character ch.   My test file will have no punctuation
    mark.  This is case-sensitive"""
    pass

print("****************** Test Problem 1 *******************")
print("****************** Test Problem 2 *******************")
print("****************** Test Problem 3 *******************")
print("****************** Test Problem 4 *******************")
print("****************** Test Problem 5 *******************")
print("****************** Test Problem 6 *******************")