1
\$\begingroup\$

Q4. Define a function make_counter that returns a counter function, which takes an immutable key and a numerical value argument and returns the sum of all arguments ever passed to counter with that same key.

Below is the solution:

def make_counter():
 """Return a counter function.
 >>> c = make_counter()
 >>> c('a', 3)
 3
 >>> c('a', 5)
 8
 >>> c('b', 7)
 7
 >>> c('a', 9)
 17
 >>> c2 = make_counter()
 >>> c2(1, 2)
 2
 >>> c2(3, 4)
 4
 >>> c2(3, c('b', 6))
 17 
 """
 dict = {} 
 def counter(key, value):
 if key in dict:
 dict[key].append(value)
 else:
 dict[key] = [value]
 lst = dict[key]
 total = 0
 length = len(lst)
 for index in range(length):
 total = total + lst[index]
 return total
 return counter
 """-m doctest gives some weird failure despite all above test case pass"""

Can we improve this solution?

jonrsharpe
14k2 gold badges36 silver badges62 bronze badges
asked May 16, 2015 at 8:17
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Rather than asking three questions in rapid succession, why not ask one, wait for feedback, apply it to the others then ask a follow-up? Also, there's no need to include all of the boiler-plate twice. \$\endgroup\$ Commented May 16, 2015 at 9:34
  • 3
    \$\begingroup\$ Note that the reason for your doctest failure is trailing whitespace, after the 17 for the final test. It is quite picky about these things! \$\endgroup\$ Commented May 16, 2015 at 9:38

1 Answer 1

5
\$\begingroup\$

Choose another name for the dictionary besides dict. Don't ever hide the name of a built-in type.

Then...

d = {} 
def counter(key,value):
 d[key] = d.get(key,0) + value
 return d[key]

You're done. I see no need to build lists of all the passed values.

answered May 16, 2015 at 8:37
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.