18
\$\begingroup\$

I have a list of decimal digits:

4, 4, 4, 7, 7, 9, 9, 9, 9, 2, 2, 2, 4, 4

The list of decimal digits are known as items. We can form "chunks" from these items by grouping together identical and adjacent numbers. I want to assign each chunk a unique number, starting from 1, and increasing by 1 in the order the chunks appear in the original list. So, the output for the given example would look like this:

1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5

Input format

A list of digits. (0-9) You may use your language built-ins to read this list however you want. Encoding: ASCII

Output format

A series of decimal numbers, separated by a delimiter. Your program must always use the same delimiter. The delimiter must be longer than 0 bits. Encoding: ASCII

Standard loopholes apply.

\$\endgroup\$
16
  • 9
    \$\begingroup\$ Any particular reason for the strict input and output format? \$\endgroup\$ Aug 13, 2019 at 21:26
  • 2
    \$\begingroup\$ @UnrelatedString Hmm, I shall loosen them. \$\endgroup\$ Aug 13, 2019 at 21:27
  • 9
    \$\begingroup\$ The IO is still rather strict. Can't you just say "input and output is as a list" and let the site defaults take care of it for you? \$\endgroup\$
    – Jo King
    Aug 14, 2019 at 3:35
  • 2
    \$\begingroup\$ Can we assume the list is non-empty? \$\endgroup\$
    – Jo King
    Aug 14, 2019 at 4:14
  • 2
    \$\begingroup\$ A list by definition has delimiters already. That's why it's a list. I also don't understand what you mean by You may use your language built-ins to read this list however you want.. Does that mean we have to include a string to list converter in our submission? And are we allowed to output as a list? \$\endgroup\$
    – Jo King
    Aug 15, 2019 at 0:47

43 Answers 43

1
2
1
\$\begingroup\$

Desmos, 65 bytes

L=join(1,1-0^{(l[2...]-l)^2})
f(l)=∑_{n=1}^{[1...L.length]}L[n]

Try It On Desmos!

Try It On Desmos! - Prettified

\$\endgroup\$
1
\$\begingroup\$

Factor, 45 bytes

[ 0 f rot [ tuck = rot dup 1 + ? tuck ] map ]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ly, 29 bytes

1sp&nry[pluy,[',o!]p=!l+spy]p

Try it online!

This one keeps a counter with the chunk number in the backup cell. That's incremented anytime the current codepoint doesn't match the next one on the list. The only other trick is the code to conditionally add the , delimiter if there's at least one more codepoint on the stack.

1sp                           - init the chunk number to 1, pop from stack
   &nr                        - read in list of digits as codepoints
      y[p                 y]p - loop as long as the stack isn't empty
         lu                   - load/print the chunk counter
           y,[   !]p          - if/then, true if there's 2+ digits left
              ',o             - print a comma delimiter
                    =!        - push "1" if the top 2 entries are not equal
                      l+sp    - increment chunk counter is digits differ

        
\$\endgroup\$
1
\$\begingroup\$

BQN, 7 bytesSBCS

+`⊢≠π⊸»

Run online!

\$\endgroup\$
1
\$\begingroup\$

Arturo, 40 bytes

$[a][p:10i:0map a'x[if p<>x[i:i+1p:x]i]]

Try it

$[a][                  ; a function taking an argument a
    p: 10              ; assign 10 to p
    i: 0               ; assign 0 to i
    map a 'x [         ; map over a; assign current element to x
        if p <> x [    ; if p doesn't equal x...
            i: i + 1   ; increment i
            p: x       ; assign x to p
        ]              ; end if
        i              ; map the current element to i
    ]                  ; end map
]                      ; end function
\$\endgroup\$
1
\$\begingroup\$

Zsh, 38 bytes

Hat-tip to @pxeger. Try it online!

n=1;>$1;for i {mv * $i&&((n++));<<<$n}

First we write a file, >$1. Then for every argument $i:
* attempt to rename that file to $i
* if the rename succeeds, increment the chunk number $n
* print $n


50 bytes (old approach)

Compares arguments. Try it online!   53 bytes   57 bytes

n=1;set $@ $@[#];for i;echo $[$2==$1?n:n++]&&shift
\$\endgroup\$
0
1
\$\begingroup\$

Thunno 2, 4 bytes

ƒ€ġḷ

Try it online!

Explanation

ƒ€ġḷ  # Implicit input
ƒ     # Prefixes
 €    # To each:
  ġ   #  Group consecutive
   ḷ  # Length of each
      # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Nekomata, 4 bytes

pNĉ#

Attempt This Online!

pNĉ#
pN      Nonempty prefix
  ĉ     Chunks of identical characters
   #    Length
\$\endgroup\$
0
\$\begingroup\$

Japt v2.0a0, 9 bytes

£T±A¦(A=X

Try it

\$\endgroup\$
0
\$\begingroup\$

Add++, 23 bytes

D,f,@*,BGd€bL$bLRz€¦XBF

Try it online!

How it works

D,f,@*,     - Define a function, f, that takes one argument:  [7 7 5 5 5 1]
       BG   - Group neighbouring elements together:           [[[7 7] [5 5 5] [1]]]          
       d    - Duplicate:                                      [[[7 7] [5 5 5] [1]] [[7 7] [5 5 5] [1]]]
       €bL  - Get the length of each:                         [[[7 7] [5 5 5] [1]] [2 3 1]]
       $bLR - Length, then range of length:                   [[2 3 1] [1 2 3]]
       z    - Zip together:                                   [[2 1] [3 2] [1 3]]
       €¦X  - Reduce each by repetition:                      [[1 1] [2 2 2] [3]]
       BF   - Flatten:                                        [1 1 2 2 2 3]
            - Due to the * in the function definition,
                 return the whole stack:                      [1 1 2 2 2 3]
\$\endgroup\$
0
\$\begingroup\$

Japt, 8 7 bytes

ä¦Ug)åÄ

Try it

\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 34 bytes

\b\d+\b(?<=(\b(\3|(\d+))\D*)*)
$#3

Try it online! Explanation:

\b\d+\b

Match each number in turn.

(?<=(...)*)

Start looking backwards for as many matches as possible. (The next entries will be in right-to-left order as that's how lookbehind works.)

\D*

Skip the separators.

(\3|(\d+))

Try to match the same number as last time, but failing that, just match any number, but remember that we had to match a new number.

\b

Ensure the whole number is matched.

$#3

Count the number of new numbers.

\$\endgroup\$
0
\$\begingroup\$

Alice, 28 bytes

0wi.hn$@.?-..3&$!h0;./ O \K

Try it online!

Takes the input as a string with no separators, outputs a string separated by new line

Explanations

0                             Initialise the counter at 0 on the stack
 wi.hn$@                  K   Main loop, while there are characters to read on the input
        .?-    $   ;          If the number read is the same as the number on the tape, discard it
           ..3& !h0           Otherwise, store the new number on the tape and increment the counter
                    ./ O \    Print the counter and a new line
\$\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.