| Task: | Fraktaali |
| Sender: | Yytsi |
| Submission time: | 2017-10-12 14:50:41 +0300 |
| Language: | Python3 |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 10 |
| #2 | ACCEPTED | 10 |
| #3 | ACCEPTED | 10 |
| #4 | ACCEPTED | 10 |
| #5 | ACCEPTED | 10 |
| #6 | ACCEPTED | 10 |
| #7 | ACCEPTED | 10 |
| #8 | ACCEPTED | 10 |
| #9 | ACCEPTED | 10 |
| #10 | ACCEPTED | 10 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.10 s | 1 | details |
| #2 | ACCEPTED | 0.08 s | 2 | details |
| #3 | ACCEPTED | 0.07 s | 3 | details |
| #4 | ACCEPTED | 0.08 s | 4 | details |
| #5 | ACCEPTED | 0.08 s | 5 | details |
| #6 | ACCEPTED | 0.09 s | 6 | details |
| #7 | ACCEPTED | 0.08 s | 7 | details |
| #8 | ACCEPTED | 0.11 s | 8 | details |
| #9 | ACCEPTED | 0.23 s | 9 | details |
| #10 | ACCEPTED | 0.72 s | 10 | details |
Code
"""
Written by, Tuukka Yildirim.
input: n -> [1, 10]
Things I know right off the getgo.
Area of the fractal = 4 ** (n - 1)
Side length of the fractal = sqrt(4 ** (n - 1)), where ** denotes exponentiation.
The solution used here is recursive. The fractal matrix that is manipulated
consists of boolean values. I found it easier to hold the matrix in a 1D list.
The idea for F(n) is to return a 1D list of boolean values that represent
# and . accordingly [true -> #, false -> .] Also, the list should be
in a form, such that the fractal lines can be easily decoded by reading
lines of the length sqrt(4 ** (n - 1)). This turned out to be a problem,
that I solved with some math that can be found at F.
At the end, we just slice the list to form the fractal.
"""
def reverseBlock(arr):
return [not val for val in arr]
from collections import Iterable
def flatten(items):
"""Yield items from any nested iterable."""
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
yield from flatten(x)
else:
yield x
def F(n):
if n == 1: return [True]
elif n == 2: return [True, True, True, False]
else:
block = F(n - 1)
grid = list(flatten([block, block, block, reverseBlock(block)]))
#print(grid)
blocks = []
area = 4 ** (n - 1)
subArea = area // 4
side = int(area ** 0.5)
for y in range(side):
# Every row has 2 sides.
# LLLL | PPPP
# LLLL | PPPP
# EEEE | IIII
# EEEE | IIII
halfWay = (side // 2)
if y < halfWay:
# Upper region
for x in range(side):
realX = x % halfWay
if x < halfWay:
# Upper left
blocks.append(grid[y * halfWay + x])
else:
# Upper right
blocks.append(grid[subArea + y * halfWay + realX])
else:
# Lower region
realY = y % halfWay
for x in range(side):
realX = x % halfWay
if x < halfWay:
# Lower left
blocks.append(grid[2 * subArea + realY * halfWay + x])
else:
# Lower right
blocks.append(grid[3 * subArea + realY * halfWay + realX])
return blocks
n = int(input())
grid = F(n)
area = 4 ** (n - 1)
subArea = area // 4
side = int(area ** 0.5)
lines = ""
for i in range(side):
cutStart = i * side
cutEnd = cutStart + side
slice = grid[cutStart : cutEnd]
print("".join([".#"[val] for val in slice]))Test details
Test 1
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 1 |
| correct output |
|---|
| # |
| user output |
|---|
| # |
Test 2
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 2 |
| correct output |
|---|
| ## #. |
| user output |
|---|
| ## #. |
Test 3
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 3 |
| correct output |
|---|
| #### #.#. ##.. #..# |
| user output |
|---|
| #### #.#. ##.. #..# |
Test 4
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 4 |
| correct output |
|---|
| ######## #.#.#.#. ##..##.. #..##..# ####.... ... |
| user output |
|---|
| ######## #.#.#.#. ##..##.. #..##..# ####.... ... |
Test 5
Group: 5
Verdict: ACCEPTED
| input |
|---|
| 5 |
| correct output |
|---|
| ################ #.#.#.#.#.#.#.#. ##..##..##..##.. #..##..##..##..# ####....####.... ... |
| user output |
|---|
| ################ #.#.#.#.#.#.#.#. ##..##..##..##.. #..##..##..##..# ####....####.... ... |
Test 6
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 6 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 7
Group: 7
Verdict: ACCEPTED
| input |
|---|
| 7 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 8
Group: 8
Verdict: ACCEPTED
| input |
|---|
| 8 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 9
Group: 9
Verdict: ACCEPTED
| input |
|---|
| 9 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 10
Group: 10
Verdict: ACCEPTED
| input |
|---|
| 10 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
