22
\$\begingroup\$

Given a string, you've to split it into groups of three characters (by padding _ at the end).

Related, but still it's different.


Sample I/O:

  • 'abcde' -> 'abc', 'de_'

  • '1234' -> '123', '4__'

  • '' -> [] or falsey value

  • 'Three spree!' -> 'Thr', 'ee ', 'spr', 'ee!'

  • 6 MB string

This is a , so fewest bytes will win!

Edit: Finally, output is unrestricted.

\$\endgroup\$
3
  • 11
    \$\begingroup\$ Why special case the empty string? May we choose which character to use for the padding? \$\endgroup\$
    – Shaggy
    Commented May 19, 2019 at 14:49
  • \$\begingroup\$ Is null considered a falsey value? \$\endgroup\$ Commented May 19, 2019 at 14:58
  • 2
    \$\begingroup\$ @LuisfelipeDejesusMunoz, falsey values are defined by the language, not the challenge author. \$\endgroup\$
    – Shaggy
    Commented May 19, 2019 at 15:00

48 Answers 48

1
2
1
\$\begingroup\$

Haskell, 40 bytes

f(a:b:c:t)=[a,b,c]:f t
f _=[]
f.(++"__")
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Babel Node), 45 bytes

_=>_.match(/.{1,3}/g).map(a=>a.padEnd(3,`_`))

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 44 \$\endgroup\$
    – Shaggy
    Commented May 19, 2019 at 15:13
  • \$\begingroup\$ Doesn't work for me \$\endgroup\$
    – Luvexina
    Commented May 22, 2019 at 21:55
0
\$\begingroup\$

Jelly, 10 bytes

s3;€⁾__ḣ€3

Try it online!

It feels like an 8 or 9 should be possible, but haven’t found one yet.

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

Attache, 34 23 bytes

PadRight&"_"&3=>@Chop&3

Try it online!

Explanation (outdated)

{On[#_-1,PadRight&"_"&3,_]}@Chop&3
                           @Chop&3    chop the input string into groups of 3s
{On[#_-1               ,_]}           on the last group
        ,PadRight                         pad it
                     &3                   to length three
                 &"_"                     with underscores
\$\endgroup\$
2
  • \$\begingroup\$ Would you save anything by padding all the elements instead of just the last one? \$\endgroup\$
    – Shaggy
    Commented May 19, 2019 at 20:18
  • \$\begingroup\$ @Shaggy Good point! I’ll look into it \$\endgroup\$ Commented May 19, 2019 at 20:19
0
\$\begingroup\$

Charcoal, 10 bytes

E⪪S³…⁺ι__³

Try it online! Link is to verbose version of code. Explanation:

  S         Input string
 ⪪ ³        Split into groups of up to 3 characters
E           Map over each group
      ι     Current group
     ⁺      Concatenated with
       __   Literal `__`
    …    ³  Moulded to length 3
            Each group implicitly printed on its own line
\$\endgroup\$
0
\$\begingroup\$

Scala, 31 bytes

(s+"___").grouped(3).toSeq.init

Try it online!

Explanation

(s+"___")       // add "___"
  .grouped(3)   // make groups of 3
  .toSeq        // change to Seq
  .init         // take all but last
\$\endgroup\$
1
  • \$\begingroup\$ You'll have to add some part of the function definition in, otherwise you won't know where s is coming from \$\endgroup\$
    – Jo King
    Commented Mar 26, 2020 at 10:17
0
\$\begingroup\$

Befunge-93, 30 29 bytes

~:1+%,~:1+!"`"*+,~:1+!"`"*+,,

Try it online!

Outputs nothing for an empty input, otherwise outputs strings of length 3 separated by NUL characters.

Explanation:

~                               Push character of input to stack
 :1+                            Duplicate input and add 1
    %                           Modulo top 2 values of stack and push result
                                If input was -1 (end of stream), calculate -1%0 -> halt
                                Otherwise calculate input%(input+1) -> input
     ,                          Pop and output character
      ~                         Push character of input to stack
       :1+!                     Duplicate input, add 1 and perform logical NOT
           "`"*                 Multiply by 96 (ASCII code for '`')
                                This returns 96 or 0 depending on the result of the NOT
               +                Add the top 2 values from the stack and push the result
                                If the input was -1 (end of stream), pushes -1+96=95, or the ASCII code for '_'
                                Otherwise pushes input+0
                ,               Pop and output character
                 ~:1+!"`"*+,    Same block again to handle the third character
                            ,   Output a NUL character (stack is empty, so popping from stack just returns 0)

The instruction pointer wraps around at the end, looping the entire line.
\$\endgroup\$
0
\$\begingroup\$

Pyth, 10 9 bytes

Pc+z*3\_3

Try it online!

-1 from u_ndefined.

Is it ok if it throws an error on empty input? (does that count as falsey)

\$\endgroup\$
2
  • \$\begingroup\$ It works on TIO, but not on Pyth compiler/executor. Is there a reason? (i.e. Pyth compiler/executor is outdated?) \$\endgroup\$
    – uanirudhx
    Commented May 19, 2019 at 19:45
  • \$\begingroup\$ -1 byte \$\endgroup\$
    – u-ndefined
    Commented May 21, 2019 at 13:27
0
\$\begingroup\$

Rust, 93 bytes

Works for ASCII input strings. Empties the input string and returns a Vec<String>.

|mut s:String|{s+="__";let mut v=vec![];while s.len()>2{let t=s.split_off(3);v.push(s);s=t}v}

Try It Online

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

GoLang, 84 bytes

func(x string)[]string{return regexp.MustCompile("...").FindAllString(x+"__",-1)}(s)

Try it !

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

Haskell, 62 61 59 bytes

z(a:b:c:d)=[a,b,c]:z d
f s=takeWhile(/="___")$z$s++cycle"_"

Infinite lists and strings!

Try it online!

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

SNOBOL4 (CSNOBOL4), 93 bytes

	S =INPUT
	S =S DUPL('_',REMDR(3 - REMDR(SIZE(S),3),3))
R	S LEN(3) . OUTPUT REM . S	:S(R)
END

Try it online!

SNOBOL has a size limit on the length of its input (1023 bytes).

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

Julia 1.0, 40 bytes

s->[(s*"__")[i:i+3] for i=1:3:length(s)]

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 37 bytes \$\endgroup\$
    – H.PWiz
    Commented Jun 3, 2019 at 19:53
0
\$\begingroup\$

GolfScript, 10 bytes

"___"+3/);

Try it online!

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

Ruby, 27 21 bytes

p"#{$_}__".scan /.../

Try it online!

-6 bytes thanks to vrintle

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Saved 6 bytes more, Try it online!. BTW, do you know why it fails for multiline input? Though, it's not a part of this challenge.. \$\endgroup\$
    – vrintle
    Commented Apr 16, 2021 at 3:37
0
\$\begingroup\$

Uiua SBCS, 8 bytes

⬚@_↯¯1_3

Try it!

\$\endgroup\$
1
  • \$\begingroup\$ ¯1 now needs to be (which saves a byte) \$\endgroup\$
    – nyxbird
    Commented yesterday
0
\$\begingroup\$

Nibbles, 8 bytes (16 nibbles)

In literal form:

`/3;_<3:$"__"

Attempt This Online!

`/             chunks of
  3              3 characters
   ;_            for input string
               then, apply map(\chunk->...):
     <3          take first 3 characters of
       :$"__"    concatenation of chunk and "__"
\$\endgroup\$
0
\$\begingroup\$

Rattle, 21 bytes

\__|!II^/3P[[gb>]3B]`

Try it Online!

Outputs groups of 3 separated by newlines.

Explanation:

\__|                  take input and concatenate "__"
    !                 disable implicit output
     I                save characters of the input in consecutive memory slots
      I^              get length of the input
        /3            divide by 3 to get n
          [...]`      loop n times (ignoring any values after the decimal point)

[...]3                loop 3 times
 g                    get the character at the pointer
  b                   add this character to the print buffer
   >                  move pointer right
      B               print contents of buffer and reset
\$\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.