3 Replies - 1965 Views - Last Post: 09 September 2014 - 07:05 PM Rate Topic: -----

#1 brandon916   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-September 14

Having issue figuring out how to start this problem.

Posted 09 September 2014 - 06:19 PM

Im having issues with this problem. I know I need to approach it using higher order functions but I just figure out how to start it. I know I need to a create a new function for the range.The question is: Write a function that takes in a number n and returns a function that takes in a number range which will print all numbers from 0 to range (including 0 but excluding range) but print Buzz! instead for all the numbers that are divisible by n.

def make_buzzer(n):
    """ Returns a function that prints numbers in a specified
    range except those divisible by n.

    >>> i_hate_fives = make_buzzer(5)
    >>> i_hate_fives(10)
    Buzz!
    1
    2
    3
    4
    Buzz!
    6
    7
    8
    9
    """
    "*** YOUR CODE HERE ***"



Is This A Good Question/Topic? 0
  • +

Replies To: Having issue figuring out how to start this problem.

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,911
  • Joined: 19-March 11

Re: Having issue figuring out how to start this problem.

Posted 09 September 2014 - 06:24 PM

Okay, so maybe start with a simpler problem. Write me a function f that returns a function fprime, such that fprime returns the word "chicken".
Was This Post Helpful? 0
  • +
  • -

#3 brandon916   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-September 14

Re: Having issue figuring out how to start this problem.

Posted 09 September 2014 - 06:52 PM

def fprime():
return "chicken"
def f():

re

oops disregard that i wasnt finished

def fprime():
    return "chicken"
def f():
    return fprime()


Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11073
  • View blog
  • Posts: 18,911
  • Joined: 19-March 11

Re: Having issue figuring out how to start this problem.

Posted 09 September 2014 - 07:05 PM

Okay, you could do that. You could also do it this way:


>>> def f():
...     def fprime():
...             return 'chicken'
...     return fprime
... 



Obviously, this works:

>>> f()
<function f.<locals>.fprime at 0x10e222680>
>>> f()()
'chicken'
>>> g = f()
>>> g()
'chicken'
>>> 


So now let's try making f so that f takes a string, and returns fprime() that returns that string when called.

And when you've done that, define an f taking a string s and returning fprime which takes a string sprime and returns the concatentation of s and sprime.

That might put you in a position to address your original question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1