-1

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?

SiHa
  • 7,830
  • 13
  • 34
  • 43
Bruffff
  • 53
  • 7
  • Hi the same question is here https://stackoverflow.com/questions/54269966/grouping-the-nearest-elements-from-a-list-in-terms-of-difference You can use that approach – Mehrdad Dolatabadi Nov 08 '21 at 08:58
  • 1
    Not really the same question. The question asked by Bruffff seems more complicated, since adding elements to your list may completely change where you split it. – Be Chiller Too Nov 08 '21 at 09:00
  • @MehrdadDolatabadi yea that question uses itertools, and I would like to refrain fromusing that :). – Bruffff Nov 08 '21 at 09:04
  • What is the expected output of `grouping([1,3,5,7,12,14,15], 3)`? – Be Chiller Too Nov 08 '21 at 09:07
  • the expected output would be [(1,3,) (5,7) (14,15)]. If the difference is same for all of them, just seperate the middle numbers, thats it. – Bruffff Nov 08 '21 at 09:11
  • the mid of all the numbers with the same difference I mean – Bruffff Nov 08 '21 at 09:11
  • Why split the middle numbers? Since the diff between 1/3 is the same as 3/5 and 5/7. – Be Chiller Too Nov 08 '21 at 09:11
  • yea, thats kinda like default. If the difference is the same then I would like to just take the middle two numbers of that list and then return it. – Bruffff Nov 08 '21 at 09:46
  • @Bruffff give me the assert for n=1, n=2 and n=3 please? – Vincent Bénet Nov 08 '21 at 11:06
  • Does this answer your question? [How can I make my output group all similar numbers into a specific number of groups?](https://stackoverflow.com/questions/69878871/how-can-i-make-my-output-group-all-similar-numbers-into-a-specific-number-of-gro) – Bibhav Nov 08 '21 at 15:02
  • @Bibhav yea but I would like to answer it without using key = lambda and reverse = true and start = 0. They are all good and work fine, I just want a code that a noobie like me can understand – Bruffff Nov 09 '21 at 00:15

1 Answers1

0

here my answer to your problem:

import numpy

def grouping(input_user, n):
    diff = numpy.diff(input_user)
    heighests = numpy.sort(diff)[-n:]
    results = []
    result = [input_user[0]]
    index = 0
    for i, cell in enumerate(input_user[1:]):
        if diff[i] in heighests:
            heighests = numpy.delete(heighests, numpy.where(heighests == diff[i])[0][0])
            result.append(cell)
            results.append(result)
            result = []
        result.append(cell)
    if result:
        results.append(result)
    return results

for n in range(1, 4):
    print(f"n={n}: {grouping(numpy.array([1,3,5,7,12,14,15]), n)}")

Result:

n=1: [[1, 3, 5, 7, 12], [14, 15]]                                                                                       
n=2: [[1, 3], [5, 7, 12], [14, 15]]                                                                                     
n=3: [[1, 3], [5], [7, 12], [14, 15]] 
Vincent Bénet
  • 1,212
  • 6
  • 20