Generalized Cantor set segment lengths
Jelly, 15 13 12 bytes
-2 thanks to Dennis (using a Link rather than a chain allows right to be used implicitly by ¡
; No need to wrap the 1
in a list due to the fact that Jelly prints lists of one item the same as the item)
-1 thanks to Erik the Outgolfer (use Ɗ
to save the newline from using Ç
)
1×€³§JḤ$¦ẎƊ¡
A full program printing a list in Jelly format (so [1]
is printed as 1
)
Try it online!
How?
1×€³§JḤ$¦ẎƊ¡ - Main link: segmentLengths; iterations
1 - literal 1 (start with a single segment of length 1)
¡ - repeat...
- ...times: implicitly use chain's right argument, iterations
Ɗ - ...do: last 3 links as a monad (with 1 then the previous output):
³ - (1) program's 3rd argument = segmentLengths
×€ - 1 multiply €ach (e.g. [1,2,3] ×€ [1,2,1] = [[1,4,3],[2,4,2],[3,6,3]])
¦ - 2 sparse application...
$ - (2) ...to: indices: last two links as a monad:
J - (2) range of length = [1,2,3,...,numberOfLists]
Ḥ - (2) double [2,4,6,...] (note: out-of bounds are ignored by ¦)
§ - (2) ...of: sum each (i.e. total the now split empty spaces)
Ẏ - 3 tighten (e.g. [[1,2,3],4,[5,6,7]] -> [1,2,3,4,5,6,7])
- implicit print
Python 2, 120 107 104 103 100 99 89 bytes
f=lambda n,l:n and[x*y for i,x in enumerate(l)for y in[f(n-1,l),[sum(l)**~-n]][i%2]]or[1]
Try it online!
Saved
- -10 bytes, thanks to Neil
Haskell, 76 58 bytes
l%0=[1]
l%n=do(x,m)<-l%(n-1)`zip`cycle[l,[sum l]];map(*x)m
Try it online!
The function (%)
takes the list of line lengths l
as first argument and the number of iterations n
as second input.
Thanks to Angs and Ørjan Johansen for -18 bytes!