File tree Expand file tree Collapse file tree 19 files changed +551
-0
lines changed
Expand file tree Collapse file tree 19 files changed +551
-0
lines changed Original file line number Diff line number Diff line change 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.
116def calculate_pi (n_terms : int ) -> float :
217 numerator : float = 4.0
318 denominator : float = 1.0
Original file line number Diff line number Diff line change 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.
116def fib1 (n : int ) -> int :
217 return fib1 (n - 1 ) + fib1 (n - 2 )
318
419
520if __name__ == "__main__" :
621 print (fib1 (5 ))
22+ # Note that this example is purposefully wrong.
Original file line number Diff line number Diff line change 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.
116def fib2 (n : int ) -> int :
217 if n < 2 : # base case
318 return n
Original file line number Diff line number Diff line change 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.
116from typing import Dict
217memo : Dict [int , int ] = {0 : 0 , 1 : 1 } # our old base cases
318
Original file line number Diff line number Diff line change 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.
116from functools import lru_cache
217
318
Original file line number Diff line number Diff line change 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.
116def fib5 (n : int ) -> int :
217 if n == 0 : return n # special case
318 last : int = 0 # initially set to fib(0)
Original file line number Diff line number Diff line change 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.
116def fib6 (n : int ) -> int :
217 yield 0 # special case
318 if n > 0 : yield 1 # special case
Original file line number Diff line number Diff line change 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.
116from typing import TypeVar , Generic , List
217T = TypeVar ('T' )
318
Original file line number Diff line number Diff line change 1+ # trivial_compression.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.
116class CompressedGene :
217 def __init__ (self , gene : str ):
318 self ._compress (gene )
Original file line number Diff line number Diff line change 1+ # unbreakable_encryption.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.
116from secrets import token_bytes
217
318
You can’t perform that action at this time.
0 commit comments