Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Pondering

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)

  • 9 months ago
  • 9 months ago

  • This Question is Closed
  1. bdean20
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 9 months ago
  2. Pondering
    Best Response
    You've already chosen the best response.
    Medals 0

    should i type it using pastebin

    • 9 months ago
  3. bdean20
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 9 months ago
  4. Pondering
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 9 months ago
  5. bdean20
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • 9 months ago
  6. codeXY
    Best Response
    You've already chosen the best response.
    Medals 1

    "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/

    • 9 months ago
  7. bwCA
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 8 months ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.