17
Problem
Let's define a generalized Cantor set by iteratively deleting some rational length segments from the middle of all intervals that haven't yet been deleted, starting from a single continuous interval.
Given the relative lengths of segments to delete or not, and the number of iterations to do, the problem is to write a program or function that outputs the relative lengths of the segments that have or have not been deleted after n
iterations.
Example: Iteratively delete the 4th and 6th eighth
Input:
n
– number of iterations, indexed starting from 0 or 1
l
– list of segment lengths as positive integers with gcd(l)=1
and odd length, representing the relative lengths of the parts that either stay as they are or get deleted, starting from a segment that doesn't get deleted. Since the list length is odd, the first and last segments never get deleted.
For example for the regular Cantor set this would be [1,1,1] for one third that stays, one third that gets deleted and again one third that doesn't.
Output:
Integer list o
, gcd(o)=1
, of relative segment lengths in the n
th iteration when the segments that weren't deleted in the previous iteration are replaced by a scaled down copy of the list l
. The first iteration is just [1]
. You can use any unambiguous output method, even unary.
Examples
n=0, l=[3,1,1,1,2] → [1]
n=1, l=[3,1,1,1,2] → [3, 1, 1, 1, 2]
n=2, l=[3,1,1,1,2] → [9,3,3,3,6,8,3,1,1,1,2,8,6,2,2,2,4]
n=3, l=[5,2,3] → [125,50,75,100,75,30,45,200,75,30,45,60,45,18,27]
n=3, l=[1,1,1] → [1,1,1,3,1,1,1,9,1,1,1,3,1,1,1]
You can assume the input is valid. This is code-golf, so the shortest program measured in bytes wins.
Would it be acceptable to input and output the indices of non-deleted segments instead of the lengths? For instance,
[0, 1, 2, 4, 6, 7]
instead of[3, 1, 1, 1, 2]
? – None – 2018-05-22T15:06:37.243@Mnemonic it's not too far from unary, so I'd say it's fine. – Angs – 2018-05-22T15:25:53.747
Could you add one (or multiple) test case(s) for even-sized input-lists? – Kevin Cruijssen – 2018-05-23T07:09:42.410
1@KevinCruijssen the input lists are guaranteed to be odd-sized – Angs – 2018-05-23T07:18:52.117