Given a string and integer n, split the string at n characters, and print as follows:

1

0

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

K Split X

Posted 2017-07-07T21:49:02.090

Reputation: 989

Question was closed 2017-07-07T22:21:48.980

1What does "print it together" mean? Print with spaces in between, or just print it in any clear format? – ETHproductions – 2017-07-07T21:51:18.760

@ETHproductions in the format I have specified, sorry for the confusing – K Split X – 2017-07-07T21:52:26.513

May we print more spaces after the string output? – dzaima – 2017-07-07T21:53:38.777

@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) – K Split X – 2017-07-07T21:55:07.580

Not a duplicate: This one is only part of the other challenge - and it allows builtins. – Titus – 2017-07-13T01:41:16.090

Can I print linebreaks instead of spaces? – Titus – 2017-07-13T01:45:02.710

Answers

2

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.

ETHproductions

Posted 2017-07-07T21:49:02.090

Reputation: 47 880

2

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)
)

darrylyeo

Posted 2017-07-07T21:49:02.090

Reputation: 6 214

1

PHP, 48 bytes

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

Run with -r.

Titus

Posted 2017-07-07T21:49:02.090

Reputation: 13 814

1

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

Jonathan Allan

Posted 2017-07-07T21:49:02.090

Reputation: 67 804

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 ;) – ETHproductions – 2017-07-07T23:55:35.293

So each conditional clause can only have one atom? – totallyhuman – 2017-07-08T13:33:32.643

1

@totallyhuman they could be: single atoms; together a multi-chain-link (individually just called "chains"); referenced-links; or compound-links.

– Jonathan Allan – 2017-07-08T13:43:49.423

0

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

Luis Mendo

Posted 2017-07-07T21:49:02.090

Reputation: 87 464

wow i dont think it can get shorter then this – K Split X – 2017-07-07T21:52:02.280

@ETHproductions Aww. Thanks! – Luis Mendo – 2017-07-07T21:56:27.960

@ETHproductions Corrected – Luis Mendo – 2017-07-07T22:00:49.167

0

Haskell, 33 bytes

import Data.List
s!n=chunksOf n s

Sergii Martynenko Jr

Posted 2017-07-07T21:49:02.090

Reputation: 213

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. – Laikoni – 2017-07-07T22:26:40.060

0

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!

totallyhuman

Posted 2017-07-07T21:49:02.090

Reputation: 15 378

Could you remove the and n? – ETHproductions – 2017-07-07T22:05:14.667

@ETHproductions Yes, I can! Don't know what I was thinking. o0 – totallyhuman – 2017-07-07T22:09:20.470

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) – ETHproductions – 2017-07-07T22:14:08.137

@ETHproductions I was just about to suggest the same for 56 bytes.

– notjagan – 2017-07-07T22:15:53.340

Oh, you guys ninja'd me. >< Thanks! – totallyhuman – 2017-07-07T22:17:07.687

0

Mathematica, 114 bytes

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

J42161217

Posted 2017-07-07T21:49:02.090

Reputation: 15 931

0

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

steenbergh

Posted 2017-07-07T21:49:02.090

Reputation: 7 772