Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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)

MIT 6.00 Intro Computer Science (OCW)
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

For python code, it's helpful to post using http://pastebin.com/ or something similar, because open study doens't maintain tabbing.
should i type it using pastebin
Just copy and paste your code into the field, set syntax to python, hit submit and paste the link here.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

http://dpaste.com/1319048/ the code above
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.
"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/
which course are you doing? OCW 6.00 (2008)?, 6.00sc (2011)?? are you keeping up with the readings? http://www.greenteapress.com/thinkpython/html/thinkpython012.html http://docs.python.org/2.7/tutorial/datastructures.html#dictionaries http://docs.python.org/2.7/library/stdtypes.html#typesmapping http://www.greenteapress.com/thinkpython/html/thinkpython004.html

Not the answer you are looking for?

Search for more explanations.

Ask your own question