So I wrote this code to find the differences between numbers.
def partition(lst: list):
f = []
l = sorted(f)
for i in range(len(lst)):
if i < len(lst)-1:
diff = lst[i+1] - lst[i]
l.append(diff)
else:
return f
and it works, but now I want to create another function grouping
to separate the list where the difference between two numbers are the greatest. So like for instance if this is my list
[1,3,5,7,12,14,15]
after running partition(lst)
, I get
[2, 2, 2, 5, 2, 1]
So Now I want to let grouping(lst, n)
separate the list where the difference is widest, which is 5, so I want grouping
to return
[(1,3,5,7),(12,14,15)]
Furthermore, in grouping(lst,n)
, there will be an int n
as well, that will determine how many groupings are required. So basically, as long as n <= len(lst), n will be the number of groupings made.
To understand what I mean, so basically if n = 1
, I want grouping(lst,n)
to separate the numbers where the MAXIMUM DIFFERENCE OCCURS.
If n = 2
, grouping(lst,n)
should separate the numbers where the TOP TWO MAXIMUM DIFFERENCES OCCUR.
If n = 3
, 'grouping(lst,n) should separate the numbers where the top 3 maximum differences occur. and so on..
Here is my code so far including grouping(lst,n)
def partition(lst: list):
f = []
for i in range(len(lst)):
if i < len(lst)-1:
diff = lst[i+1] - lst[i]
f.append(diff)
else:
return f
print(partition([1,3,5,7,12,14,15]))
def grouping(lst: list, n):
for x in lst:
if partition(x) == max(partition(lst)):
lst.append(x)
What should I write under grouping(lst,n)
to make it right?