For this lab you must work with a partner that you have not previously submitted a lab with.
For Lab 6, snarf the code for assignment 5. You will start the assignment in this lab.
To get credit for lab, you will need to do the following by Sunday night:
A Python list comprehension allows you to create a list from another list/sequence. For example the list comprehensions below generate the output shown. Note that a list comprehension is a list defined by a for-loop inside square brackets, and that the loop iterates over some list, creating a new list in the process. In the examples below, the output of each print statement is shown in italics.
somelist = [x*2 for x in range(0,10)] print somelist [0,2,4,6,8,10,12,14,16,18] somelist = [s[0] for s in ['apple', 'termite', 'elephant']] print somelist ['a','t','e'] somelist = [n % 2 == 0 for n in [2,4,6,1,3,5,8]] print somelist [True,True,True,False,False,False,True] somelist = [n for n in [2,4,6,1,3,5,8] if n % 2 == 0] print somelist [2,4,6,8]Without using a computer, (so by thinking) show what each line below prints. Enter your answers in the online form.
print [x**2 for x in range(1,10,2)]
print [s[1] for s in ["big", "brown", "bare", "stem", "pea"]]
print ['po'*i for i in range(0,5)]
print [i*i for i in range(1,10) if i % 2 == 0]
print sum([1 for p in [1,2,3,4,5,6,7,8]])
print sum([1 for x in "ostentatiously" if "aeiou".find(x) > -1])
In writing your own list comprehensions it often helps to write intermediate expressions before creating the final list. Recall that the first variable in a comprehension determines the type of elements stored in the list. General steps in creating a list comprehension:
sum
that returns the sum of all the odd integers
in a list of integers named nums
-- so for [1,3,2,4,5] your
expression should evaluate to 9 --- fill in the parentheses:
sum(...)
.
sum
and len
write an expression that returns
the average length of all the strings in a list of strings
named strs
. So you'll
call two list functions: one with strs
as an
argument
and one with a list comprehension using strs
as an
argument: combining in one expression
sum(...)
and len(...)
. For example,
the average length of the strings in the list below
is (3+3+4+5)/4 = 15/4 = 3.75
strs = ["cat", "dog", "bear", "tiger"]
los
that begin with the letter 't'. For example,
los = ['the','turtle','was','a','tiny','green','troubled','animal']The list comprehension should create [0,1,4,6], and it should work even when the values in los change to other strings.
In Python a set does not store duplicates, each value is only stored once. For example, the code below generates the output shown:
3, set([1,2,3])A set can be created from a list as shown, and elements can be added to the set using the set method
.add
, e.g.,
2, set(['small','big'])
foods
below, e.g., that
sets diff
to have the value 4, but will work even if
the values in food
change:
s
, the value of sorted(s)
is
a sorted list of the individual characters in the string,
e.g.,
sorted("apple") = ['a','e','l','p','p']
What's the value of the list comprehension below (write it out) --- you should have a list of lists:
join
creates a string from a list of
strings as
shown below.
method call | result |
---|---|
''.join(["a", "r", "t"])
| "art" |
' '.join(["the","big", "dog", "runs"])
| "the big dog runs" |
The string to the left of the dot '.' is inserted between each string in
the list that's a parameter to the join
method
and one string is returned as shown --- the new string is
formed by concatenation. In the first call above
an empty string ''
is inserted, in the second a
space ' '
is inserted.
Using sorted
and join
write a list
comprehension to create in unique
the sorted
string
version of each string in words
, e.g., to create
["aet", "aet", "aet", "abt", "abt", "deo" "deo"]
out of the list ["eat", "aet", "tea", "bat", "tab", "ode", "doe"]
getMaximumSubset
for the
AnagramFree APT. Use set
, sorted
,
join
and a list comprehension. If you can't
do it one line, use more than one.
REMEMBER - If you want to use this APT as one of your required APTs you must submit it through ambient/eclipse.
This part of the lab suggests some ways to do parts of the Hangman assignment including the second part.
For this part you will snarf assignment 5 and get started on it. Here is also the code for starting.
The program starts by reading in words from the
file lowerwords.txt
. Then the user is asked for how many
letters there are in a word.
wordToGuess
we represent it as a list of characters
in the variable
guessList
. That is, guessList
is a list that represents
how many letters of the secret word have been guessed. For
example, suppose the secret word is "ukelele". Then
guessList
is initially ["_", "_", "_", "_", "_", "_", "_"]
.
The function updateLetter is given wordToGuess,
guessList
and letter
and updates guessList
if
letter
is in wordToGuess
.
NOTE:
updateLetter is a mutator function, it should not have a return
statement, there is no return value, it just
modifies the list guessList.
Here is an example of using updateLetter.
Note how the function is called. Since there is no return value, there is no value to catch in a variable. The list guessList is modified after each call if there was a change.
Complete the function for updateLetter
.
def updateLetter(guessList,wordToGuess, letter):
Modify the code so that if the user guesses a letter that's not in word, the program prints a message "that's a miss", and otherwise prints "you guessed a letter" Explain in words changes you had to make.
Cut and paste the function playGame.
Write the function getPossiblePhrases
that is similar to
getPossibleWords
and has the input, wordlist
, which is a list of strings of all
movie titles, and length
, which is a number. This function should
return the list of titles that have length
words.
For example if length is 3, return a list of strings of titles that are exactly three words.
You should use a list comprehension in writing this code.