| Bytes | Lang | Time | Link |
|---|---|---|---|
| 047 | Haskell | 241215T213145Z | MaroonSp |
| 041 | AWK | 241029T204105Z | xrs |
| 090 | Raku Perl 6 rakudo | 240820T090906Z | Themooni |
| 009 | Pyth | 240820T081827Z | int 21h |
| 009 | Uiua | 240703T112631Z | noodle p |
| 046 | tinylisp 2 | 240816T190028Z | DLosc |
| 044 | JavaScript Node.js | 240714T232756Z | Andrew B |
| 055 | Haskell | 240705T151856Z | DPD- |
| 022 | Haskell + hgl | 240707T133716Z | Wheat Wi |
| 010 | Pip p | 240707T025922Z | Aiden Ch |
| 036 | Julia 1.0 | 240706T125129Z | MarcMush |
| 037 | Ruby | 240703T091600Z | int 21h |
| 012 | APL Dyalog Extended | 240702T224208Z | Adá |
| 1785 | Nibbles | 240704T155522Z | Dominic |
| 050 | C gcc | 240703T211432Z | corvus_1 |
| 019 | GolfScript | 240703T115536Z | Twilight |
| 041 | Python 3.8 prerelease | 240702T225141Z | squarero |
| 026 | brainfuck | 240702T233900Z | noodle p |
| 012 | J | 240703T190336Z | noodle p |
| 012 | Brachylog | 240704T093923Z | Fatalize |
| 3534 | Excel | 240703T040741Z | z.. |
| 054 | Julia 1.0 | 240703T150526Z | Ashlin H |
| 028 | Perl 5 n | 240703T140300Z | Xcali |
| 011 | MathGolf | 240703T131038Z | Kevin Cr |
| 005 | Husk | 240703T123630Z | Dominic |
| 006 | 05AB1E | 240703T084621Z | Kevin Cr |
| 043 | PowerShell | 240703T115900Z | user3141 |
| 015 | K ngn/k | 240703T112728Z | ovs |
| 041 | Google Sheets | 240702T230407Z | doubleun |
| 005 | Japt m | 240703T085355Z | Shaggy |
| 032 | Arturo | 240703T074338Z | chunes |
| 046 | R | 240703T073506Z | pajonk |
| 020 | APL+WIN | 240703T062803Z | Graham |
| 013 | Charcoal | 240703T054954Z | Neil |
| 058 | C gcc | 240703T045119Z | Digital |
| 011 | Uiua | 240703T005301Z | noodle p |
| 005 | Nekomata | 240703T020237Z | alephalp |
| 038 | JavaScript V8 | 240703T011421Z | l4m2 |
| 016 | K ngn/k | 240703T000159Z | Bubbler |
| 016 | J | 240703T001653Z | Bubbler |
| 021 | Retina 0.8.2 | 240702T235815Z | Neil |
| 005 | Jelly | 240702T230349Z | Jonathan |
| 003 | Vyxal | 240702T224503Z | lyxal |
Haskell, 47 bytes
g n""=[]
g n s|(h,t)<-splitAt n s=h:g(n*2)t
g 1
It returns the answer as a list of strings. This is basically the simplest possible answer. More readably, it might be written like this:
go n "" = []
go n string = chunk : go (n*2) string'
where (chunk, string') = splitAt n string
AWK, 64 52 41 bytes
{for(i=1;i<NF;i=2*i)print substr($0,i,i)}
Use [ -F '' ] flag upon calling awk to separate fields and give us the right number for NF.
{for(i=1;i<length($0);i=2*i)printf substr($0,i,i)FS}
{for(i=1;i<length($0);i=2*i)printf substr($0,i,i)"\n"(y=y y" ")}
{for(i=1;i<length($0);i=2*i) # twice as many letters per
printf substr($0,i,i) # just the letters we need
"\n"(y=y y" ")} # whitespace for next line
Raku (Perl 6) (rakudo), 90 bytes
sub MAIN($i){my $s=$i;for (1,2,4...^*>$s.chars) ->$x {say $s.substr(0,$x);$s.=substr($x)}}
ungolfed:
sub MAIN($i){
my $s=$i;
for (1,2,4...^*>$s.chars) ->$x {
say $s.substr(0,$x);
$s.=substr($x)
}
}
straightfowards and uninventive and probably not very golfed but this is my first time doing raku or perl and i wanted to try it out.
Pyth, 9 bytes
fTcQtM^2U
Takes a string as an input and outputs a list of strings.
U # create a range 0..length of the string
^2 # make them powers of 2
tM # decrease by 1
cQ # chop the input string Q
fT # remove empty strings
Uiua, 9 bytes
This solution was written by Kai Schmidt, the creator of Uiua, not by me, so I'm making this a community wiki.
⊕□⌊ₙ2+1°⊏
This is a very simple solution, and very short: Take the range of 0 to the length of the input, add one to each, take the base-2 logarithm, and floor it. This list has natural numbers repeated doubling in length, so we use this as a group to take each part of the input into a list of boxes.
tinylisp 2, 46 bytes
(d F(\(S(I 1))(? S(c(] I S)(F([ I S)(* I 2)))(
Ungolfed & commented
(def segments
(lambda ; Function
(S (I 1)) ; Takes two args: a string, and a number defaulting to 1
(if S ; If the string is nonempty:
(cons ; Prepend
(take I S) ; the first I characters of S
(segments ; to the result of a recursive call on
(drop I S) ; All but the first I characters of S
(* I 2))) ; I times 2
nil))) ; Else, empty list
Haskell, 61 58 56 55 bytes
- -2 bytes thanks to Wheat Wizard ♦
takeWhile(>"").evalState(mapM(state.splitAt.(2^))[0..])
It maps (with state) over the infinite sequence of powers of two. Each map step returns the first n characters of the string in the state and puts the remaining in the state. So giving as initial state the string and stop iterating when the result is the empty string, we obtain the result.
Haskell + hgl, 22 bytes
This answer was written jointly with 0 '
cr<<<gB(l<bi<st)<zp nN
Explanation
zp nNzip with the natural numbers to pair each element with its 1-index.gB(l<ni<st)group the list by the length of the index as a binary number.crremove the indices
23 bytes
he<pST(fo<pa(eq<db.*l))
Explanation
This implements an exponential time algorithm. Unlike the previous solution this errors when the input is not one less than a power of two in length.
heget the first ..pSTpartition of the input such that ...fofor all ...papairs of consecutive elements ...eq<db.*lthe length of the second is double the length of the first.
Reflection
There are quite a few things I see here to improve.
- There should be a version of
euwhich starts indexing from1instead of0.zp nNis the way we are doing that now. - There should be some log functions for calculating the digit lengths in various bases. Currently this is
l<bi. - There should be a function to determine if every pair of consecutive elements in a list satisfies a predicate. This is currently
fo<<pa. - There are currently functions for breaking lists into fixed-size chunks. There should be a function which takes a list and spits it into chunks of the sizes given in the list. This would vastly trivialize this challenge.
As of 79db5b20 these reflections are implemented and this answer can be
10 bytes
xuc$pw2<nn
18 bytes
cr<<<gB(lB2<st)<eU
19 bytes
he<pST(apa$eq<db.*l)
Pip -p, 10 bytes
a^@--E\,#a
I feel there could be 1 or 2 bytes improvement but I don't see it at the moment.
Julia 1.0, 36 bytes
!s=keys(s).|>k->show(s[2^~-k:~-2^k])
Ends with an error. Substrings are printed in quotes. Cleaner output with println instead of show (+3 bytes)
Ruby, 55 53 47 45 37 bytes
f=->s,a=1{s&&[s[0,a],*f[s[a..],a+a]]}
@OP just to let you know, the easy tasks are welcome! :)
A recursive function with 2 variables: the original string and the counter. The function chops a string piece, adds it to the unnamed output vector and updates the counter until the string is empty.
APL (Dyalog Extended), 16 12 bytes
Anonymous tacit prefix function, port of Bubbler's J.
⊢⊂⍨1=1⊥2⊤⍳∘≢
⊢ the argument…
⊂⍨ partitioned by…
1= where one equals…
1⊥ the vertical sum (lit. base-1 evaluation) of…
2⊤ the binary representation (one number per column) of
⍳∘ the indices from 1 to the…
≢ length of the argument
Alternative APL (Dyalog Unicode), 12 bytes
Anonymous tacit prefix function, port of noodle person's J.
⊢⊆⍨1+∘⌊2⍟⍳∘≢
⊢ the argument…
⊂⍨ grouped by…
1+∘ incremented…
⌊ floored…
2⍟ log₂ of…
⍳∘ the indices of…
≢ the argument length
Try it online!
Old APL (Dyalog Unicode), 16 bytes
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0) which is default on many systems.
⊢⊂⍨≢↑∘∊1↑¨⍨2*⍳∘≢
⊢ the argument…
⊂⍨ partitioned by…
≢ the length of the argument…
↑∘∊ -sized prefix of the the flattened…
1↑¨⍨ prefixes of 1 (padding with trailing 0s) of lengths…
2* two to the power of…
⍳∘≢ the indices (0…n−1) of the length of the argument
Nibbles, 17 nibbles (8.5 bytes)
| . `,,$ <;^2$ >-$~ _
. # map over
`, # range from zero to
,$ # length of input:
< # take the first
^2$ # 2^n characters
; # (and save that number)
# from
> # drop the first
-$~ # saved number minus 1
# characters
# from
_ # the input
| # and finally filter-out
# any empty strings
Nibbles, 17 nibbles (8.5 bytes)
\ ! ;`. $ </,$~$ >>@ ~ -
A completely different approach for same bytes: successively halve the string and take pairwise differences.
\ ! ;`. $ </,$~$ >>@ ~ -
`' # iterate while unique
$ # starting with the input:
< # select the first
/,$~ # half its length
$ # characters
; # and save this list;
! # now, zip this list with
>>@ # itself without the first element:
~ - # get differences;
\ # finally, reverse it
This would be 1 nibble shorter if we're allowed to output the divided segments in reverse order.
C (gcc), 54 50 bytes
-4 bytes thanks to Mukundan314
i;f(s,l){for(i=1;i<l;i*=2)puts(strndup(s+i-1,i));}
May have a memory leak or two.
Python 3.8 (pre-release), 66 65 50 49 46 42 41 bytes
-1 byte by doing away with int.bit_length().
-15 bytes: recursion for the win!
-1 byte by using list unpacking instead of list().
-3 bytes by moving some stuff around.
-4 bytes by using and instead of the ternary ... if ... else ....
-1 byte by Jakque.
f=lambda x,n=1:x and[x[:n],*f(x[n:],2*n)]
brainfuck, 26 bytes
+[[->,.>++<<]+++++++++.>>]
Output separated by tabs with some extra tabs at the end.
This is pretty short so I may as well add an explanation:
+ 1 is the first segment length
[
[- Repeat that many times:
>,. Go right: read and output one character
>++ Go right: Add two to this cell
(This is what doubles the length)
<<] Go back to the loop counter cell
+++++ The cell is now zero so we can freely
++++. change it; output a tab (ASCII 9)
>>] Go to our new loop counter cell and start
again
J, 16 12 bytes
</.~2<.@^.#\
This is my first time trying to golf in J! I got a lot of help golfing the forks and whatnot. I tried a different method from Bubbler's way and it ended 4 bytes shorter :)
Thanks to ovs for telling me about #\ which is a much shorter way of getting the length-one-range.
Explanation:
</.~2<.@^.#\
</.~ NB. Group the items of the input by
2<.@^. NB. floors of the base-2 logarithms of
#\ NB. the range of 1 to the input's length.
NB. (Literally: length of prefixes)
Brachylog, 12 bytes
{Ẹg|ḍgᵗ↰ʰc}b
Explanation
We rely on ḍ - dichotomize to split the string in 2. When the length is odd, the first string is shorter which is what we want. We then recurse dichotomization on the first string. The tricky part is then to flatten the list recursively up in the same predicate. We use the same idea as @Dlosc answer here, integrated directly in the predicate thas splits the string instead of applied after (which is longer).
{ } Apply the following predicate on the input string:
Ẹ If the input is the empty string ""…
g …output [""]
| Else
ḍ Dichotomize the string [<first half>, <second half>]
gᵗ Wrap the second half in a list
↰ʰ Recursive call on the first half
c Concatenate to flatten the current level
b Remove the first element which is the empty string at the end
Julia 1.0, 54 bytes
~s=[s[2^i:2*2^i-1] for i=0:floor(Int,log2(length(s)))]
Uses array comprehension to generate pairs of indices. The related solution with map takes the same number of bytes:
Julia 1.0, 54 bytes
~s=map(i->s[2^i:2*2^i-1],0:floor(Int,log2(length(s))))
MathGolf, 12 11 bytes
hâΣÆïó‼<≥;]
Explanation:
Step 1: Calculate the amount of parts we need to output, basically \$\log_2(length+1)\$ manually:
# (e.g. input = "abcdefghijklmno")
h # Push the length of the (implicit) input-string
# → 15
â # Convert it to a binary list
# → [1,1,1,1]
Σ # Sum this list of 1-bits
# → 4
Step 2: Actually split the input into the power of 2 parts:
Æ # Loop in the range [0,value],
# using 5 characters as inner code-block:
ï # Push the current 0-based loop-index
ó # Pop and convert it to 2 to the power this index
‼ # Apply the next two operators separately on the current stack:
< # Slice to substring [0,val)
≥ # Slice to substring [val,length)
; # After the loop: discard the trailing no-op part
] # Wrap all correct parts on the stack into a list
# (after which the entire stack is output implicitly as result)
Husk, 5 bytes
C:1İ2
C # cut input into chunks with lengths:
:1 # prepend 1 to
İ2 # infinite sequence of powers of 2
# (starting at 2)
05AB1E, 6 bytes
gÝo£õÜ
Or minor alternative:
ā<o£ʒĀ
Explanation:
g # Push the length of the (implicit) input-list
Ý # Pop and push a list in the range [0,length]
# OR:
ā # Push a list in the range [1, (implicit) input-length]
< # Decrease each by 1 to the range [0,length)
o # Map each value to 2 to the power this value
£ # Split the (implicit) input-string into parts of those sizes
õÜ # Trim all trailing ""
# OR:
ʒ # Filter these string-parts:
Ā # Check if truthy (where "" is falsey)
# (after which the result is output implicitly)
PowerShell, 43 bytes
%{for($i=1;$_[$i];$i*=2){$_|% S*g($i-1)$i}}
Straightforward: extract substrings of doubling length at doubling start indexes.
Relevant golfing:
- The loop exit condition
$_[$i]tests just whether there is still a character at the current index; way shorter than$i-lt$_.Length $_|% S*g($i-1)$itakes the input string in$_, pipes it to%(an alias for ForEach-Object), calls the string object's member 'Substring' (the only member matchingS*g), passing the start index and the length of the substring to retrieve. Shorter than$_.Substring($i-1,$i)
K (ngn/k), 15 bytes
{x@.=+|\2\!-#x}
x:"abcdefg"
!-#x / integers from -length to -1
-7 -6 -5 -4 -3 -2 -1
2\!-#x / convert to base 2
-1 -1 -1 -1 -1 -1 -1
0 0 0 1 1 1 1
0 1 1 0 0 1 1
1 0 1 0 1 0 1
|\2\!-#x / maximum scan
-1 -1 -1 -1 -1 -1 -1
0 0 0 1 1 1 1
0 1 1 1 1 1 1
1 1 1 1 1 1 1
.=+|\2\!-#x / group identical columns
(,0; 1 2; 3 4 5 6)
x@.=+|\2\!-#x / index back into the input string
(,"a"; "bc"; "defg")
Google Sheets, 41 bytes
=index(let(i,2^(row(A:A)-1),mid(A1,i,i)))

Arturo, 34 32 bytes
$=>[i:1chunk&=>[2i'i+1ceil log]]
Explanation
$=>[] ; a function where input is assigned to &
i:1 ; assign 1 to i
chunk&=>[] ; split input by...
2i ceil log ; bit length of i
'i+1 ; increment i
R, 46 bytes
\(x)substring(x,a<-2^(0:log2(nchar(x))),2*a-1)
Takes input as a string and outputs a vector of strings.
R, 44 bytes
\(x)split(x,rep(i<-2^(0:log2(length(x))),i))
Takes input as a vector of characters and outputs a list of vectors of characters.
APL+WIN, 20 bytes
Prompts for string. Outputs a nested vector of segments. Index origin = 0
(∊n⍴¨n←2*⍳⌈2⍟⍴s)⊂s←⎕
Charcoal, 13 bytes
E↨Lθ²×ψX²κ↑¤θ
Try it online! Link is to verbose version of code. Explanation:
E↨Lθ²×ψX²κ
Reserve a number of lines corresponding to the 1s in the base-2 representation of the input length, with each line's length being each power of 2 in turn, according to its index.
↑¤θ
Fill that reserved area using the original input string, thereby partitioning it as required.
Uiua, 11 bytes
⊕□⊚⍜ₙ⇡2+1⧻.
Nice and short! ⊕ (group) and ⊚ (where) are a great combo for this.
⊕□⊚⍜ₙ⇡2+1⧻. # input string on the right. "abcdefghijklmno"
+1⧻. # length + 1 16
⍜ₙ⇡2 # 2^( range( log_2( that ))) [1 2 4 8]
⊕□⊚ # group into pieces that size {"a" "bc" "defg" "hijklmno"}
⍜ₙ⇡2 means range (⇡) "under" (⍜) logarithm ₙ base 2. ⍜ applies a function, then a second function, and then the inverse of that first function. Here, we apply log_2(x), then range, and then the inverse of log_2(x) which is 2^x.
Literally, ⊕□⊚ does:
# [1 2 4 8]
⊚ # these repeat the naturals: [0 1 1 2 2 2 2 3 3 3 3 3 3 3 3]
⊕□ # group by boxing the chars a b c d e f g h i j k l m n o
# corresponding with indices {"a" "bc" "defg" "hijklmno"}
Nekomata, 5 bytes
JxËᶻL
JxËᶻL
J Split the input into a list of substrings
x Push [0, ..., length of the list - 1]
Ë Power of 2
ᶻL Check that each substring has the corresponding length
K (ngn/k), 16 bytes
{(&*/'2\'!#x)_x}
A near-backport from my own J solution. Instead of checking each 1-based index has exactly one 1 bit, this checks if each 0-based index is all ones in binary. This works in K (2\0 is an empty array and its product is 1) but not in J (#:0 is 0 instead of an empty array).
K (ngn/k), 17 bytes
{(|1_(-2!)\#x)_x}
Similar to Jelly (x_y cuts y at the indices specified in x), but had to be careful to avoid generating large indices or indices in non-increasing order (which give domain error).
My initial approach used 2\ (integer into binary vector) and 2/ (binary vector into integer), but I realized that simply repeatedly dividing the length by 2 gives the correct cut indices.
{(|1_(-2!)\#x)_x} Input: a string of length 2^n-1 (example: "abcdefg")
( #x) length of x = 7
(-2!)\ repeatedly floor-divide by 2 until convergence and collect:
(7 3 1 0)
1_ remove head which is too large (3 1 0)
| reverse (0 1 3)
_x cut x with those indices ("a"; "bc"; "defg")
J, 16 bytes
<;.1~1=1#.[:#:#\
<;.1~1=1#.[:#:#\ Input: string of length 2^n-1 (example: 'abcdefg')
#\ 1-based indices (1 2 3 4 5 6 7)
[:#: each number to binary (0 0 1; 0 1 0; 0 1 1; 1 0 0; ...; 1 1 1)
1#. sum the bits of each number (1 1 2 1 2 2 3)
1= is it 1? (1 1 0 1 0 0 0)
<;.1~ cut at 1s and box each word ('a'; 'bc'; 'defg')
Retina 0.8.2, 21 bytes
!`(?<=(.)*).(?<-1>.)*
Try it online! Explanation: The last stage is by default a Match stage if there is no replacement pattern for it to be a Replace stage. The ! changes the output from the number of matches (in decimal) to the matches themselves (on separate lines). The pattern then uses a .NET balancing group to count its match index (which will be one less than a power of 2) and allow one character more than that to be matched.
Jelly, 5 bytes
J2*œṖ
A monadic Link that accepts a list and yields a list of lists.
How?
J2*œṖ - Link: List of characters, S
J - indices -> [1..len(S)]
2* - two exponentiate {that}
œṖ - partition {S} at {those} indices
Vyxal, 3 bytes
ẏEẇ
Golf pilled sbcs maxxer.
Explained
ẏEẇ
ẏ # Range [0, len input)
E # Each to the power of 2
ẇ # Partition the string into groups corresponding to each length
💎
Created with the help of Luminespire.
