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 (answer on handin pages):
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.
Read the ChocolateBar APT.
str_unique
that returns true if all
the letters in its string parameter are different (no repeats)
and false otherwise. Use a set for this, you may want to to use
list("help")
to change a string "help" to the list
["h","e","l","p"], for example.
Call | Return |
---|---|
str_unique("slight") | true |
str_unique("differ") | false |
str_unique("monster") | true |
guessed
is a list that represents
how many letters of the secret word have been guessed. For
example, suppose the secret word is "ukelele". Then
guessed
is initially ["_", "_", "_", "_", "_", "_", "_']
.
A function process_letter works as follows:
process_letter
-- note what the types
of the three parameters to your function are (see handin pages).
process_letter
you'll need to access
elements of secret
and
guessed
. Write the code that
loops, checks whether a letter has been
guessed, and modifies guessed
(the list parameter) appropriately?
Suppose that instead of a word you want to guess a phrase --- like a pro-basketball team: such as "New York Knicks", "Miami Heat", "Los Angeles Lakers". This could lead to this initialization
knowledge
?
process_letter
from the
previous
problem, how can you process a guess from the user to change all
elements
of knowledge
, e.g. what's the value of
knowledge
after executing the code below?
knowledge
is a list of lists, how can these be
displayed for hangman, how do you display all of them like
what's below (for New York Knicks), e.g., with extra spaces between
the chunks of underscores? Write code to do this.
_ _ _ _ _ _ _ _ _ _ _ _ _