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
SUMMARY

The discussion focuses on creating sublists from a list of numbers in PHP, based on a specified sum threshold. The provided PHP function, makeList, iteratively adds numbers to a sublist until the sum exceeds the threshold, at which point it creates a new sublist. The example uses the input list [1,2,3,2,4,1] with a threshold of 5, resulting in three sublists: [[1,2,3],[2,4],[1]]. The conversation also touches on the possibility of translating the solution into Python.

PREREQUISITES
  • Basic understanding of PHP programming
  • Familiarity with arrays and loops in PHP
  • Knowledge of function definitions and return values in PHP
  • Concept of threshold-based conditions in programming
NEXT STEPS
  • Learn how to implement similar logic in Python using lists and loops
  • Explore advanced array manipulation techniques in PHP
  • Research performance optimization for iterative algorithms
  • Investigate error handling in PHP functions
USEFUL FOR

Developers looking to enhance their PHP skills, educators teaching programming concepts, and anyone interested in algorithmic problem-solving related to list manipulation.

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