Yesterday I couldn't solve a problem which seemed to be rather simple. I tried RepleaceRepeated
, SequenceReplace
and similar functions.
1. Example data
list = {5, 1, 4, 3, 1, 4, 9, 4, 3, 1, 2, 0, 0, 1, 4, 4, 0, 0, 0};
2. Expected result
{{5}, {1, 4, 3, 1}, {4, 9, 4}, {3}, {1, 2, 0, 0, 1}, {4}, {4}, {0, 0, 0}};
3. Rules
- Go to the first element, 5, and look ahead until the end of the list to see if it is repeated. If not, bracket it. We call this single case.
- Go to the next element, 1, and look ahead until you find the first closing 1. Bracket the opening and closing 1 and all elements between. We call this repetition case.
- Repetition case
- Single case
- Repetition case with inclusion of another repetition case (3, 4, 3). Included repetition cases are not bracketed.
- Two equal numbers form two single cases.
- Three or more equal numbers form one repetition case.
4. Another example:
{5, 1, 4, 3, 1, 4, 9, 4, 3, 1, 2, 0, 0, 1, 4, 4, 0, 5, 0, 0}
would result in
{{5, 1, 4, 3, 1, 4, 9, 4, 3, 1, 2, 0, 0, 1, 4, 4, 0, 5}, {0}, {0}};
{1, 2, 3, 4, 3, 1}
? Also since there are two4s
at the end of the list, it can't be called a single case. Should this result{4,4}
instead - just as the three zeros result in{0,0,0}
at the end? $\endgroup${1, 2, 3, 4, 3, 1}
would become{{1, 2, 3, 4, 3, 1}}
- Two equal numbers following each other should be treated as two singles like shown underExpected result
. $\endgroup$