Python Multiple Function CompositionCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonUsing global variables in a functionHow can I make a time delay in Python?How to make a chain of function decorators?Does Python have a string 'contains' substring method?
Which US defense organization would respond to an invasion like this?
Why are oscilloscope input impedances so low?
My first C++ game (snake console game)
Motion-trail-like lines
Why did WWI include Japan?
How to deal with employer who keeps me at work after working hours
Counting the Number of Real Roots of A Polynomial
Dangerous workplace travelling
Drawing an hexagonal cone in TikZ 2D
Should I simplify my writing in a foreign country?
How to pass query parameters in URL in Salesforce Summer 19 Release?
Sheared off exhasut pipe: How to fix without a welder?
What do you call a painting on a wall?
Is Iron Man stronger than the Hulk?
Is any special diet an effective treatment of autism?
Would a "Permanence" spell in 5e be overpowered?
How to remap repeating commands i.e. <number><command>?
Where are the "shires" in the UK?
It isn’t that you must stop now
Should homeowners insurance cover the cost of the home?
Dirichlet series with a single zero
Gerrymandering Puzzle - Rig the Election
weird pluperfect subjunctive in Eutropius
Madam I m Adam..please don’t get mad..you will no longer be prime
Python Multiple Function Composition
Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonUsing global variables in a functionHow can I make a time delay in Python?How to make a chain of function decorators?Does Python have a string 'contains' substring method?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
So i have a homework question, but I'm not sure why I got it wrong / how it works.
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))
My ans: 25 -> 8*2 +9
Actual ans: 11 -> 2 + 9
What I was thinking:
thrice -> f(f(f(x))),
let new_x = twice(x)
thrice -> f(f(new_x)),
let new_x2 = twice(new_x)
thrice -> f(new_x2),
let new_thrice = twice(new_x2)
so afterwards I add in the (once)
and did new_thrice(once)(lambda x: x+2)(9)
But answer seems to be that (once)
nullifies the earlier thrice(twice)
and am lost about. Would be great if someone has an explanation.. Thanks!
python
add a comment |
So i have a homework question, but I'm not sure why I got it wrong / how it works.
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))
My ans: 25 -> 8*2 +9
Actual ans: 11 -> 2 + 9
What I was thinking:
thrice -> f(f(f(x))),
let new_x = twice(x)
thrice -> f(f(new_x)),
let new_x2 = twice(new_x)
thrice -> f(new_x2),
let new_thrice = twice(new_x2)
so afterwards I add in the (once)
and did new_thrice(once)(lambda x: x+2)(9)
But answer seems to be that (once)
nullifies the earlier thrice(twice)
and am lost about. Would be great if someone has an explanation.. Thanks!
python
In general, it is bad form to assignlambdas
to variables - prefer defining named functions withdef
instead.
– gmds
Mar 23 at 4:01
The code provided outputs 11. Where are you getting 25 from?
– alec_a
Mar 23 at 4:23
I was supposed to write it on paper lol
– LostChild
Mar 23 at 7:48
add a comment |
So i have a homework question, but I'm not sure why I got it wrong / how it works.
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))
My ans: 25 -> 8*2 +9
Actual ans: 11 -> 2 + 9
What I was thinking:
thrice -> f(f(f(x))),
let new_x = twice(x)
thrice -> f(f(new_x)),
let new_x2 = twice(new_x)
thrice -> f(new_x2),
let new_thrice = twice(new_x2)
so afterwards I add in the (once)
and did new_thrice(once)(lambda x: x+2)(9)
But answer seems to be that (once)
nullifies the earlier thrice(twice)
and am lost about. Would be great if someone has an explanation.. Thanks!
python
So i have a homework question, but I'm not sure why I got it wrong / how it works.
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))
My ans: 25 -> 8*2 +9
Actual ans: 11 -> 2 + 9
What I was thinking:
thrice -> f(f(f(x))),
let new_x = twice(x)
thrice -> f(f(new_x)),
let new_x2 = twice(new_x)
thrice -> f(new_x2),
let new_thrice = twice(new_x2)
so afterwards I add in the (once)
and did new_thrice(once)(lambda x: x+2)(9)
But answer seems to be that (once)
nullifies the earlier thrice(twice)
and am lost about. Would be great if someone has an explanation.. Thanks!
python
python
asked Mar 23 at 3:29
LostChildLostChild
133
133
In general, it is bad form to assignlambdas
to variables - prefer defining named functions withdef
instead.
– gmds
Mar 23 at 4:01
The code provided outputs 11. Where are you getting 25 from?
– alec_a
Mar 23 at 4:23
I was supposed to write it on paper lol
– LostChild
Mar 23 at 7:48
add a comment |
In general, it is bad form to assignlambdas
to variables - prefer defining named functions withdef
instead.
– gmds
Mar 23 at 4:01
The code provided outputs 11. Where are you getting 25 from?
– alec_a
Mar 23 at 4:23
I was supposed to write it on paper lol
– LostChild
Mar 23 at 7:48
In general, it is bad form to assign
lambdas
to variables - prefer defining named functions with def
instead.– gmds
Mar 23 at 4:01
In general, it is bad form to assign
lambdas
to variables - prefer defining named functions with def
instead.– gmds
Mar 23 at 4:01
The code provided outputs 11. Where are you getting 25 from?
– alec_a
Mar 23 at 4:23
The code provided outputs 11. Where are you getting 25 from?
– alec_a
Mar 23 at 4:23
I was supposed to write it on paper lol
– LostChild
Mar 23 at 7:48
I was supposed to write it on paper lol
– LostChild
Mar 23 at 7:48
add a comment |
2 Answers
2
active
oldest
votes
once(lambda x: x+2)
evaluates to a function that applies lambda x: x+2
to its argument. In other words, it's equivalent to lambda x: x+2
.
once(once(lambda x: x+2))
evaluates to a function that applies once(lambda x: x+2)
to its argument. In other words, it's also equivalent to lambda x: x+2
.
once(once(once(lambda x: x+2)))
evaluates to a function that applies once(once(lambda x: x+2))
to its argument. In other words, this is also equivalent to lambda x: x+2
. This doesn't change no matter how many times you apply once
.
thrice(twice)(once)
evaluates to a function that applies once
to its argument some number of times. (8 times, not that it matters for the analysis.) once
doesn't change a function's behavior. No matter how many times you apply once
, the final function only applies the underlying function once.
thrice(twice)(once)(lambda x: x + 2)
thus evaluates to a function that does the same thing as lambda x: x + 2
.
Now, if it had been thrice(twice)(once(lambda x: x + 2))
(note the moved parentheses), then that would have applied thrice(twice)
to once(lambda x: x + 2)
, and the result would be a function that applies lambda x: x + 2
8 times.
oh I see! It seems I had a misconception of starting from thrice(twice) first
– LostChild
Mar 23 at 7:55
add a comment |
I hope this will help you to figure out what is going on!
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
# Created this one to help readability.
custom_func = lambda x: x + 2
print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value
print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.
# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))
# That is it! Hope it helps.
thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha
– LostChild
Mar 23 at 7:56
Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!
– João Luiz
Mar 23 at 12:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310356%2fpython-multiple-function-composition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
once(lambda x: x+2)
evaluates to a function that applies lambda x: x+2
to its argument. In other words, it's equivalent to lambda x: x+2
.
once(once(lambda x: x+2))
evaluates to a function that applies once(lambda x: x+2)
to its argument. In other words, it's also equivalent to lambda x: x+2
.
once(once(once(lambda x: x+2)))
evaluates to a function that applies once(once(lambda x: x+2))
to its argument. In other words, this is also equivalent to lambda x: x+2
. This doesn't change no matter how many times you apply once
.
thrice(twice)(once)
evaluates to a function that applies once
to its argument some number of times. (8 times, not that it matters for the analysis.) once
doesn't change a function's behavior. No matter how many times you apply once
, the final function only applies the underlying function once.
thrice(twice)(once)(lambda x: x + 2)
thus evaluates to a function that does the same thing as lambda x: x + 2
.
Now, if it had been thrice(twice)(once(lambda x: x + 2))
(note the moved parentheses), then that would have applied thrice(twice)
to once(lambda x: x + 2)
, and the result would be a function that applies lambda x: x + 2
8 times.
oh I see! It seems I had a misconception of starting from thrice(twice) first
– LostChild
Mar 23 at 7:55
add a comment |
once(lambda x: x+2)
evaluates to a function that applies lambda x: x+2
to its argument. In other words, it's equivalent to lambda x: x+2
.
once(once(lambda x: x+2))
evaluates to a function that applies once(lambda x: x+2)
to its argument. In other words, it's also equivalent to lambda x: x+2
.
once(once(once(lambda x: x+2)))
evaluates to a function that applies once(once(lambda x: x+2))
to its argument. In other words, this is also equivalent to lambda x: x+2
. This doesn't change no matter how many times you apply once
.
thrice(twice)(once)
evaluates to a function that applies once
to its argument some number of times. (8 times, not that it matters for the analysis.) once
doesn't change a function's behavior. No matter how many times you apply once
, the final function only applies the underlying function once.
thrice(twice)(once)(lambda x: x + 2)
thus evaluates to a function that does the same thing as lambda x: x + 2
.
Now, if it had been thrice(twice)(once(lambda x: x + 2))
(note the moved parentheses), then that would have applied thrice(twice)
to once(lambda x: x + 2)
, and the result would be a function that applies lambda x: x + 2
8 times.
oh I see! It seems I had a misconception of starting from thrice(twice) first
– LostChild
Mar 23 at 7:55
add a comment |
once(lambda x: x+2)
evaluates to a function that applies lambda x: x+2
to its argument. In other words, it's equivalent to lambda x: x+2
.
once(once(lambda x: x+2))
evaluates to a function that applies once(lambda x: x+2)
to its argument. In other words, it's also equivalent to lambda x: x+2
.
once(once(once(lambda x: x+2)))
evaluates to a function that applies once(once(lambda x: x+2))
to its argument. In other words, this is also equivalent to lambda x: x+2
. This doesn't change no matter how many times you apply once
.
thrice(twice)(once)
evaluates to a function that applies once
to its argument some number of times. (8 times, not that it matters for the analysis.) once
doesn't change a function's behavior. No matter how many times you apply once
, the final function only applies the underlying function once.
thrice(twice)(once)(lambda x: x + 2)
thus evaluates to a function that does the same thing as lambda x: x + 2
.
Now, if it had been thrice(twice)(once(lambda x: x + 2))
(note the moved parentheses), then that would have applied thrice(twice)
to once(lambda x: x + 2)
, and the result would be a function that applies lambda x: x + 2
8 times.
once(lambda x: x+2)
evaluates to a function that applies lambda x: x+2
to its argument. In other words, it's equivalent to lambda x: x+2
.
once(once(lambda x: x+2))
evaluates to a function that applies once(lambda x: x+2)
to its argument. In other words, it's also equivalent to lambda x: x+2
.
once(once(once(lambda x: x+2)))
evaluates to a function that applies once(once(lambda x: x+2))
to its argument. In other words, this is also equivalent to lambda x: x+2
. This doesn't change no matter how many times you apply once
.
thrice(twice)(once)
evaluates to a function that applies once
to its argument some number of times. (8 times, not that it matters for the analysis.) once
doesn't change a function's behavior. No matter how many times you apply once
, the final function only applies the underlying function once.
thrice(twice)(once)(lambda x: x + 2)
thus evaluates to a function that does the same thing as lambda x: x + 2
.
Now, if it had been thrice(twice)(once(lambda x: x + 2))
(note the moved parentheses), then that would have applied thrice(twice)
to once(lambda x: x + 2)
, and the result would be a function that applies lambda x: x + 2
8 times.
answered Mar 23 at 6:44
user2357112user2357112
160k13179274
160k13179274
oh I see! It seems I had a misconception of starting from thrice(twice) first
– LostChild
Mar 23 at 7:55
add a comment |
oh I see! It seems I had a misconception of starting from thrice(twice) first
– LostChild
Mar 23 at 7:55
oh I see! It seems I had a misconception of starting from thrice(twice) first
– LostChild
Mar 23 at 7:55
oh I see! It seems I had a misconception of starting from thrice(twice) first
– LostChild
Mar 23 at 7:55
add a comment |
I hope this will help you to figure out what is going on!
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
# Created this one to help readability.
custom_func = lambda x: x + 2
print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value
print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.
# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))
# That is it! Hope it helps.
thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha
– LostChild
Mar 23 at 7:56
Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!
– João Luiz
Mar 23 at 12:31
add a comment |
I hope this will help you to figure out what is going on!
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
# Created this one to help readability.
custom_func = lambda x: x + 2
print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value
print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.
# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))
# That is it! Hope it helps.
thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha
– LostChild
Mar 23 at 7:56
Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!
– João Luiz
Mar 23 at 12:31
add a comment |
I hope this will help you to figure out what is going on!
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
# Created this one to help readability.
custom_func = lambda x: x + 2
print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value
print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.
# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))
# That is it! Hope it helps.
I hope this will help you to figure out what is going on!
once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
# Created this one to help readability.
custom_func = lambda x: x + 2
print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value
print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.
# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))
# That is it! Hope it helps.
edited Mar 23 at 6:36
Matt C
2,39541836
2,39541836
answered Mar 23 at 4:21
João LuizJoão Luiz
264
264
thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha
– LostChild
Mar 23 at 7:56
Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!
– João Luiz
Mar 23 at 12:31
add a comment |
thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha
– LostChild
Mar 23 at 7:56
Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!
– João Luiz
Mar 23 at 12:31
thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha
– LostChild
Mar 23 at 7:56
thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha
– LostChild
Mar 23 at 7:56
Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!
– João Luiz
Mar 23 at 12:31
Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!
– João Luiz
Mar 23 at 12:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310356%2fpython-multiple-function-composition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In general, it is bad form to assign
lambdas
to variables - prefer defining named functions withdef
instead.– gmds
Mar 23 at 4:01
The code provided outputs 11. Where are you getting 25 from?
– alec_a
Mar 23 at 4:23
I was supposed to write it on paper lol
– LostChild
Mar 23 at 7:48