How to write a function max_and_min that accepts a tuple containing integer elements as an argument? - python-3.x

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.

Related

Pandas apply function - args how is it passed

df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
d = {'Min.Price': np.nanmean, 'Max.Price': np.nanmedian}
df[['Min.Price', 'Max.Price']] = df[['Min.Price', 'Max.Price']].apply(lambda x, d: x.fillna(d[x.name](x)), args=(d, ))
apply(lambda x, d: x.fillna(d[x.name](x)), args=(d, )) - Am not understanding this part. I know how apply and fillna works but with this args it's confusing.
why does args have comma at the end
what does the d actually pass -
As you know, df.apply function applies a function to the DataFrame's elements. Now this function normally takes only one argument. When you need to use a multi argument function with df.apply, you can specify the other arguments with the args parameter
From the docstring (print(pd.DataFrame.apply.__doc__))
args : tuple
Positional arguments to pass to `func` in addition to the
array/series.
Here you are using two argument function with lambda x, d: therefore you need to specify the 2nd argument with args=. Here it is expecting a tuple with the arguments
To put a single element into a tuple, you need to put it into brackets with a trailing comma.
print(1)
>>>1
print((1,))
>>>(1,)
To put the variable d into a tuple, you need to do (d,)
The dictionary contains two separate functions for the two columns with column names as its keys. So x being the column passed into the lambda function, x.name gives you the column name and d[x.name] gives you the function intended for that column name. Then that function is applied to the column.
For the 'Min.Price' column the function is np.nanmean. So d[x.name](x) is evaluated to np.nanmean(x) which gives you the column mean excluding nans. Now you're using the mean value to fill the nans in the original column by doing x.fillna(d[x.name](x))
To more directly answer your question:
args is a tuple, and a single element tuple requires a trailing comma in order to properly be identified as a tuple and not just a parameterized object.
d is your dictionary, and when it's being passed at the end I believe it is your current list after having the lambda function applied to each element in the dictionary saying that if the element is NaN or N/A (ie Null/missing) then it will fill them with the given value specified. pandas.DataFrame.fillna
Hopefully that helps.

Argument list unpacking

I'm currently trying to retrieve the max value contained in a list, but the max() builtin accept a variable number of floats instead of a single list.
variable "my_list" {
default = [1, 2]
}
output map_out {
value = "${max(var.my_list)}"
}
Error: output.map_out: At column 3, line 1: max: argument 1 should be type float, got type list in:
There's a way to accomplish this?
I think I need something similar to the argument list unpacking in Python, but I don't know how to do this in Terraform.
Function "map" does not accept list, it only accept individual floats.
https://www.terraform.io/docs/configuration/interpolation.html#max-float1-float2-
so only that format will work.
output map_out {
value = "${max(var.my_list[0],var.my_list[1])}"
}
if the number of values can differ - may be sort will work instead.

A Function That Counts The Number of Loops

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

How to create an array of functions which partly depend on outside parameters? (Python)

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.

Two parameters in a predicate function

Is there a way that I can write a predicate function that will compare two strings and see which one is greater? Right now I have
def helper1(x, y):
return x > y
However, I'm trying to use the function in this way,
new_tuple = divide((helper1(some_value, l[0]),l[1:])
Please note that the above function call is probably wrong because my helper1 is incomplete. But the gist is I'm trying to compare two items to see if one's greater than the other, and the items are l[1:] to l[0]
Divide is a function that, given a predicate and a list, divides that list into a tuple that has two lists, based on what the predicate comes out as. Divide is very long, so I don't think I should post it on here.
So given that a predicate should only take one parameter, how should I write it so that it will take one parameter?
You should write a closure.
def helper(x):
def cmp(y):
return x > y
return cmp
...
new_tuple = divide(helper1(l[0]), l[1:])
...

Resources