Return the maximum value of a variable array C-plex - opl

I have tried to return the maximum value of the pbefore[t] array using maxl function, but it didn't work. is there any possible way to do it? Thanks in advance.
dvar float dvar float+ pbefore[t];
forall(h in Gwsystem,t in time)
Gw == GW[h].Gwc* maxl(pbefore[t]);

Related

Get the value of a list that produces the maximum value of a calculation

I apologize if this is a duplicate, I tried my best to find an existing question but was unsuccessful.
Recently, I've run into a couple of problems where I've needed to find the element in a list that produces the max/min value when a calculation is performed. For example, a list of real numbers where you want to find out which element produces the highest value when squared. The actual value of the squared number is unimportant, I just need the element(s) from the list that produces it.
I know I can solve the problem by finding the max, then making a pass through the list to find out which values' square matches the max I found:
l = [-0.25, 21.4, -7, 0.99, -21.4]
max_squared = max(i**2 for i in l)
result = [i for i in l if i**2 == max_squared]
but I feel like there should be a better way to do it. Is there a more concise/one-step solution to this?
This will return you just the element which gives the max when squared.
result = max(l, key = lambda k: k**2)
It does not get much better if you need the value in a list f.e. to see how often it occures. You can remeber the source element as well if you do not need that:
l = [-0.25, 21.4, -7, 0.99, -21.4]
max_squared = max( (i**2, i) for i in l) # remeber a tuple, with the result coming first
print(max_squared[1]) # print the source number (2nd element of the tuple)
Output:
21.4
Your calculation does only return the first occurence of abs(24.1) because max only returns one value, not two - if you need both, you still need to do:
print( [k for k in l if abs(k) == max_squared[1]])
to get
[21.4,-21.4]

I am not getting where I am getting wrong

I have written this program to find the maximum of a 1 dimensional array. But, when I entered 3,7,etc.. as number of elements, I am getting list index out of range error. Please help me to solve this query.
t = list(map(int, input().split()))
t = [1,2,3,4,5,6,7,8,9]
def peakf(t):
n=len(t)//2
if(len(t)==2):
if(t[0]>t[n]):
return(t[0])
else:
return(t[1])
else:
if(t[n-1]>t[n]):
return(peakf(t[:n]))
elif(t[n+1]>t[n]):
return(peakf(t[n+1:]))
else:
return(t[n])
print(peakf(t))
you are using n+1 in else condition that is causing index out of range error. use peakf(t[n:]) it will work.
t = [1,2,3,4,5,6,7,8,9]
def peakf(t):
n=len(t)//2
if(len(t)==2):
if(t[0]>t[n]):
return(t[0])
else:
return(t[1])
else:
if(t[n-1]>t[n]):
return(peakf(t[:n]))
elif(t[n+1]>t[n]):
return(peakf(t[n:]))
else:
return(t[n])
print(peakf(t))
If all you are trying to do is find the largest value in an array, why not use something much simpler like the following:
t = [10,2,3,4,5,6,7,8,9]
def peakf(vals):
max = -999999999999
for val in vals:
if val > max:
max = val
return max
print(peakf(t))
It simply loops over every value in the array and checkers it against the current largest value, if it is larger, then the current largest value becomes that value, otherwise it continues.
An even easier way to do this would be to use the max() function.

How to add a float number into a list until the lenght of the list equals N number?

I have to create a program where it asks for the length of a list of float numbers, then it should ask for the float numbers in the list and finally it will print the average of those numbers. Working with Python 3.
I have tried with list.extend and adding the number with an assignment into the list and adding a float input.
numbersInTheList=int(input())
thoseNumbers=[]
while len(thoseNumbers)!=numbersInTheList:
thoseNumbers=thoseNumbers + float(input())
print(sum(thoseNumbers)/numbersInTheList)
I expect the output to be the average of the numbers in the list.
list.extend is used to extend one list with another. In this case, you want to append a single value to an existing list.
numbersInTheList= int(input())
thoseNumbers = []
while len(thoseNumbers) != numbersInTheList:
thoseNumbers.append(float(input()))
print(sum(thoseNumbers)/numbersInTheList)

Python seems to make every variable a float

I tried to write a simple fizzbuzz program in python,
I tried to use isinstance to determine if a number is a float or a int instead of creating a list, but whenever I try it for some reason it considers every number to be a float is this a thing in python or a flaw in my logic?
i = 3
if isinstance(i/3,float) == False:
print("Fizz")
else:
print("???")
The / operator returns a float. Even if the value could be expressed by an integer, you'll still get a float. E.g., 3/3 will return the float value of 1.0. Instead, you could check the remainder directly using the % operator:
if i % 3 == 0:
print("Fizz")
else:
print("???")

Max Sample Value (sound file) python

I am trying to write a code to determine the minimum, maximum, and total zero values for a .wav file. The output should return something like
Largest Sample Value is:xxxx
Smallest Sample Value is:xxxx
There are x amount of 0's in sample.
I am having trouble determining if using the max() for the .wav sample is the right thing to do after using a for loop.
f=pickAFile()
sound=makeSound(f)
for i in range(0,getLength(sound)):
value=getSampleValueAt(sound,i)
print max(value)
print min(value)
Not sure how to find the zero values within it either. I can use the print function to output the smallest and largest statement however I cannot figure out the min/max to start.
Repetition of Max() function using python from loop:
def largest():
f = pickAFile()
sound = makeSound(f)
value = [getSampleValueAt(sound, i) for i in range(1, getLength(sound))]
print max(value)
print min(value)
print value.count(0)
Also, as you tagged the question as python-3.x, the print statements should be print(max(values))

Resources