4 Replies - 225 Views - Last Post: 28 February 2014 - 01:11 PM Rate Topic: -----

#1 activee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-December 13

Empty dic has no keys

Posted 28 February 2014 - 08:07 AM

Hi, I'm studiing python on the OCW and there is a question in the QUIZZ n1 that I don't understand.

6) Consider the following code:

 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)
Solution :
1
5
{}

I've absolutely no idea how those answers came. Could someone help me at least with the begining so I can figure it out ? Especially the line :
for k in d.keys():
d[k] = 0
d is an empty dic so it has no keys ? I'm really confused lol.

Is This A Good Question/Topic? 0
  • +

Replies To: Empty dic has no keys

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3623
  • View blog
  • Posts: 12,511
  • Joined: 12-December 12

Re: Empty dic has no keys

Posted 28 February 2014 - 08:13 AM

I have changed the title for your question, "Question" is useless as a title.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,269
  • Joined: 21-June 11

Re: Empty dic has no keys

Posted 28 February 2014 - 08:20 AM

That code is horribly confusing - no wonder you have trouble understanding it.

View Postactivee, on 28 February 2014 - 04:07 PM, said:

Especially the line :
for k in d.keys():
d[k] = 0
d is an empty dic so it has no keys ? I'm really confused lol.


The first time that f is called, it is indeed called with an empty dictionary (d1), so the above loop does indeed not do anything. Then the rest of the function will actually put stuff into the dictionary. So after that d1 is now no longer empty.

Then f is called on d2. Because the code earlier did d2 = d1, d2 actually refers to the same dictionary as d1, so this time the dictionary is not empty. So the second time that f gets called, the above loop actually does accomplish something (namely resetting all values in the dictionary to 0). Note that this will still affect d1. That is at any time in your program d1 and d2 will contain the same keys and values because they are in fact references to the exact same dictionary. This also means that if you moved the print addUp(d1) one line down, it would produce a completely different result.

Needless to say that this style of coding is quite confusing and not at all recommended.
Was This Post Helpful? 0
  • +
  • -

#4 activee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-December 13

Re: Empty dic has no keys

Posted 28 February 2014 - 09:47 AM

Thanks a lot your first sentence gave me enough to get me going.


Considering the "how confusing the code is" I think it's exactly what they are trying to do in the quizz.
Just in case anyone is interested this is a quizz on Mit online for an intro to python : http://ocw.mit.edu/c..._00SCS11_q1.pdf
Was This Post Helpful? 0
  • +
  • -

#5 woooee  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 89
  • Joined: 21-November 12

Re: Empty dic has no keys

Posted 28 February 2014 - 01:11 PM

It prints "result" is not defined when it hits the last line, which was not one of the options. If you posted the code incorrectly then so be it, but if this is the actual question I would call them on it so they can fix it for future users.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1