Skip to content

Commit 25e9989

Browse files
author
Kristian Rother
committed
add sierpinski challenge
1 parent 315128e commit 25e9989

4 files changed

Lines changed: 96 additions & 1 deletion

File tree

challenges/sierpinski.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Sierpinski Triangle
2+
===================
3+
4+
**🎯 Construct a Sierpinski Triangle with a Cellular Automaton.**
5+
6+
.. figure:: ../images/sierpinski.png
7+
8+
Consider the following rules:
9+
10+
.. code:: python3
11+
12+
rules = {
13+
' ': ' ',
14+
' #': '#',
15+
' # ': '#',
16+
' ##': ' ',
17+
'# ': '#',
18+
'# #': ' ',
19+
'## ': ' ',
20+
'###': ' ',
21+
}
22+
23+
Now when you start with a string consisting of a single hash flanked by spaces:
24+
25+
::
26+
27+
#
28+
29+
The rules define how the next line looks like.
30+
For the new line you look up character triplets from the original string.
31+
The three triplets `" #", " # ", "# "` involving the original hash result in a new hash each.
32+
So the second line is:
33+
34+
::
35+
36+
###
37+
38+
If you propagate that line once again, the triplets with two hashes reult in a space:
39+
40+
::
41+
42+
# #
43+
44+
If you propagate once again, the first four lines look like:
45+
46+
::
47+
48+
#
49+
###
50+
# #
51+
### ###
52+
53+
Write a program that propagates the following line 32 times:
54+
55+
.. code:: python3
56+
57+
line = " " * 32 + "#" + " " * 32
58+
59+
Add an empty space at the beginning and end of each new line so that the line length stays the same.
60+
61+
.. hint::
62+
63+
Experiment with the rule set defined by the dictionary by changing its values.
64+
65+
What you can practise in this coding challenge
66+
----------------------------------------------
67+
68+
- loops
69+
- string operations
70+
- looking up things in dictionaries
71+
72+
.. seealso::
73+
74+
`Sierpinski Triangle on Wikipedia <https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle>`__

images/sierpinski.png

27.1 KB
Loading

index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ Coding Challenges
5656
:maxdepth: 1
5757

5858
challenges/ctree.rst
59-
challenges/pyramid.rst
6059
challenges/fizzbuzz.rst
6160
challenges/quiz.rst
6261
challenges/checker.rst
@@ -70,6 +69,7 @@ Coding Challenges
7069
challenges/querprodukt.rst
7170
challenges/birthdays.rst
7271
challenges/misty_mountains.rst
72+
challenges/sierpinski.rst
7373
challenges/count_words.rst
7474
challenges/spiral.rst
7575

solutions/sierpinski.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
code = {
3+
' ': ' ',
4+
' #': '#',
5+
' # ': '#',
6+
' ##': ' ',
7+
'# ': '#',
8+
'# #': ' ',
9+
'## ': ' ',
10+
'###': ' ',
11+
}
12+
13+
N = 32
14+
line = " " * N + "#" + " " * N
15+
for _ in range(N):
16+
print(line)
17+
new = " "
18+
for start in range(len(line) - 2):
19+
sub = line[start: start + 3]
20+
new += code[sub]
21+
line = new + " "

0 commit comments

Comments
 (0)