Skip to content

Commit ed269ee

Browse files
author
Alexander Tometzki
committed
nothing was commited for some reason
1 parent 8a164b6 commit ed269ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3865
-0
lines changed

.gitignore 2

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/

Chapter1/calculating_pi 2.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# calculating_pi.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def calculate_pi(n_terms: int) -> float:
19+
numerator: float = 4.0
20+
denominator: float = 1.0
21+
operation: float = 1.0
22+
pi: float = 0.0
23+
for _ in range(n_terms):
24+
pi += operation * (numerator / denominator)
25+
denominator += 2.0
26+
operation *= -1.0
27+
return pi
28+
29+
30+
if __name__ == "__main__":
31+
print(calculate_pi(1000000))

Chapter1/fib1 2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# fib1.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def fib1(n: int) -> int:
19+
return fib1(n - 1) + fib1(n - 2)
20+
21+
22+
if __name__ == "__main__":
23+
print(fib1(5))
24+
# Note that this example is purposefully wrong.

Chapter1/fib2 2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# fib2.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def fib2(n: int) -> int:
19+
if n < 2: # base case
20+
return n
21+
return fib2(n - 2) + fib2(n - 1) # recursive case
22+
23+
24+
if __name__ == "__main__":
25+
print(fib2(5))
26+
print(fib2(10))

Chapter1/fib3 2.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# fib3.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
from typing import Dict
17+
memo: Dict[int, int] = {0: 0, 1: 1} # our base cases
18+
19+
20+
def fib3(n: int) -> int:
21+
if n not in memo:
22+
memo[n] = fib3(n - 1) + fib3(n - 2) # memoization
23+
return memo[n]
24+
25+
26+
if __name__ == "__main__":
27+
print(fib3(5))
28+
print(fib3(50))

Chapter1/fib4 2.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# fib4.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
from functools import lru_cache
17+
18+
19+
@lru_cache(maxsize=None)
20+
def fib4(n: int) -> int: # same definition as fib2()
21+
if n < 2: # base case
22+
return n
23+
return fib4(n - 2) + fib4(n - 1) # recursive case
24+
25+
26+
if __name__ == "__main__":
27+
print(fib4(5))
28+
print(fib4(50))

Chapter1/fib5 2.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# fib5.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
def fib5(n: int) -> int:
17+
if n == 0: return n # special case
18+
last: int = 0 # initially set to fib(0)
19+
next: int = 1 # initially set to fib(1)
20+
for _ in range(1, n):
21+
last, next = next, last + next
22+
return next
23+
24+
25+
if __name__ == "__main__":
26+
print(fib5(2))
27+
print(fib5(50))
28+

Chapter1/fib6 2.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# fib6.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
from typing import Generator
17+
18+
19+
def fib6(n: int) -> Generator[int, None, None]:
20+
yield 0 # special case
21+
if n > 0: yield 1 # special case
22+
last: int = 0 # initially set to fib(0)
23+
next: int = 1 # initially set to fib(1)
24+
for _ in range(1, n):
25+
last, next = next, last + next
26+
yield next # main generation step
27+
28+
29+
if __name__ == "__main__":
30+
for i in fib6(50):
31+
print(i)

Chapter1/hanoi 2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# hanoi.py
2+
# From Classic Computer Science Problems in Python Chapter 1
3+
# Copyright 2018 David Kopec
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
from typing import TypeVar, Generic, List
17+
T = TypeVar('T')
18+
19+
20+
class Stack(Generic[T]):
21+
22+
def __init__(self) -> None:
23+
self._container: List[T] = []
24+
25+
def push(self, item: T) -> None:
26+
self._container.append(item)
27+
28+
def pop(self) -> T:
29+
return self._container.pop()
30+
31+
def __repr__(self) -> str:
32+
return repr(self._container)
33+
34+
35+
num_discs: int = 3
36+
tower_a: Stack[int] = Stack()
37+
tower_b: Stack[int] = Stack()
38+
tower_c: Stack[int] = Stack()
39+
for i in range(1, num_discs + 1):
40+
tower_a.push(i)
41+
42+
43+
def hanoi(begin: Stack[int], end: Stack[int], temp: Stack[int], n: int) -> None:
44+
if n == 1:
45+
end.push(begin.pop())
46+
else:
47+
hanoi(begin, temp, end, n - 1)
48+
hanoi(begin, end, temp, 1)
49+
hanoi(temp, end, begin, n - 1)
50+
51+
52+
if __name__ == "__main__":
53+
hanoi(tower_a, tower_c, tower_b, num_discs)
54+
print(tower_a)
55+
print(tower_b)
56+
print(tower_c)

0 commit comments

Comments
 (0)