Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Python. Define a function make_counter that returns a counter function, wh

ID: 3917436 • Letter: U

Question

Using Python. Define a function make_counter that returns a counter function, which takes a string and returns the number of times that the function has been called on that string.
def make_counter(): """Return a counter function.
>>> c = make_counter() >>> c('a') 1 >>> c('a') 2 >>> c('b') 1 >>> c('a') 3 >>> c2 = make_counter() >>> c2('b') 1 >>> c2('b') 2 >>> c('b') + c2('b') 5 """ "*** YOUR CODE HERE ***" Using Python. Define a function make_counter that returns a counter function, which takes a string and returns the number of times that the function has been called on that string.
def make_counter(): """Return a counter function.
>>> c = make_counter() >>> c('a') 1 >>> c('a') 2 >>> c('b') 1 >>> c('a') 3 >>> c2 = make_counter() >>> c2('b') 1 >>> c2('b') 2 >>> c('b') + c2('b') 5 """ "*** YOUR CODE HERE ***" Using Python. Define a function make_counter that returns a counter function, which takes a string and returns the number of times that the function has been called on that string.
def make_counter(): """Return a counter function.
>>> c = make_counter() >>> c('a') 1 >>> c('a') 2 >>> c('b') 1 >>> c('a') 3 >>> c2 = make_counter() >>> c2('b') 1 >>> c2('b') 2 >>> c('b') + c2('b') 5 """ "*** YOUR CODE HERE ***"

Explanation / Answer

mydict={}

def make_counter(param):

if param in mydict.keys():

mydict[param]=mydict[param]+1

return mydict[param]

else:

mydict[param]=1

return mydict[param]

  

c=make_counter

c("a")

print(c("b")+c("b"))

print(c("a"))

Output:

3

2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote