Put answers on handin page.
assignNumbers(numbers,howMany,slots)
for the values
below? Show how you determined the answer.
sum
, list
comprehensions, and the enumerate
loop (e.g.,
for i,val in enumerate(numbers)
that calculates the total
number of keypresses needed when there are no speed-dial
slots. Ideally you'll write one line as below, but more than one
is ok if that helps you think about the problem. It will likely
help to convert integer values to string values using
str
, e.g., str(123) = "123"
because
you can use the len
function with strings.
saved
of how many key-presses are saved for each
phone number in numbers
-- you code should result
in saved[i]
having the value of how many key-presses are
saved when numbers[i]
is dialed
howMany[i]
times.
saved
created in the
previous problem represent numbers that should be put in
speed-dial slots? Why?
saved_presses
using
the list [1,2,3,4,5][-2:]
evaluates to the list [4,5]
by
"slicing" the last two elements.
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. 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.