This worksheet focuses on dispatch tables, operators on iterables (e.g. any(), all()), and modules.
Following the new policy, Problem 1 is different in that:
The main course materials to refer to for this worksheet are:
(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)
terminal2.py
¶Recall that we developed a simple terminal application called terminal2.py
. Download this program and save it under a new name, terminal3.py
.
Then, modify the program as follows:
Create a module called termcommands
(in a file called termcommands.py
) and move all the terminal commands out of terminal3.py
and into this file. That means all the functions that begin with do_
should be moved. Also move the dispatch table into the module.
Have terminal3.py
import and use this module, so that when you're done it works in the same way as terminal2.py
. Test running it, and make sure the commands work.
Add a new command called haspy?
(think of that as "has python?") that simply prints "yes" or "no" depending on whether the current directory contains any files whose names end in .py
. It should be implemented by a function do_haspy
that is in the module termcommands
. You'll need to add a new entry to the dispatch table, of course.
If you use list comprehensions and any()
in a clever way, you can make do_haspy
a one-line function. After you get it working by any method, see if you can achieve a one-line version.
Write a program called commentzapper.py
that takes one command line argument, which is expected to be the name of a Python file. It should read that file and print all the code, except it should skip any lines that consist entirely of comments (i.e. no code at all).
For example, suppose that example.py
contains the code below:
# Save this as `example.py` if you want to test the counter program
"Sample of counting"
# MCS 260
def this_line_counts(): # I am a comment on a line that contains code
"I am a docstring!"
# TODO: Fix this!
print("Hello.")
this_line_counts()
Then running
python commentzapper.py example.py
should print the following:
"Sample of counting"
def this_line_counts(): # I am a comment on a line that contains code
"I am a docstring!"
print("Hello.")
this_line_counts()
Hint: This is an ideal problem in which to try out Python's continue
statement that we learned about last week. If you solve it another way, try to figure out how you might use continue
.
Here's a JSON file that contains a list of 100 lists of words.
The word lists have various lengths, and were selected randomly from a large dictionary.
To complete this problem you'll need that file and some code to load it into a variable in Python. Here's an example of code that can do so:
import json
# We assume "iterpuzzles.json" is in the CWD
fobj=open("iterpuzzles.json","r",encoding="UTF-8")
L=json.load(fobj)
fobj.close()
The rest of this problem consists of puzzles that assume you have the data from that file in a variable called L
.
Each puzzle asks you to write an expression in Python, ideally one line, that answers the question.
We start with a few examples (with solutions).
L[0]
any( [ len(x)==17 for x in L ] )
(This means the answer is "yes". An example is L[92]
, as you can check.)
all( [ len(x[0]) >= 5 for x in L ] )
(This means the answer is "no". You can check that L[53]
begins with "lamp"
.)
any( [ all( [len(w)%2==0 for w in x] ) for x in L ] )
(This means the answer is "yes". An example is L[7]
, which contains words of lengths [8, 6, 6, 14, 8, 8]
.)
Now the actual problems.
e
?¶"antipathy"
appear in any of the lists?¶i
is L[i]
a list in which the second word begins with the letter a
?¶Write a function multi_input(L)
that accepts a list like ["float", "string", "integer", "letter"]
and reads corresponding input from the keyboard, returning a list of results. Thus, if given the list above as input, the function might return
[ 8.5, "Chicago", 31, "g" ]
.
Specifically: Each item in the list given as an argument will be a string, and is meant to specify the expected type of one line of input. The possible values are:
"float"
- indicates a float value is to be read"integer"
- indicates an integer is to be read"string"
- indicates a string is to be read"letter"
- indicates a single letter is to be readIf the actual input does not match the expected type, the function should print a message and try again.
Here is a sample interaction with multi_input( ["float", "string", "integer", "letter"] )
:
float: asdf
That didn't work; expected entry of a float.
float: 8.5
string: Chicago
integer: Denver
That didn't work; expected entry of a integer.
integer: 31
letter: jkl
That didn't work; expected entry of a letter.
letter: g
The return value in this case would be [ 8.5, "Chicago", 31, "g" ]
.
Please use a dispatch table with keys "float"
, "integer"
, "string"
, "letter"
.