Python dual function recursive vs iterative functionCalling 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?Does Python have a ternary conditional operator?Using global variables in a functionHow to make a chain of function decorators?Iterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?

How would I as a DM create a smart phone-like spell/device my players could use?

Why does Intel's Haswell chip allow multiplication to be twice as fast as addition?

Is TA-ing worth the opportunity cost?

Keeping a Weakness Secret

Why couldn't soldiers sight their own weapons without officers' orders?

Plausibility of Ice Eaters in the Arctic

English - Acceptable use of parentheses in an author's name

Why can I log in to my Facebook account with a misspelled email/password?

Does the United States guarantee any unique freedoms?

Does this Foo machine halt?

Replace data between quotes in a file

Can a College of Swords bard use Blade Flourishes multiple times in a turn?

As a 16 year old, how can I keep my money safe from my mother?

Why does this Pokémon I just hatched need to be healed?

Geometric programming: Why are the constraints defined to be less than/equal to 1?

How can I read one message at a time from /var/mail

Is there a way to create a report for the failed entries while calling REST API

SQL Minimum Row count

How does The Fools Guild make its money?

Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?

Shabbat clothing on shabbat chazon

Team goes to lunch frequently, I do intermittent fasting but still want to socialize

Reusing story title as chapter title

Word or idiom defining something barely functional



Python dual function recursive vs iterative function


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?Does Python have a ternary conditional operator?Using global variables in a functionHow to make a chain of function decorators?Iterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Basically, I'm not sure what's wrong with my answer, versus the model answer.
What I'm trying to achieve here is to produce a function that can do lambda x: f(g(f(g(f(g(f(x))))))) # for n == 7 or lambda x: f(g(f(g(x)))) # for n == 4



What I've tried



def dual_function(f, g, n):
if n == 1:
return f
elif n % 2 == 0:
return lambda x: dual_function(f,g, n-1)(g(x))
else:
return lambda x: dual_function(f,g, n-1)(f(x))
# the code seems to do the above from my understanding?
# it starts off at n == 7: new_x = f(x)
# n == 6: new_x = g(new_x)
# n == 5: new_x = f(new_x)
# and so continues down...


The model answer (Sorry i got the wrong model answer for reference, here's the proper one, but now both actually works lol)



 def dual_function(f,g,n):
def helper(x):
f1,g1 = f,g
if n%2==0:
f1,g1 = g1,f1
for i in range(n):
x = f1(x)
f1,g1= g1,f1
return x
return helper


Sample example



f = lambda x: x+1
g = lambda x: x/2
print(dual_function(f, g, 7)(1))
# correct answer is 0.9375, versus my wrong answer: 2.0









share|improve this question


























  • What's the range for n?

    – dcg
    Mar 27 at 6:31











  • probably something around sys.getrecursionlimit() which defaults to 1000

    – Aemyl
    Mar 27 at 6:33











  • Your code gives 2 and f(g(f(g(f(g(f(1))))))) = 2. So the code matches the description. I guess you and the model simply solve slightly different problems.

    – Poolka
    Mar 27 at 7:47

















0















Basically, I'm not sure what's wrong with my answer, versus the model answer.
What I'm trying to achieve here is to produce a function that can do lambda x: f(g(f(g(f(g(f(x))))))) # for n == 7 or lambda x: f(g(f(g(x)))) # for n == 4



What I've tried



def dual_function(f, g, n):
if n == 1:
return f
elif n % 2 == 0:
return lambda x: dual_function(f,g, n-1)(g(x))
else:
return lambda x: dual_function(f,g, n-1)(f(x))
# the code seems to do the above from my understanding?
# it starts off at n == 7: new_x = f(x)
# n == 6: new_x = g(new_x)
# n == 5: new_x = f(new_x)
# and so continues down...


The model answer (Sorry i got the wrong model answer for reference, here's the proper one, but now both actually works lol)



 def dual_function(f,g,n):
def helper(x):
f1,g1 = f,g
if n%2==0:
f1,g1 = g1,f1
for i in range(n):
x = f1(x)
f1,g1= g1,f1
return x
return helper


Sample example



f = lambda x: x+1
g = lambda x: x/2
print(dual_function(f, g, 7)(1))
# correct answer is 0.9375, versus my wrong answer: 2.0









share|improve this question


























  • What's the range for n?

    – dcg
    Mar 27 at 6:31











  • probably something around sys.getrecursionlimit() which defaults to 1000

    – Aemyl
    Mar 27 at 6:33











  • Your code gives 2 and f(g(f(g(f(g(f(1))))))) = 2. So the code matches the description. I guess you and the model simply solve slightly different problems.

    – Poolka
    Mar 27 at 7:47













0












0








0








Basically, I'm not sure what's wrong with my answer, versus the model answer.
What I'm trying to achieve here is to produce a function that can do lambda x: f(g(f(g(f(g(f(x))))))) # for n == 7 or lambda x: f(g(f(g(x)))) # for n == 4



What I've tried



def dual_function(f, g, n):
if n == 1:
return f
elif n % 2 == 0:
return lambda x: dual_function(f,g, n-1)(g(x))
else:
return lambda x: dual_function(f,g, n-1)(f(x))
# the code seems to do the above from my understanding?
# it starts off at n == 7: new_x = f(x)
# n == 6: new_x = g(new_x)
# n == 5: new_x = f(new_x)
# and so continues down...


The model answer (Sorry i got the wrong model answer for reference, here's the proper one, but now both actually works lol)



 def dual_function(f,g,n):
def helper(x):
f1,g1 = f,g
if n%2==0:
f1,g1 = g1,f1
for i in range(n):
x = f1(x)
f1,g1= g1,f1
return x
return helper


Sample example



f = lambda x: x+1
g = lambda x: x/2
print(dual_function(f, g, 7)(1))
# correct answer is 0.9375, versus my wrong answer: 2.0









share|improve this question
















Basically, I'm not sure what's wrong with my answer, versus the model answer.
What I'm trying to achieve here is to produce a function that can do lambda x: f(g(f(g(f(g(f(x))))))) # for n == 7 or lambda x: f(g(f(g(x)))) # for n == 4



What I've tried



def dual_function(f, g, n):
if n == 1:
return f
elif n % 2 == 0:
return lambda x: dual_function(f,g, n-1)(g(x))
else:
return lambda x: dual_function(f,g, n-1)(f(x))
# the code seems to do the above from my understanding?
# it starts off at n == 7: new_x = f(x)
# n == 6: new_x = g(new_x)
# n == 5: new_x = f(new_x)
# and so continues down...


The model answer (Sorry i got the wrong model answer for reference, here's the proper one, but now both actually works lol)



 def dual_function(f,g,n):
def helper(x):
f1,g1 = f,g
if n%2==0:
f1,g1 = g1,f1
for i in range(n):
x = f1(x)
f1,g1= g1,f1
return x
return helper


Sample example



f = lambda x: x+1
g = lambda x: x/2
print(dual_function(f, g, 7)(1))
# correct answer is 0.9375, versus my wrong answer: 2.0






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 11:17







LostChild

















asked Mar 27 at 6:27









LostChildLostChild

133 bronze badges




133 bronze badges















  • What's the range for n?

    – dcg
    Mar 27 at 6:31











  • probably something around sys.getrecursionlimit() which defaults to 1000

    – Aemyl
    Mar 27 at 6:33











  • Your code gives 2 and f(g(f(g(f(g(f(1))))))) = 2. So the code matches the description. I guess you and the model simply solve slightly different problems.

    – Poolka
    Mar 27 at 7:47

















  • What's the range for n?

    – dcg
    Mar 27 at 6:31











  • probably something around sys.getrecursionlimit() which defaults to 1000

    – Aemyl
    Mar 27 at 6:33











  • Your code gives 2 and f(g(f(g(f(g(f(1))))))) = 2. So the code matches the description. I guess you and the model simply solve slightly different problems.

    – Poolka
    Mar 27 at 7:47
















What's the range for n?

– dcg
Mar 27 at 6:31





What's the range for n?

– dcg
Mar 27 at 6:31













probably something around sys.getrecursionlimit() which defaults to 1000

– Aemyl
Mar 27 at 6:33





probably something around sys.getrecursionlimit() which defaults to 1000

– Aemyl
Mar 27 at 6:33













Your code gives 2 and f(g(f(g(f(g(f(1))))))) = 2. So the code matches the description. I guess you and the model simply solve slightly different problems.

– Poolka
Mar 27 at 7:47





Your code gives 2 and f(g(f(g(f(g(f(1))))))) = 2. So the code matches the description. I guess you and the model simply solve slightly different problems.

– Poolka
Mar 27 at 7:47












1 Answer
1






active

oldest

votes


















2














Your code and the model code seem to be solving different problems. Your code always starts with f(...) as the outermost call (and the innermost call can vary depending on whether n is even or odd), while the reference code always has g(x) as the innermost call (and the outermost call can vary).



So the reason your function isn't matching for n=7 is that you're computing f(g(f(g(f(g(f(x))))))) while the other function is doing g(f(g(f(g(f(g(x))))))). Unfortunately, I can't tell you which of those is what you're actually supposed to be computing.






share|improve this answer

























  • As per the question, the objective is to have f(x) as the outermost function.

    – Abhijeetk431
    Mar 27 at 7:00











  • Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.

    – Abhijeetk431
    Mar 27 at 7:03







  • 1





    @Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.

    – Blckknght
    Mar 27 at 7:14











  • It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.

    – Abhijeetk431
    Mar 27 at 7:21












  • No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.

    – Blckknght
    Mar 27 at 7:28










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371060%2fpython-dual-function-recursive-vs-iterative-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Your code and the model code seem to be solving different problems. Your code always starts with f(...) as the outermost call (and the innermost call can vary depending on whether n is even or odd), while the reference code always has g(x) as the innermost call (and the outermost call can vary).



So the reason your function isn't matching for n=7 is that you're computing f(g(f(g(f(g(f(x))))))) while the other function is doing g(f(g(f(g(f(g(x))))))). Unfortunately, I can't tell you which of those is what you're actually supposed to be computing.






share|improve this answer

























  • As per the question, the objective is to have f(x) as the outermost function.

    – Abhijeetk431
    Mar 27 at 7:00











  • Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.

    – Abhijeetk431
    Mar 27 at 7:03







  • 1





    @Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.

    – Blckknght
    Mar 27 at 7:14











  • It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.

    – Abhijeetk431
    Mar 27 at 7:21












  • No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.

    – Blckknght
    Mar 27 at 7:28















2














Your code and the model code seem to be solving different problems. Your code always starts with f(...) as the outermost call (and the innermost call can vary depending on whether n is even or odd), while the reference code always has g(x) as the innermost call (and the outermost call can vary).



So the reason your function isn't matching for n=7 is that you're computing f(g(f(g(f(g(f(x))))))) while the other function is doing g(f(g(f(g(f(g(x))))))). Unfortunately, I can't tell you which of those is what you're actually supposed to be computing.






share|improve this answer

























  • As per the question, the objective is to have f(x) as the outermost function.

    – Abhijeetk431
    Mar 27 at 7:00











  • Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.

    – Abhijeetk431
    Mar 27 at 7:03







  • 1





    @Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.

    – Blckknght
    Mar 27 at 7:14











  • It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.

    – Abhijeetk431
    Mar 27 at 7:21












  • No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.

    – Blckknght
    Mar 27 at 7:28













2












2








2







Your code and the model code seem to be solving different problems. Your code always starts with f(...) as the outermost call (and the innermost call can vary depending on whether n is even or odd), while the reference code always has g(x) as the innermost call (and the outermost call can vary).



So the reason your function isn't matching for n=7 is that you're computing f(g(f(g(f(g(f(x))))))) while the other function is doing g(f(g(f(g(f(g(x))))))). Unfortunately, I can't tell you which of those is what you're actually supposed to be computing.






share|improve this answer













Your code and the model code seem to be solving different problems. Your code always starts with f(...) as the outermost call (and the innermost call can vary depending on whether n is even or odd), while the reference code always has g(x) as the innermost call (and the outermost call can vary).



So the reason your function isn't matching for n=7 is that you're computing f(g(f(g(f(g(f(x))))))) while the other function is doing g(f(g(f(g(f(g(x))))))). Unfortunately, I can't tell you which of those is what you're actually supposed to be computing.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 6:45









BlckknghtBlckknght

68.2k8 gold badges66 silver badges114 bronze badges




68.2k8 gold badges66 silver badges114 bronze badges















  • As per the question, the objective is to have f(x) as the outermost function.

    – Abhijeetk431
    Mar 27 at 7:00











  • Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.

    – Abhijeetk431
    Mar 27 at 7:03







  • 1





    @Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.

    – Blckknght
    Mar 27 at 7:14











  • It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.

    – Abhijeetk431
    Mar 27 at 7:21












  • No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.

    – Blckknght
    Mar 27 at 7:28

















  • As per the question, the objective is to have f(x) as the outermost function.

    – Abhijeetk431
    Mar 27 at 7:00











  • Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.

    – Abhijeetk431
    Mar 27 at 7:03







  • 1





    @Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.

    – Blckknght
    Mar 27 at 7:14











  • It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.

    – Abhijeetk431
    Mar 27 at 7:21












  • No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.

    – Blckknght
    Mar 27 at 7:28
















As per the question, the objective is to have f(x) as the outermost function.

– Abhijeetk431
Mar 27 at 7:00





As per the question, the objective is to have f(x) as the outermost function.

– Abhijeetk431
Mar 27 at 7:00













Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.

– Abhijeetk431
Mar 27 at 7:03






Replacing the f(x) and g(x) the elif and else block of OP's answer would be right as per the description of the question.

– Abhijeetk431
Mar 27 at 7:03





1




1





@Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.

– Blckknght
Mar 27 at 7:14





@Abhijeetk431: I don't understand your second comment. The questioner's recursive code does exactly what it is trying to do, it's the model solution that does something different. In my answer I wanted to raise the possibility that the questioner may have misunderstood their homework assignment. It's also possible that the model solution is wrong, but I have no way to know.

– Blckknght
Mar 27 at 7:14













It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.

– Abhijeetk431
Mar 27 at 7:21






It is a recursive function, so OP's code has f(x) as the innermost call. Seems I made a mistake in my second comment and You made a mistake in your answer. :) I already upvoted your answer. Change that one thing that the OP's code has f(x) as innermost call always. Also, neglect my second comment as it is obviously not right.

– Abhijeetk431
Mar 27 at 7:21














No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.

– Blckknght
Mar 27 at 7:28





No, double check how the OP's code works. It works from the inside out, with the base case being the outermost call. Try it yourself and you'll see that calling their code with n=4 you get f(g(f(g(x)))), not g(f(g(f(x)))) as you seem to be expecting.

– Blckknght
Mar 27 at 7:28








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371060%2fpython-dual-function-recursive-vs-iterative-function%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현