1

I am to return a function such that:

add1 = make_adder(1)
add1(5)
#Result: 6

make_adder(5)(10)
#Result: 15

I currently have

def make_adder(n):

    return lambda n: n+1

add1 = make_adder(1)

** Mistake noted!!*

BUT I have another similiar question where I need to check where the numbers match each other.

def is_same(x,y): 
    return x == y 

def make_verifier(key): 
    return lambda n: is_same(262010771,key ) 
check_password = make_verifier(262010771) 


If the key is having a different number, I am supposed to get a False, but I do not get where it is wrong

1
  • You should make another question, because you already have an accepted answer for this one. Commented Feb 7, 2014 at 11:14

3 Answers 3

4

I think what you want is:

def make_adder(n):
    return lambda x: n + x

As far as I can see, make_adder(n) should return a function that adds n to something that calls it. But what you have done is return lambda n: n+1, which is a function that adds 1 to something that calls it.

More explanation: In the first example, add1 or make_adder(1) is a function that adds 1 to a value passed in. In the second example: make_adder(5) is a function by itself (which adds 5), and 10 is passed into this function to give 5 + 10.

make_adder by itself is a function that creates a function that adds n to a value passed in.

Sign up to request clarification or add additional context in comments.

Comments

1
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
>>> class Adder(int):
...     def __call__(self, number):
...             return self.__class__(self + number)
... 
>>> Adder(5)(10)
15
>>> Adder(5)(10)(15)
30
>>> 

Comments

0
def is_same(x,y): 
    return x == y 

def make_verifier(key): 
    return lambda n: is_same(n,key) 
check_password = make_verifier(262010771)

Instead of putting in the number 262010771, you should put n to allow the flexibility to change n later in check_password(262010771), if not it'll be fixed and always be True.

Comments

Your Answer

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.