How Values are assigned in Anonymous function? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I don't understand whether 15 is sent to x or n
def func_compute(n):
return lambda x : x * n
result = func_compute(2)
print("Double the number of 15 =", result(15))

Assume a function f(x, n) = xn. Now you want to have this function for a fixed n, i.e., f_n(x) = xn. This is what func_compute(n) does, it returns f_n(x), a function, for the given n.
result(15) is then f_2(15) = f(15, 2) = 30.

Related

My code is running fine but I don't know why it shows 16 instead of (16, 2)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
The answer always comes out to be 16 but what happens to the x in function sq(func,x) because func turned into 16 but x remains 2. So it should show something like 16, 2 when I try to print it.
def sq(func,x):
y = x**2
return func(y)
def f(x):
return x**2
Calc = sq(f,2)
print(Calc)
Your print statement prints cal, which is the number returned by the function f(x), which is what is returned by the function sq(f,y).
If you want to print the structure that you need, try to do the following:
def sq(func,x):
y = x**2
return (func(y), x)
def f(x):
return x**2
Calc = sq(f,2)
print(Calc)

Python Program using List [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Accept N numbers in the list L. Shift the first half element to second half and second half elements to first half. Eg. L = [1,2,3,4,5,6] after shifting L = [4,5,6,1,2,3].
If L is a list of length N:
L = L[N // 2:] + L[:N // 2]
will switch the first and second halves of the list.

writing code - a compare function with two values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to write a program that takes two values x and y and returns
1 if x > y
0 if x == y
-1 if x < y
I cannot figure out how to start this. I know how to do one value, but not sure where to add the second.
This should work:
def func (x, y):
if x>y:
return 1
elif x==y:
return 0
else:
return -1
To get return:
output=func(4, 6)
Or whatever

append all Integers of an integer in a list in python using recursive function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need an example of a recursive function that will append all integer elements of a number into a list using python and doing this with the most basic algorithm.
For example if n = 1234, list will be [1,2,3,4].
Try this?
def numberToList(number):
# base case
if number == 0:
return []
# recurse
return numberToList(number / 10) + [ number % 10 ]
Run it like this:
>>> numberToList(1234)
[4, 3, 2, 1]

Iterator python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was trying to do something like this for example
def functionx(x):
while x > 0:
x = 2 + 2
x -= 1
for x in functionx(x):
print(x)
Well, in for I wanted to print x = 2 + 2 but it just give me the direction where the function is.
Also just wanted to use x = 2 + 2, use it in another function but then use the stored number again and so on but I don't know how to do that.
Use the yield keyword.
Example
def functionx(x):
while x > 0:
x += 1
yield x
for i in functionx(1):
print i
This creates the functionx as an iterator.

Categories

Resources