Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 12 Online
OpenStudy (anonymous):

def f(s, d): for k in d.keys(): d[k] = 0 for c in s: if c in d: d[c] += 1 else: d[c] = 0 return d Does the return function return the key of d or value of d? and WHY? Thanks!

OpenStudy (turingtest):

what is s? a list of some sort?

OpenStudy (anonymous):

s is a list of strings, d is dictionary.

OpenStudy (turingtest):

this function returns d, the dictionary, after some modification to the original input. You should break up the code and comment on each section to walk through it more clearly so you can see exactly what it does.

OpenStudy (turingtest):

def f(s, d): for k in d.keys(): # iterates over a list of the keys of the dict d[k] = 0 # sets each value for each key to 0 for c in s: #iterates over each element in the list of strings if c in d: #if an element in the list is a key in the dict d[c] += 1 #add 1 to the value in the dict for that key else: d[c] = 0 #otherwise add that key to the dict and set it to 0 return d #return the dictionary

OpenStudy (turingtest):

so it returns neither the keys, nor values of d, but d itself in its entirety, albeit modified

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!