1

I am pretty beginner in Python and trying to do the following:

main_list[80,80,30,30,30,30,20,10,5,4,3,2,1] #list of integers - slicing the main_list in multiple lists for example list1,2,3,..,n with a sum of sub lists < 100 for i in range of n: print(list(i)) list1[80,20], list2[80,10,5,4,1], list3[30,30,30], listn[30,3,2]

Thanks!

Nayed
  • 11
  • 1
  • Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Joe Apr 25 '20 at 07:23
  • What are you actually asking for? Maybe code formatting is needed. – Alexey Novakov Apr 25 '20 at 14:17
  • I saw the sline notation but it is not exactly what I need. I am really new in Python and I tried to explain in more details in another comments in the post – Nayed Apr 29 '20 at 18:31

1 Answers1

0

It's not really clear to me what you consider an acceptable output so I'm assuming that it's any list where its elements sum less than 100.

The solution I found is using recursion. For the list [a, b, c, d] we are going to check the condition for this sublists:

[a]
[a, b] (if the condition for [a] is met)
[a, b, c] (if the condition for [a, b] is met)
[a, b, c, d] (if the condition for [a, b, c] is met)
[a, c] (if the condition for [a] is met)
[a, c, d] (if the condition for [a, c] is met)
[a, d] (if the condition for [a] is met)

[b]
[b, c] (if the condition for [b] is met)
[b, c, d] (if the condition for [b, c] is met)
[b, d] (if the condition for [b] is met)

[c]
[c, d] (if the condition for [c] is met)

[d]

The concept is that for the "n" element in the list we are going to look for the sublists of size "n - 1" to 0 (so the element itself) that meet the requirements. The sublists are formed by the elements at the right of the studied element of each iteration, so for the first 30, the sublist to use would be [30, 30, 30, 20, 10, 5, 4, 3, 2, 1]

This process of finding the sublists for each element is the one that uses recursion. It calls itself for each element of the sublists checking if it meets the condition. For the example above, if the condition is met for [a, b] then it will also try for [a, b, c] and [a, b, d] (by calling itself with the sum of (a, b) and the sublist [c, d].

I've added a few prints so you can study how it works, but you should just use the results variable at the end of the script for getting your results.

main_list = [80,80,30,30,30,30,20,10,5,4,3,2,1] 

def less_than_hundred(input) -> bool:
    return input < 100

def sublists_meet_condition(condition, input):
    """
    This function is used to call the sublists_for_element function with every element in the original list and its sublist:
    - For the first element (80) it calls the second function with the sublist [80,30,30,30,30,20,10,5,4,3,2,1]
    - For the fifth element (30) it calls the second function with the sublist [30,20,10,5,4,3,2,1]
    Its purpose is to collect all the sublists that meet the requirements for each element
    """
    results = []
    for index, element in enumerate(input):
        print('Iteration {} - Element {}'.format(index, element))
        if condition(element):
            results.append([element])
            print('{} = {}'.format([element], element))
            num_elements = len(input) - index
            main_element = element
            sublist = input[index+1:]
            for result in sublists_for_element(condition, main_element, sublist):
                new_result = [element] + result
                sum_new_result = sum(new_result)
                results.append(new_result)
                print('{} = {}'.format([element] + result, sum_new_result))
    return results

def sublists_for_element(condition, sum_main_elements, sublist):
    """
    This function is used to check every sublist with the given condition.
    The variable sum_main_elements allows the function to call itself and check if for a given list of numbers that meet the conditions [30, 30, 4] for example, any of the elements of the remaining sublists also meets the condition for example adding the number 3 still meets the condition.
    Its purpose is to return all the sublists that meet the requirements for the given sum of main elements and remaining sublist
    """
    num_elements = '{}{}'.format('0' if len(sublist) + 1 < 10 else '',len(sublist) + 1)
    #print('Elements: {} | Main element: {} | Sublist: {}'.format(num_elements, sum_main_elements, sublist))
    result = []
    for index, element in enumerate(sublist):
        if condition(sum_main_elements + element):
            result.append([element])
            sublist_results = sublists_for_element(condition, sum_main_elements + element, sublist[index+1:])
            for sublist_result in sublist_results:
                result.append([element] + sublist_result)
    return result

results = sublists_meet_condition(less_than_hundred, main_list)
Samuel O.D.
  • 346
  • 2
  • 13
  • Thanks for your answer! I need to clarify something. My idea was to make a sum of all elements of the list in the example above 325. This means 325/100 it is 3.25 => which means 4 sublists. Now in this sublists I would like to add each element a,b,c,d but to have list1 [a,h,i,j] (80,10,5,4) closest sum <100. NOW in the second list2 [b,k,l,m] (80,3,2,1) I do not want to repeat a,h,i,j as I already have them in list1, I need to take the elements from the first sublist OUT and to create the list2, and in the same way list3 and list4 – Nayed Apr 29 '20 at 18:35