## Pondering 2 years ago I have tried to understand problem number 6 (below) from quiz 1. I dont understand what "d.keys()" does/mean? I dont get d[k] = 0 means? Is k indexing to 0? I dont understand why d2 has d2 in one of the parameters. 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 def addUp(d): result = 0 for k in d: result += d[k] return result d1 = {} d2 = d1 d1 = f('abbc', d1) print addUp(d1) d2 = f('bbcaa', d2) print addUp(d2) print f('', {}) print result 6.1) What does it print? (9 points)

1. bdean20

For python code, it's helpful to post using http://pastebin.com/ or something similar, because open study doens't maintain tabbing.

2. Pondering

should i type it using pastebin

3. bdean20

Just copy and paste your code into the field, set syntax to python, hit submit and paste the link here.

4. Pondering

http://dpaste.com/1319048/ the code above

5. bdean20

Did you copy from idle? Copying from here means the tabs are still lost. Anyway, d.keys() is a function you can call on a dictionary to get the names of each key used in a key->value mapping. For: a dictionary { "A"=>1, "B"=>2, "C"=>3}, the keys would be the array ["A","B","C"]. d[k]=0 sets a new key->value with the key k and the value 0. Not sure what the rest of the code is doing to know why d2 uses d2 as a parameter.

6. codeXY

"I dont understand what "d.keys()" does/mean? " "d" would be the name of the dictionary and ".keys()" is what they call a method attribute for a dictionary. The .keys() method gets the keys in the dictionary. An example would be like if d was a dictionary for months and the number representation of the month, d = {'Jan':1, 'Feb':2, 'Mar':3} http://dpaste.com/1319099/ In this example d.keys() would return Jan, Feb and Mar (in a random order because dictionaries doesn't keep the keys in order). "I dont get d[k] = 0 means? Is k indexing to 0?" In this statement d[k] = 0, k is not an index. since d is a dictionary, k is the key in the dictionary. "I dont understand why d2 has d2 in one of the parameters." When the program executes, the first thing that happens after functions f and addUp are defined is that d1 is set to be a blank dictionary and d2 is set to equal d1. In the statement, d1 = f('abbc', d1), d1, the value of d1 is being passed in the parameter to the function f. Since d1 is currently a blank dictionary, there are no keys for d.keys() to do anything and d[k] will not set anything to 0 yet. In a nutshell, the function f takes any existing keys in a dictionary and sets there value to 0. Then it checks if any of the letters in the string, in this case 'abbc', are in the dictionary. If the key does not exist in the dictionary then it will create it. If it does exist, then it will add 1 to it's value. At the end of the function, d1 will have the keys a, b and c. Looking at the line d2 = f('bbcaa', d2), at this point d2 is still a blank dictionary, so there are no pre-existing keys in d2 being passed. BUT the function f will create keys and give them values based on the string 'bbcaa' I realize that this might be hard to understand without looking at actual code and examples so what I've done was take the code you provided and inserted print statements to help demonstrate what is happening. Just copy and paste the code into IDLE and run it. http://dpaste.com/1319151/