TSql Challenge 56 is over .... So I can safetly ask doubt on that
I have tried to solve the problem as under
WITH CTE AS
(
SELECT
id ,
level ,
1 AS row ,
REPLICATE('X', POWER(3, level)) AS carpet
FROM TC56
UNION ALL
SELECT id ,
level ,
row + 1 ,
carpet
FROM CTE
WHERE Row < POWER(3, level)
)
SELECT
id
,row
,carpet
FROM CTE
ORDER BY id,row
But the output is not at par with the one specified.. My output is as under
id row carpet
1 1 X
2 1 XXX
2 2 XXX
2 3 XXX
3 1 XXXXXXXXX
3 2 XXXXXXXXX
3 3 XXXXXXXXX
3 4 XXXXXXXXX
3 5 XXXXXXXXX
3 6 XXXXXXXXX
3 7 XXXXXXXXX
3 8 XXXXXXXXX
3 9 XXXXXXXXX
The problem is that I am not able to fill positions that needs to be filled with empty spaces
Help needed
Here's a solution that can be extended to be able to generate Sierpinski carpets of greater orders. Currently it can correctly generate Sierpinski carpets up to the 3rd order.
By 'correctly' I mean, it can produce all the necessary rows for greater order patterns, but presently additional columns need to be added (those
sortN
ones) to provide the correct arrangement of the rows. I've added comments where modifications are required.Here's the result: