1
\$\begingroup\$

Given a string and integer n, split the string at n character, and print as follows: Ex)

laptop 3 should print lap top

laptop 2 should print la pt op (see how it splits the string at every 2nd char)

Special cases:

laptop 0 should print laptop (since we split at every 0th char, thus none)

laptop 9 should print laptop (since length of laptop < 9, so no splitting)

bob 2 should print bo b (we split at the o (2nd char), but then there aren't enough characters after, so we just leave as is`

You may assume n >= 0

\$\endgroup\$
6
  • 1
    \$\begingroup\$ What does "print it together" mean? Print with spaces in between, or just print it in any clear format? \$\endgroup\$ Jul 7, 2017 at 21:51
  • \$\begingroup\$ @ETHproductions in the format I have specified, sorry for the confusing \$\endgroup\$
    – K Split X
    Jul 7, 2017 at 21:52
  • \$\begingroup\$ May we print more spaces after the string output? \$\endgroup\$
    – dzaima
    Jul 7, 2017 at 21:53
  • \$\begingroup\$ @dzaima no. thats why I made it so that if you do not enough characters after splitting, you just print the rest of the string (no extra spaces after the rest of the string has been printed) \$\endgroup\$
    – K Split X
    Jul 7, 2017 at 21:55
  • \$\begingroup\$ Not a duplicate: This one is only part of the other challenge - and it allows builtins. \$\endgroup\$
    – Titus
    Jul 13, 2017 at 1:41

9 Answers 9

2
\$\begingroup\$

JavaScript, 54 47 bytes

s=>n=>n?eval(`s.match(/.{1,${n}}/g)`).join` `:s

f=
s=>n=>n?eval(`s.match(/.{1,${n}}/g)`).join` `:s


console.log(
    f('laptop')(0),
    f('laptop')(1),
    f('laptop')(2),
    f('laptop')(3),
    f('laptop')(4)
)

\$\endgroup\$
2
\$\begingroup\$

Japt, 7 bytes

òVªLl)¸

Test it online!

Explanation

            Implicit: U, V = inputs
UòV   )     Split U into slices of length V.
   ªLl      If V is 0, instead use 100! (roughly 9.3326e+157).
       ¸    Join with spaces.
            Implicit: output result of last expression

9.3326e+157 is far larger than JavaScript's max string size of 9.0072e+15, so this will work for any input.

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

PHP, 48 bytes

[,$s,$n]=$argv;echo$n?chunk_split($s,$n," "):$s;

Run with -r.

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

Jelly,  7  5 bytes

sW⁹?K

A full program printing the result (or a dyadic link returning a list of characters).

Try it online!

How?

sW⁹?K - Main link: list of characters, a; number, n
   ?  - if:
  ⁹   -       chain's right argument = n (0 is not truthy, positive integers are)
s     - then: split a into chunks of length n (overflow kept like the challenge requires)
 W    - else: wrap a in a list
    K - join with spaces (the wrapped list becomes depth 1 again with no spaces)
      - implicit print
\$\endgroup\$
3
  • \$\begingroup\$ When I first learned Jelly, I didn't understand how ? could really be useful, but that's twice now in 24 hours... Dennis really can do no wrong ;) \$\endgroup\$ Jul 7, 2017 at 23:55
  • \$\begingroup\$ So each conditional clause can only have one atom? \$\endgroup\$ Jul 8, 2017 at 13:33
  • 1
    \$\begingroup\$ @totallyhuman they could be: single atoms; together a multi-chain-link (individually just called "chains"); referenced-links; or compound-links. \$\endgroup\$ Jul 8, 2017 at 13:43
0
\$\begingroup\$

Haskell, 33 bytes

import Data.List
s!n=chunksOf n s
\$\endgroup\$
1
  • \$\begingroup\$ If not explicitly specified, the order of inputs can be arbitrary, so just chunksOf is a valid submission. In case the list has to be the first argument flip chunksOf is a shorter way. \$\endgroup\$
    – Laikoni
    Jul 7, 2017 at 22:26
0
\$\begingroup\$

CJam, 11 bytes

q~:X{X/S*}&

Try it online!

How it works

q~           e# Read all input and evaluate. Pushes the string and the number n
  :X         e# Store n in variable X
    {    }&  e# If top of the stack (number n) is nonzero, run this block
     X       e# Push N again
      /      e# Split into pieces that long. Last piece may be shorter
       S     e# Push space
        *    e# Join using spaces
             e# Implicitly display
\$\endgroup\$
3
  • \$\begingroup\$ wow i dont think it can get shorter then this \$\endgroup\$
    – K Split X
    Jul 7, 2017 at 21:52
  • \$\begingroup\$ @ETHproductions Aww. Thanks! \$\endgroup\$
    – Luis Mendo
    Jul 7, 2017 at 21:56
  • \$\begingroup\$ @ETHproductions Corrected \$\endgroup\$
    – Luis Mendo
    Jul 7, 2017 at 22:00
0
\$\begingroup\$

QBIC, 27 bytes

_L;|~:|\b=a][1,a,b|?_sA,c,b

Explanation

_L;|        set a as the length of A$ (input from cmd line)
~:|         IF b (input num from cmd line) is anything but 0 THEN (empty)
\b=a        ELSE (b==0) set b to the full length of A$
]           END IF
[1,a,b|     FOR c = 1; c <= LEN(A$); c = c + b
?_sA,c,b    PRINT a substring of A$, starting at c, running for b chars.
            NEXT is auto-added at EOF
\$\endgroup\$
0
\$\begingroup\$

Python 2, 76 67 56 bytes

-13 bytes thanks to ETHproductions and notjagan.

lambda s,n:n and[s[i:i+n]for i in range(0,len(s),n)]or s

Try it online!

Regex solution, 56 bytes

lambda s,n:re.findall('.{1,%i}'%n or len(s),s)
import re

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Could you remove the and n? \$\endgroup\$ Jul 7, 2017 at 22:05
  • \$\begingroup\$ @ETHproductions Yes, I can! Don't know what I was thinking. o0 \$\endgroup\$ Jul 7, 2017 at 22:09
  • \$\begingroup\$ I think you can do s,n=input();print n and[s[i:i+n]for i in range(0,len(s),n)]or s for 63 (note: I have not tested this) \$\endgroup\$ Jul 7, 2017 at 22:14
  • \$\begingroup\$ @ETHproductions I was just about to suggest the same for 56 bytes. \$\endgroup\$
    – notjagan
    Jul 7, 2017 at 22:15
  • \$\begingroup\$ Oh, you guys ninja'd me. >< Thanks! \$\endgroup\$ Jul 7, 2017 at 22:17
0
\$\begingroup\$

Mathematica, 114 bytes

d=StringRiffle;If[#2>(s=StringLength@#)||#2==0,#,k=d@StringPartition[#,#2];If[#2<=s/2,k,d[{k,StringDrop[#,#2]}]]]&
\$\endgroup\$

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