Write a function multisplit that consumes two positive integers total and split and produces the number of times total is repeatedly divided into split even pieces before each piece is of size at most 1. For example, the value returned by multisplit(8, 2) will be 3, since 8 can be split into 2 pieces of size 4, which are then each split into 2 pieces of size 2, which are then each split into 2 pieces of size 1 (at which point no further splitting takes place since the pieces are of size at most 1).
total= int(input("Total:"))
split= int(input("Split:"))
def multisplit(total,split):
x=o
while value>=1:
value= total//split
x= x+1
return x
print(x)
It's telling me that the name 'x' is not defined
There are several issues with the code you posted:
In python, the contents of a function must be indented.
def myfunction():
# code inside the function goes here
# code after you've unindented is not in the function
You didn't define your value variable before using it.
Assuming that your final line gets appropriately unindented, so that it won't be completely ignored because of being inside the function, but after the return statement:
You're trying to print the value of a variable that was defined in a different scope. Specifically, you defined x inside the function, and now you're trying to look at it outside the function.
You never called your function...
If I understand what you're trying to do, you want to call the function inside print. i.e.: print(multisplit(total, split))
Related
Actually I am using a function rem() to find the maximum possible integer present in the list?Here the list l contains n elements,from 1 to n.We will do this following operation till n-1 times:
Here I am removing the first two elements of list, X and Y,then I am adding this value (X+Y+(X*Y)) in this list.
Finally after the completion of this operation,the list will contain only one integer,which is the maximum possible integer present in the list,we will return this integer from the list.
I have tried many ways to optimize the statements which are contained inside the body of the loop.My aim is to optimize the code from line 3 to line 5.
def rem(n):
l=list(range(1,n+1))
for i in range(n-1):
l.append(l[0]+l[1]+(l[0]*l[1]))
del l[0:2]
return (l[0]%1000000007)
Let´s say I have 2 functions like these ones:
def list(n):
l=[x for x in range(n)]
return l
def square(l):
l=list(map(lambda x:x**2,l))
print(l)
The first one makes a list from all numbers in a given range which is "n" and the second one receives a list as a parameter and returns the squared values of this list.
However when I write:
square(list(20))
it raises the error "map object cannot be interpreted as an integer" and whenever I erase one of the functions above and run the other one it runs perfectly and I have no idea what mistake I made.
You redefined the standard function list()! Rename it to my_list() and clean the code accordingly.
As a side note, your function list() is doing exactly what list(range(n)) would do. Why do you need it at all? In fact, for most purposes (including your example), range(n) alone is sufficient.
Finally, you do not pass a function as a parameter. You pass the value generated by another function. It is not the same.
k = reduce((lambda x,y,z : abs(x-y) + abs(y-z) + abs(z-x)) , range(List[0]))
is giving runtime error.
error message:- 'TypeError: () missing 1 required positional argument:
z'.
What could be the solution for it?
Your problem is (most likely, it is a little hard to judge without the rest of the code...) that you're using reduce (which takes a function with two arguments as its first argument) and pass it a function which requires three arguments. reduce then calls your anonymous function with two arguments which results in the exception.
As a side comment: I suggest avoiding the use of capitalized names (such as "List") for your variables. To most python programmers a capitalized name implies that a class.
From help(reduce)
reduce(...)
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
Seems like reduce passes only 2 parameters at a time and reduces them.
I am interested in creating a list / array of functions "G" consisting of many small functions "g". This essentially should correspond to a series of functions 'evolving' in time.
Each "g" takes-in two variables and returns the product of these variables with an outside global variable indexed at the same time-step.
Assume obs_mat (T x 1) is a pre-defined global array, and t corresponds to the time-steps
G = []
for t in range(T):
# tried declaring obs here too.
def g(current_state, observation_noise):
obs = obs_mat[t]
return current_state * observation_noise * obs
G.append(g)
Unfortunately when I test the resultant functions, they do not seem to pick up on the difference in the obs time-varying constant i.e. (Got G[0](100,100) same as G[5](100,100)). I tried playing around with the scope of obs but without much luck. Would anyone be able to help guide me in the right direction?
This is a common "gotcha" to referencing variables from an outer scope when in an inner function. The outer variable is looked up when the inner function is run, not when the inner function is defined (so all versions of the function see the variable's last value). For each function to see a different value, you either need to make sure they're looking in separate namespaces, or you need to bind the value to a default parameter of the inner function.
Here's an approach that uses an extra namespace:
def make_func(x):
def func(a, b):
return a*b*x
return func
list_of_funcs = [make_func(i) for i in range(10)]
Each inner function func has access to the x parameter in the enclosing make_func function. Since they're all created by separate calls to make_func, they each see separate namespaces with different x values.
Here's the other approach that uses a default argument (with functions created by a lambda expression):
list_of_funcs = [lambda a, b, x=i: a*b*x for i in range(10)]
In this version, the i variable from the list comprehension is bound to the default value of the x parameter in the lambda expression. This binding means that the functions wont care about the value of i changing later on. The downside to this solution is that any code that accidentally calls one of the functions with three arguments instead of two may work without an exception (perhaps with odd results).
The problem you are running into is one of scoping. Function bodies aren't evaluated until the fuction is actually called, so the functions you have there will use whatever is the current value of the variable within their scope at time of evaluation (which means they'll have the same t if you call them all after the for-loop has ended)
In order to see the value that you would like, you'd need to immediately call the function and save the result.
I'm not really sure why you're using an array of functions. Perhaps what you're trying to do is map a partial function across the time series, something like the following?
from functools import partial
def g(current_state, observation_noise, t):
obs = obs_mat[t]
return current_state * observation_noise * obs
g_maker = partial(g, current, observation)
results = list(map(g_maker, range(T)))
What's happening here is that partial creates a partially-applied function, which is merely waiting for its final value to be evaluated. That final value is dynamic (but the first two are fixed in this example), so mapping that partially-applied function over a range of values gets you answers for each value.
Honestly, this is a guess because it's hard to see what else you are trying to do with this data and it's hard to see what you're trying to achieve with the array of functions (and there are certainly other ways to do this).
The issue (assuming that your G.append call is mis-indented) is simply that the name t is mutated when you loop over the iterator returned by range(T). Since every function g you create stores returns the same name t, they wind up all returning the same value, T - 1. The fix is to de-reference the name (the simplest way to do this is by sending t into your function as a default value for an argument in g's argument list):
G = []
for t in range(T):
def g(current_state, observation_noise, t_kw=t):
obs = obs_mat[t_kw]
return current_state * observation_noise * obs
G.append(g)
This works because it creates another name that points at the value that t references during that iteration of the loop (you could still use t rather than t_kw and it would still just work because tg is bound to the value that tf is bound to - the value never changes, but tf is bound to another value on the next iteration, while tg still points at the "original" value.
Functions can only return a single value but sometimes, we may want functions to return multiple values. Tuples can come in handy in such cases. We can create a tuple containing multiple values and return the tuple instead of a single value.
Write a function max_and_min that accepts a tuple containing integer elements as an argument and returns the largest and smallest integer within the tuple. The return value should be a tuple containing the largest and smallest value, in that order.
for example: max_and_min((1, 2, 3, 4, 5)) = (5, 1)
I am told to use an iteration to loop through each value of the tuple parameter to find the maximum and minimum values. Also, I must use Python 3.x.
How do I do this? I am really clueless. Thanks for your help!
def max_and_min(values):
# Write your code here
You are looking to pass a variable number of arguments to a function. In python, you can get multiple arguments passed at invocation with the * notation:
def max_and_min(*arg):
return (max(arg), min(arg))
Note that the Python 3 min and max functions themselves accept a variable number of arguments.