-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
Sierpinski Triangle | ||
=================== | ||
|
||
**🎯 Construct a Sierpinski Triangle with a Cellular Automaton.** | ||
|
||
Consider the following rules: | ||
|
||
.. code:: python3 | ||
rules = { | ||
' ': ' ', | ||
' #': '#', | ||
' # ': '#', | ||
' ##': ' ', | ||
'# ': '#', | ||
'# #': ' ', | ||
'## ': ' ', | ||
'###': ' ', | ||
} | ||
Now when you start with a string consisting of a single hash flanked by spaces: | ||
|
||
:: | ||
|
||
# | ||
|
||
The rules define how the next line looks like. | ||
For the new line you look up character triplets from the original string. | ||
The three triplets `" #", " # ", "# "` involving the original hash result in a new hash each. | ||
So the second line is: | ||
|
||
:: | ||
|
||
### | ||
|
||
If you propagate that line once again, the triplets with two hashes reult in a space: | ||
|
||
:: | ||
|
||
# # | ||
|
||
If you propagate once again, the first four lines look like: | ||
|
||
:: | ||
|
||
# | ||
### | ||
# # | ||
### ### | ||
|
||
Write a program that propagates the following line 32 times: | ||
|
||
.. code:: python3 | ||
line = " " * 32 + "#" + " " * 32 | ||
Add an empty space at the beginning and end of each new line so that the line length stays the same. | ||
|
||
.. hint:: | ||
|
||
Experiment with the rule set defined by the dictionary by changing its values. | ||
|
||
What you can practise in this coding challenge | ||
---------------------------------------------- | ||
|
||
- loops | ||
- string operations | ||
- looking up things in dictionaries | ||
|
||
.. seealso:: | ||
|
||
`Sierpinski Triangle on Wikipedia <https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle>__` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
code = { | ||
' ': ' ', | ||
' #': '#', | ||
' # ': '#', | ||
' ##': ' ', | ||
'# ': '#', | ||
'# #': ' ', | ||
'## ': ' ', | ||
'###': ' ', | ||
} | ||
|
||
N = 32 | ||
line = " " * N + "#" + " " * N | ||
for _ in range(N): | ||
print(line) | ||
new = " " | ||
for start in range(len(line) - 2): | ||
sub = line[start: start + 3] | ||
new += code[sub] | ||
line = new + " " |