19
1
The Challenge
Build a N-Leveled Cantor Set.
The Cantor ternary set is created by repeatedly deleting the open middle thirds of a set of line segments.
The program receives one parameter N
(a integer number) and then prints (in console or similar way) a Cantor Set of N levels. The print can only contain undescore (_
) and whithe spaces characters. The parameter can be positive or negative and the sign indicates the Cantor Set construction orientation: If N > 0
the Cantor Set is constructed downwards and if N < 0
the Cantor Set is constructed upwards. If N = 0
then the program prints a single line (_
).
For example:
N = 2
_________
___ ___
_ _ _ _
N = -2
_ _ _ _
___ ___
_________
N = 3
___________________________
_________ _________
___ ___ ___ ___
_ _ _ _ _ _ _ _
N = -3
_ _ _ _ _ _ _ _
___ ___ ___ ___
_________ _________
___________________________
Winning criteria
As it is a code golf challenge, the shortest code wins.
Edited: Modify 0 input by ugoren's suggestion.
Why print nothing when N=0? This makes 0 a special case, and makes it harder to use recursion. General handling would be to print a single
_
(but print it downward when getting -0). – ugoren – 2012-01-30T09:55:06.870Right. I have modified the specs already. – Averroes – 2012-01-30T10:11:29.650