# Pyramid of broken strings

Given a string n, create a pyramid of the string split into pieces relative to the current row.

The first row contains the string unmodified.

The second row contains the string separated into halves by a pipe.

The third row separates it by thirds...

And so on. The length of each substring, where l is the length of string n is equal to

floor(l/n)

Characters left over are put in their own substring. The last row used is the first one where substrings are 2 in length.

Test Cases:

Input: Hello, world.

Output:

Hello, world.

Hello,| world|.

Hell|o, w|orld|.

Hel|lo,| wo|rld|.

He|ll|o,| w|or|ld|.

Input: abcdefghij

Output:

abcdefghij

abcde|fghij

abc|def|ghi|j

ab|cd|ef|gh|ij

Input: 01234567890abcdef

Output:

01234567890abcdef

01234567|890abcde|f

01234|56789|0abcd|ef

0123|4567|890a|bcde|f

012|345|678|90a|bcd|ef

01|23|45|67|89|0a|bc|de|f

# Extra rules:

• You can write a full program or a function, whichever uses less code.

• Input will always be at least 4 characters in length.

• You MUST use line breaks if your language supports them. If not possible, replace line breaks with :

• Input will always be printable ASCII.

• Minus 100% if your program solves P vs. NP.

• 0 bytes: return: false – Gabriel Benamy Dec 23 '16 at 14:29
• Nice first challenge! A few clarification questions -- is the input only printable ASCII (I strongly suggest "yes")? What does "line breaks are necessary when possible" mean? – AdmBorkBork Dec 23 '16 at 14:39
• It's a joke. P vs NP is an unsolved problem in computing. The joke is that if you can solve it, I will stop caring about the fact the your program doesn't solve the challenge. – Julian Lachniet Dec 23 '16 at 19:24
• The real unsolved problem in computing is "tabs or spaces"... – FlipTack Dec 23 '16 at 22:49
• No, the real problem is Internet Explorer. – Julian Lachniet Dec 23 '16 at 22:50

## JavaScript (ES6), 10310191 84 bytes

Fixed to respect challenge requirements

f=(s,n=0,p=s.length/++n|0)=>p>1?s.match(eval('/.{1,'+p+'}/g')).join|+'\n'+f(s,n):''

Lambda f that takes input string as first parameter s and recursively prints to console the split string. Pretty straightforward: as long as the substring length, p, is above 1, print the string split by a '|' every p characters, then proceed with appending the following level. This then calls the function again with p being t / n floored, where t is the original string length and n being an incremented divider.

• I don't think dividing n by 2 each time is correct. – Neil Dec 24 '16 at 0:12
• @Neil you are correct, mistake on my part. I fixed the problem and saved 2 bytes in the process. – XavCo7 Dec 24 '16 at 13:37
• @ETHproductions I thought of that, but I don't know if that would count as STDOUT... I guess I would need to do alert(f(s)) just after right? – XavCo7 Dec 24 '16 at 18:33

# Perl, 46 + 1 = 47 bytes

Run with the -n flag

say s/.{$=}(?=.)/$&|/gr while($==y///c/++$,)-2

Try it online!

### Code breakdown

-n                                              #Reads input into the $_ variable say s/.{$=}(?=.)/$&|/gr while($==y///c/++$,)-2 y///c #Transliteration. Implicitly operates on$_, replacing every character with itself and counting replacements
#y///c effectively returns the length of $_ /++$,     #Increments $, (which starts off at 0) and divides the length of$_ by $,$==               #Stores the result of this division into $= #$= forces its contents to be an integer, so it truncates any decimal
(             )-2  #Returns 0 if $= is equal to 2 while #Evaluates its RHS as the condition. If truthy, evaluates its LHS. s/ / /gr #Substitution. Implicitly operates on$_.
#Searches for its first argument and replaces it with its second argument, repeating until it's done, and returns the new string.  $_ is not modified. .{$=}                                     #Looks for a string of $= characters... (?=.) #...that is followed by at least one non-newline character, but does not include this character in the match...$&|                            #...and replaces it with itself followed by a pipe character.
say                                             #Output the result of the substitution.
• This doesn't seem to work for longer inputs. – Neil Dec 24 '16 at 0:16

## Pyth, 16 bytes

Vh/lQ3j\|cQ/lQhN

V                # For N in range(1, \/ )
h/lQ3           # 1+lenght(input)/3
j\|        # join with '|'
cQ      # chop input in
/lQhN # lenght(input)/(N+1) pieces

try here

• This might work for the test cases but I don't think it works for longer inputs. – Neil Dec 24 '16 at 0:15

## C, 145131128 125 bytes

l,n,i=1,j;f(char*s){l=strlen(s);puts(s);do{n=l/++i;for(j=0;j<l;)j&&(j%n||putchar('|')),putchar(s[j++]);puts("");}while(n>2);}

This is a function that takes a string as its argument and prints the output to STDOUT.

l,n,i=1,j;       // declare some variables
f(char*s){       // declare the function
l=strlen(s);     // get the length of the string
puts(s);         // output the initial version, with trailing newline
do{n=l/++i;      // n is the number of characters per "section",
//  and we'll do-while n>2 to stop at the right time
for(j=0;j<l;)    // loop through the characters of the string
j&&(             // if j != 0,
j%n||            // and j % n == 0,
putchar('|')),   // insert a | before this character
putchar(s[j++]); // print the character
puts("");        // print a newline after the loop
}while(n>2);}
• How does this work once i*i>l? It looks as if it will start to repeat sections. – Neil Dec 24 '16 at 0:13
• @Neil I'm not sure what you mean. Could you give an example? – Doorknob Dec 24 '16 at 0:16
• @Neil Ah, never mind, I see what you're saying. That appears to be a hole in the specification, which explicitly states that the length of each substring is floor(l/n); I'm not sure what the intended behavior is for longer inputs or if the OP anticipated that. – Doorknob Dec 24 '16 at 0:19

# Pyth, 17 bytes

jmj\|cQ/lQdSh/lQ3

### Explanation

cQ/lQ         Divide into equal pieces (with the last shorter)
j\|              Join with pipes
m        d        Map to each row index...
Sh/lQ3  ... up to the first row with substrings of length 2
j                  Join with newlines

# Javascript, 98 Bytes

a=>{for(b=1;2<=a.length/b;)eval("console.log(a.match(/.{1,"+(a.length/b|0)+"}/g).join('|'))"),b++}

Function x(a). Call using

console.log(x("ABCDEF"))

# Ruby 60 + 1 = 61 bytes

+1 byte for -n flag.

z= ~/$/ (z/3+1).times{|n|puts$_.scan(/.{1,#{z/(n+1)}}/)*?|}

See it on Ideone: http://ideone.com/RtoReG

# Python 3, 123 bytes

f=lambda s:print(*['|'.join(s[i:i+n]for i in range(0,len(s),n))for n in[len(s)//i for i in range(1,len(s)//2+1)]],sep='\n')

At longer strings some parts will be repeated, as the formula for the length of the substring is floor(l/n). For example with a string 13 chars long, the string split into 5's would be the same as the string split into 6's as floor(13/5)==floor(13/6). I'm not sure if the OP expected this or if it was an oversight.