21
\$\begingroup\$

Grouping takes a list and splits it into new lists of equal adjacent elements. For example

[1,1,2,1,1] -> [[1,1],[2],[1,1]]

If you then take the length of these groups you get a new list of integers

[1,1,2,1,1] -> [2,1,2]

Your task is to write a program that takes a list of positive integers and find the number of times you can group and length it before the resulting list has a single element. For example the list [1,2,3,3,2,1] can be regrouped 4 times

[1,2,3,3,2,1]
[1,1,2,1,1]
[2,1,2]
[1,1,1]
[3]

This is so answers will be scored in bytes with fewer bytes being better.

Test cases

[1,2,3,3,2,1] -> 4
[1,2,3,4,5,6,7] -> 2
[1,1,1,1,1,1] -> 1
[2] -> 0
[1,2,4] -> 2
[1,2,2,1,1,2] -> 4
[1,2,2,1,1,2,1,2,2] -> 5
[1] -> 0
\$\endgroup\$
4
  • 3
    \$\begingroup\$ This is basically run-length encoding without storing the values. \$\endgroup\$
    – 12Me21
    Commented Feb 23, 2018 at 23:09
  • \$\begingroup\$ [1] is a valid input and should give 0, correct? \$\endgroup\$ Commented Feb 24, 2018 at 1:43
  • \$\begingroup\$ @ETHproductions Yes, I'll add that because it is a bit of a tricky case. \$\endgroup\$
    – Wheat Wizard
    Commented Feb 24, 2018 at 2:11
  • 2
    \$\begingroup\$ The whole task is precisely the definition of runs-resistance. Related OEIS sequence: A318928 - Runs-resistance of binary representation of n \$\endgroup\$
    – Bubbler
    Commented May 28, 2020 at 0:39

32 Answers 32

1
2
0
\$\begingroup\$

Red, 140 bytes

func[b][n: 0 while[(length? b)> 1][l: copy[]parse split form b" "[any[copy s[set t string! thru any t](append l length? s)]]b: l n: n + 1]n]

Try it online!

I just wanted to give Red's Parse dialect another try.

Ungolfed

f: func [b] [
    n: 0
    while [(length? b) > 1][
        l: copy []
        parse split form b " " [
            any [copy s [set t string! thru any t]
                (append l length? s)]
        ]
        b: l
        n: n + 1
    ]
    n
]
\$\endgroup\$
0
\$\begingroup\$

Arturo, 50 bytes

$->a[0while->¬one? a[a:chunk a=>[&]|map=>size+1]]

Try it!

Explanation

$->a[...]            ; a function taking an arg named a
0                    ; push 0, our count
while->¬one? a[...]  ; while a's length is not one...
a:                   ; assign to a...
chunk a=>[&]         ; split adjacent numbers by identity
|                    ; then...
map=>size            ; map each group to its size
+1                   ; increment count
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.