+ 6
Coding challenge #10 - Add two numbers (Functional Programming #1) (Min Chars #3)
Create a function "add" which takes one integer argument "n" and RETURNS A FUNCTION that adds n to any other number. Examples addOne = add(1) print(addOne(3)) #4 addFour = add(4) print(addFour(8)) #12 Maximum number of characters to be used in Python is 25. Good luck.
9 Respostas
+ 6
Python solution :
https://code.sololearn.com/cl4dBdno8Sj8/?ref=app
C++ solution :
https://code.sololearn.com/c67vm6vs6vqu/#cpp
+ 8
@Baptiste. I'm gonna make the next challenge REALLY difficult since you always solve them very fast!! 😉😅
+ 8
@JG
add() returns a function.
When I say addFour=add(4),
addFour(n) will have the value 4+n , since add function returns another function x+n (where x=4 in this case)
When addFour(8) is called, it will return the value of 8+4=12. This concept is functional programming
+ 8
In Baptiste's code, he hasnt explicitly stated another function to call. Instead, he gave it as a secondary argument.
add(4)(8)
will first return lambda n:4+n and then compute to 4+8=12 since the argument 8 is given.
+ 5
I'm a bit confused. The func name is add, right? Then how do you use the name "addFour" as a func? Without having multiple functions?
+ 2
@Pixie, that would make me happy ! :D
+ 2
I edited my answer to add C++ as nobody did (when I am posting this text)
Thanks to your challenge, I learned basics of lambda expressions in C++ ^^
0
A week late, but thought it would be fun. Takes user input and checks for valid numerical input.
Edit: Also totally didnt see the maximum chars part for Python lol, this is definitely more than 25 chars xD
https://code.sololearn.com/ch7cR94kRAXm/?ref=app