Version 7 (modified by 9 months ago) ( diff ) | ,
---|
Welcome to Playground project
Sample project flowtimer
see source:flowtimer@master for the master branch
see source:flowtimer@guix for the development branch
from Phase import Phase from RecurringPhaseSequence import RecurringPhaseSequence from Schedule import Schedule p1 = Phase('Huddle', 10) p2 = Phase('Tasking', 5) p3 = Phase('Work', 45) p4 = Phase('Break', 15) seq1 = RecurringPhaseSequence(phase_list = [p1, p2, p3, p4], repetitions=3) unrolled = [[deepcopy(seq) for seq in [each for each in seq1.repetitions * seq1.phase_list]]] schedule = Schedule(unrolled)
Certainly! Let's break down the task with an example:
### Given:
- You have a list of lists.
- You want to split each sublist at the point where the sublist starts with a given symbol.
### Example:
#### Input:
`python
list_of_lists = [
['a', 1, 2, 3], ['b', 4, 5, 6], ['*', 7, 8, 9], ['c', 10, 11], ['*', 12, 13], ['d', 14, 15, 16]
]
symbol = '*'
`
#### Expected Output:
`python
[
'a', 1, 2, 3], ['b', 4, 5, 6, '*', 7, 8, 9], ['c', 10, 11, '*', 12, 13], ['d', 14, 15, 16
]
`
### Steps:
- Initialize an empty result list: This will hold the final split sublists.
- Iterate through each sublist: Check if the first element of the sublist is the given symbol.
- When the symbol is found: Start a new group in the result list.
- Continue grouping until the symbol is encountered again.
### Python Code Implementation:
`python
def split_list_of_lists(list_of_lists, symbol):
result = [] current_group = []
for sublist in list_of_lists:
if sublist[0] == symbol and current_group:
result.append(current_group) current_group = []
current_group.append(sublist)
if current_group:
result.append(current_group)
return result
# Example usage list_of_lists = [
['a', 1, 2, 3], ['b', 4, 5, 6], ['*', 7, 8, 9], ['c', 10, 11], ['*', 12, 13], ['d', 14, 15, 16]
]
symbol = '*'
output = split_list_of_lists(list_of_lists, symbol)
print(output)
`
### Output:
`python
[
'a', 1, 2, 3], ['b', 4, 5, 6, '*', 7, 8, 9], ['c', 10, 11, '*', 12, 13], ['d', 14, 15, 16
]
`
This code takes a list of lists and splits it into separate groups whenever a sublist starts with the specified symbol. Each group is then added to the result. The result is a list of these grouped sublists.
To solve this problem in Scheme recursively, let's define a recursive function that will process the list of lists and split them into groups whenever a sublist starts with the specified symbol.
### Steps:
- Base Case: If the list of lists is empty, return an empty list.
- Recursive Case:
- If the first sublist starts with the given symbol and the current group is not empty, start a new group.
- Otherwise, continue adding sublists to the current group.
### Scheme Implementation:
Here’s how you can implement this in Scheme:
`scheme
(define (split-list-of-lists list-of-lists symbol)
(define (split-helper lists current-group result)
(cond
((null? lists)
(if (null? current-group)
result (append result (list current-group))))
((equal? (caar lists) symbol)
(if (null? current-group)
(split-helper (cdr lists) (list (car lists)) result) (split-helper (cdr lists) (list (car lists)) (append result (list current-group)))))
(else
(split-helper (cdr lists) (append current-group (list (car lists))) result))))
(split-helper list-of-lists '() '()))
;; Example usage: (define list-of-lists
'((a 1 2 3)
(b 4 5 6) (* 7 8 9) (c 10 11) (* 12 13) (d 14 15 16)))
(define symbol '*)
(split-list-of-lists list-of-lists symbol)
`
### Explanation:
split-list-of-lists
: The main function that initializes the recursive process with an emptycurrent-group
andresult
.
split-helper
:- Base Case: When
lists
is empty, ifcurrent-group
is also empty, return theresult
. Otherwise, append thecurrent-group
to theresult
. - When symbol is encountered: If the current group is not empty, append it to the result and start a new group. If the current group is empty, simply start a new group with the current sublist.
- Else case: Append the current sublist to the
current-group
and continue the recursion.
- Base Case: When
### Example Output:
Running the above code with the example list-of-lists
and symbol
, you should get:
`scheme
'(((a 1 2 3) (b 4 5 6))
((* 7 8 9) (c 10 11)) ((* 12 13) (d 14 15 16)))
`
This Scheme code recursively processes the list of lists and splits them into groups based on the given symbol.