How can we iteratively create sublists from a list based on a sum threshold?

  • Context: Python 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    List
Click For Summary

Discussion Overview

The discussion revolves around the problem of iteratively creating sublists from a list of numbers based on a specified sum threshold. Participants explore how to implement this functionality within a loop, specifically focusing on the programming language Python.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • The original poster (OP) seeks guidance on how to create sublists from a list of numbers [1,2,3,2,4,1] such that each sublist's sum does not exceed a given threshold.
  • One participant provides a PHP code example that demonstrates how to achieve the desired functionality, including the logic for summing values and creating new sublists when the threshold is exceeded.
  • Another participant points out that the OP specified Python in the thread title, implying that the provided PHP solution may not be directly applicable.
  • There is a suggestion that converting the PHP code to Python is left as an exercise for the OP.
  • A later reply humorously reflects on the common experience of students receiving similar responses from teachers.

Areas of Agreement / Disagreement

Participants generally agree on the approach to the problem but have differing views on the programming language to be used, with some focusing on PHP and others emphasizing Python.

Contextual Notes

The discussion does not resolve how to convert the provided PHP code into Python, nor does it address any specific programming challenges that may arise during this conversion.

member 428835
Hello PF!

Initializing with an empty list [], how would we perform the operations [[1]] and then [[1,2]] then [[1,2,3]] then [[1,2,3],[2]] then [[1,2,3], [2,4]] then [[1,2,3],[2,4],[1]] and so on. I'm trying to do this iteratively, so within a for-loop. Basically, given a list of numbers [1,2,3,2,4,1] add them to a sublist until the sum of values of that sublist exceeds a threshold, in which case create a new sublist. I've no clue how to do this, and have googled but to no avail
 
Technology news on Phys.org
You didn't specified a language, but in PHP:

PHP:
<?php
function makeList($numbers, $threshold){
    $list = [];
    $sublist = [];
    $total = 0;

    foreach($numbers as $number){
        $sublist[] = $number;
        $total += $number;
    
        if($total > $threshold){
            $list[] = $sublist;
            $total = 0;
            $sublist = [];       
        }
    }

    if(!empty($sublist)){
        $list[] = $sublist;
    }
    
    return $list;
}

$numbers = [1,2,3,2,4,1];
$threshold = 5;

var_dump(makeList($numbers, $threshold));

Output:

Code:
array(3) {
  [0]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  [1]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    int(4)
  }
  [2]=>
  array(1) {
    [0]=>
    int(1)
  }
}
 
jack action said:
You didn't specified a language
It's in the thread title -- Python.
 
  • Like
Likes   Reactions: jack action
Mark44 said:
It's in the thread title -- Python.
Then it will be left as an exercise for the OP to convert it!
 
  • Haha
Likes   Reactions: jedishrfu
jack action said:
Then it will be left as an exercise for the OP to convert it!
I wonder how many teachers have gotten that response from their students.
 
  • Like
Likes   Reactions: jack action
Got it, thanks for the suggestion!
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
3K
Replies
3
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
8K
Replies
4
Views
4K