Can anyone explain the logic for the following code in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anyone explain the logic for the following code in python

def sub(x,y): return x-y def mult(x,y): return x*y def pr(f1, f2, d): print(f1(f2(d+1, d), d)) pr(mult, sub, 4) answer is 4

14th Oct 2019, 11:12 PM
Aleksandar Mojsoski
Aleksandar Mojsoski - avatar
2 Answers
+ 10
In this code function pr(f1, f2, d) = pr(mult, sub, d) f1 refer to mult function f2 refer to sub function d refer to 4 So for f2 : sub will be (x - y) = (d+1-d) = 1 So f2(d+1, d) = 1 Now print(f1(1, d)) So for f1 : mult will be (x*y) = 1*d = 1*4 = 4 so now f1(1, d) = f1(1, 4) = 4 now print(4) = 4
14th Oct 2019, 11:23 PM
A͢J
A͢J - avatar
+ 5
look at line 6 first. subtraction 5 - 4 = 1. then multiplication 1*4 = 4 then you print.
14th Oct 2019, 11:25 PM
LONGTIE👔
LONGTIE👔 - avatar