I made a function which takes a list of 2d elements(containing lists with 2 elements) as an argument and returns the element(or elements) whose elements' difference is the biggest and the one(or more) whose elements' difference is the smallest. For example, given an argument ([2,8],[3,4],[2,7],[4,10]), the function would return: max=([2,8], [4,10]), min=[3,4].
I have made a function that does that, but the code is rather big, without me adding the part which I fill the list to be passed as argument with user input,which I also want to do.
def maxminIntervals(lst):
mx=lst[0][1]-lst[0][0]
mn=mx
count_max=count_min=0
max=[]
min=[]
print(max,min)
print(mx,mn)
for element in lst:
y=element[1]-element[0]
if y>mx:
max=[]
max.append(element)
count_max=0
mx=y
elif y==mx:
max.append(element)
mx=y
count_max+=1
if y<mn:
min=[]
min.append(element)
count_min=0
mn=y
elif y==mn:
min.append(element)
mn=y
count_min+=1
print(y)
print("Max=",end='')
if count_max>0:
print("(",end=" ")
for i in max:
print(i,end=' ')
if count_max>0:
print(")",end=" ")
print("\n")
print("Min=",end=' ')
if count_min>0:
print("(",end=" ")
for i in min:
print(i,end=' ')
if count_min>0:
print(")",end=" ")
It seems to me the code is too big for Python. Is there a simple shortcut(built-in function, etc) to make it shorter?