I have a list of lengths that need to be sorted based on their sum but are not to exceed X. When a sum of the lengths add up to X or would exceed X the sorting our then move onto the next length until all lengths have been used.
This would mean with the numbers provided I would need 5 lengths of 20. I would also wonder if there was someway to add the lengths to 20 efficiently possibly reducing the number from 5 lengths of 20 to 4 lengths of 20.
You can loop through the list and add the number to a temp variable to check if the temp variable is <= 20. If this is the case add the number to a list. Once it goes over 20, reset the temp variable and add the list to a new list, and reset the first list to add the last number, and continue the loop. At the end of the loop if there is a remaining list, add that to the list of lists.
Hope this makes sense
Thank you, if I may. Could this be taken a step further so the max is not only 20, but every number was considered before the finial sum was determined for each list item? This would allow the result list to be reduced to the smallest possible list.