DataReader.py
and
describe the parameters to the function csv.reader
in parsing/reading the .csv file.
row
read is
row[2]
, the name of the song is
row[1]
. In the reading loop of dump
remove/comment out the print statement. Use the artist as the
key to a dictionary in which the value is a list of songs. Add
each song read to the list of songs for the artist. Here's the
code you'll need (if variables artist
and
song
have been set appropriately).
d[artist] = []
?
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.
The union of two sets A and B is the elements that are in A together/combined with the elements in B. The union is a set, so there are no duplicates. The vertical bar | is the set-union operator in Python:
set([1,2,3]) | set([2,3,4,5]) == set([1,2,3,4,5])
To solve this APT consider the list comprehension below:
cheat1
cheat1
,
cheat2
, and cheat3
that represent
similar
lists for the three clubs.
x = set(lst)
,
and the union operator |
combines sets just as
the addition operator +
adds integers or
concatenates strings, e.g., the expression below
stores the union of three sets in a variable w
.
The sorted
function returns a sorted
list from any Python sequence, e.g., a list a string or a set. Write
one line using sorted
, the three lists created in the
previous problem, and set operators to return the correct
value for the APT.
return sorted(......)
names
. You can
do this by combining the functions set
,
list
, and sorted
, e.g.,
list(set(list(sorted(xx))))
or
sorted(list(set(x)))
, for example. Store the list in a
variable
unique
.
name
? Once you have
this value, how will you concatenate the string form of
this count (which is an int), e.g., finish the APT on paper
using the function str
to convert an int to a string.b