Skip to content

Commit 57556ab

Browse files
authored
Merge branch 'master' into master
2 parents 0387ecb + 8d7ef6a commit 57556ab

Some content is hidden

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

59 files changed

+1377
-208
lines changed

.github/workflows/build.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@ jobs:
2020
- name: Install dependencies
2121
run: |
2222
python -m pip install --upgrade pip setuptools six wheel
23-
python -m pip install pytest-cov -r requirements.txt
23+
python -m pip install mypy pytest-cov -r requirements.txt
24+
# FIXME: #4052 fix mypy errors in other directories and add them here
25+
- run: mypy --ignore-missing-imports
26+
backtracking
27+
bit_manipulation
28+
blockchain
29+
boolean_algebra
30+
cellular_automata
31+
computer_vision
32+
fractals
33+
fuzzy_logic
34+
genetic_algorithm
35+
geodesy
36+
knapsack
37+
networking_flow
38+
scheduling sorts
2439
- name: Run tests
2540
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. .
2641
- if: ${{ success() }}

.github/workflows/pre-commit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
~/.cache/pip
1515
key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
1616
- uses: actions/setup-python@v2
17+
- uses: psf/black@stable
1718
- name: Install pre-commit
1819
run: |
1920
python -m pip install --upgrade pip

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.2.0
3+
rev: v3.4.0
44
hooks:
55
- id: check-executables-have-shebangs
66
- id: check-yaml
@@ -13,17 +13,17 @@ repos:
1313
)$
1414
- id: requirements-txt-fixer
1515
- repo: https://github.com/psf/black
16-
rev: stable
16+
rev: 20.8b1
1717
hooks:
1818
- id: black
1919
- repo: https://github.com/PyCQA/isort
20-
rev: 5.5.3
20+
rev: 5.7.0
2121
hooks:
2222
- id: isort
2323
args:
2424
- --profile=black
2525
- repo: https://gitlab.com/pycqa/flake8
26-
rev: 3.8.3
26+
rev: 3.9.0
2727
hooks:
2828
- id: flake8
2929
args:
@@ -38,11 +38,11 @@ repos:
3838
# args:
3939
# - --ignore-missing-imports
4040
- repo: https://github.com/codespell-project/codespell
41-
rev: v1.17.1
41+
rev: v2.0.0
4242
hooks:
4343
- id: codespell
4444
args:
45-
- --ignore-words-list=ans,fo,followings,hist,iff,mater,secant,som,tim
45+
- --ignore-words-list=ans,crate,fo,followings,hist,iff,mater,secant,som,tim
4646
- --skip="./.*,./other/dictionary.txt,./other/words,./project_euler/problem_022/p022_names.txt"
4747
- --quiet-level=2
4848
exclude: |

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ We want your work to be readable by others; therefore, we encourage you to note
155155
return a + b
156156
```
157157

158+
Instructions on how to install mypy can be found [here](https://github.com/python/mypy). Please use the command `mypy --ignore-missing-imports .` to test all files or `mypy --ignore-missing-imports path/to/file.py` to test a specific file.
159+
158160
- [__List comprehensions and generators__](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) are preferred over the use of `lambda`, `map`, `filter`, `reduce` but the important thing is to demonstrate the power of Python in code that is easy to read and maintain.
159161

160162
- Avoid importing external libraries for basic algorithms. Only use those libraries for complicated algorithms.

DIRECTORY.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
* [Binary Count Setbits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_count_setbits.py)
3030
* [Binary Count Trailing Zeros](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_count_trailing_zeros.py)
3131
* [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py)
32+
* [Binary Shifts](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_shifts.py)
33+
* [Binary Twos Complement](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_twos_complement.py)
3234
* [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py)
35+
* [Count Number Of One Bits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/count_number_of_one_bits.py)
3336
* [Reverse Bits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/reverse_bits.py)
3437
* [Single Bit Manipulation Operations](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/single_bit_manipulation_operations.py)
3538

@@ -276,6 +279,8 @@
276279

277280
## Graphics
278281
* [Bezier Curve](https://github.com/TheAlgorithms/Python/blob/master/graphics/bezier_curve.py)
282+
* [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/graphics/koch_snowflake.py)
283+
* [Mandelbrot](https://github.com/TheAlgorithms/Python/blob/master/graphics/mandelbrot.py)
279284
* [Vector3 For 2D Rendering](https://github.com/TheAlgorithms/Python/blob/master/graphics/vector3_for_2d_rendering.py)
280285

281286
## Graphs
@@ -468,6 +473,8 @@
468473
* [Runge Kutta](https://github.com/TheAlgorithms/Python/blob/master/maths/runge_kutta.py)
469474
* [Segmented Sieve](https://github.com/TheAlgorithms/Python/blob/master/maths/segmented_sieve.py)
470475
* Series
476+
* [Arithmetic Mean](https://github.com/TheAlgorithms/Python/blob/master/maths/series/arithmetic_mean.py)
477+
* [Geometric Mean](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric_mean.py)
471478
* [Geometric Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric_series.py)
472479
* [Harmonic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/harmonic_series.py)
473480
* [P Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/p_series.py)
@@ -523,6 +530,7 @@
523530
* [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py)
524531
* [Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py)
525532
* [Gauss Easter](https://github.com/TheAlgorithms/Python/blob/master/other/gauss_easter.py)
533+
* [Graham Scan](https://github.com/TheAlgorithms/Python/blob/master/other/graham_scan.py)
526534
* [Greedy](https://github.com/TheAlgorithms/Python/blob/master/other/greedy.py)
527535
* [Integeration By Simpson Approx](https://github.com/TheAlgorithms/Python/blob/master/other/integeration_by_simpson_approx.py)
528536
* [Largest Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/other/largest_subarray_sum.py)
@@ -755,6 +763,8 @@
755763
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_102/sol1.py)
756764
* Problem 107
757765
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_107/sol1.py)
766+
* Problem 109
767+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_109/sol1.py)
758768
* Problem 112
759769
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_112/sol1.py)
760770
* Problem 113
@@ -763,6 +773,8 @@
763773
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_119/sol1.py)
764774
* Problem 120
765775
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py)
776+
* Problem 121
777+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_121/sol1.py)
766778
* Problem 123
767779
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_123/sol1.py)
768780
* Problem 125
@@ -843,6 +855,7 @@
843855
* [Merge Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_insertion_sort.py)
844856
* [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py)
845857
* [Natural Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/natural_sort.py)
858+
* [Odd Even Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_sort.py)
846859
* [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py)
847860
* [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py)
848861
* [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py)
@@ -859,6 +872,7 @@
859872
* [Recursive Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_quick_sort.py)
860873
* [Selection Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py)
861874
* [Shell Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py)
875+
* [Slowsort](https://github.com/TheAlgorithms/Python/blob/master/sorts/slowsort.py)
862876
* [Stooge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/stooge_sort.py)
863877
* [Strand Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/strand_sort.py)
864878
* [Tim Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/tim_sort.py)

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 The Algorithms
3+
Copyright (c) 2016-2021 The Algorithms
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

bit_manipulation/binary_shifts.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Information on binary shifts:
2+
# https://docs.python.org/3/library/stdtypes.html#bitwise-operations-on-integer-types
3+
# https://www.interviewcake.com/concept/java/bit-shift
4+
5+
6+
def logical_left_shift(number: int, shift_amount: int) -> str:
7+
"""
8+
Take in 2 positive integers.
9+
'number' is the integer to be logically left shifted 'shift_amount' times.
10+
i.e. (number << shift_amount)
11+
Return the shifted binary representation.
12+
13+
>>> logical_left_shift(0, 1)
14+
'0b00'
15+
>>> logical_left_shift(1, 1)
16+
'0b10'
17+
>>> logical_left_shift(1, 5)
18+
'0b100000'
19+
>>> logical_left_shift(17, 2)
20+
'0b1000100'
21+
>>> logical_left_shift(1983, 4)
22+
'0b111101111110000'
23+
>>> logical_left_shift(1, -1)
24+
Traceback (most recent call last):
25+
...
26+
ValueError: both inputs must be positive integers
27+
"""
28+
if number < 0 or shift_amount < 0:
29+
raise ValueError("both inputs must be positive integers")
30+
31+
binary_number = str(bin(number))
32+
binary_number += "0" * shift_amount
33+
return binary_number
34+
35+
36+
def logical_right_shift(number: int, shift_amount: int) -> str:
37+
"""
38+
Take in positive 2 integers.
39+
'number' is the integer to be logically right shifted 'shift_amount' times.
40+
i.e. (number >>> shift_amount)
41+
Return the shifted binary representation.
42+
43+
>>> logical_right_shift(0, 1)
44+
'0b0'
45+
>>> logical_right_shift(1, 1)
46+
'0b0'
47+
>>> logical_right_shift(1, 5)
48+
'0b0'
49+
>>> logical_right_shift(17, 2)
50+
'0b100'
51+
>>> logical_right_shift(1983, 4)
52+
'0b1111011'
53+
>>> logical_right_shift(1, -1)
54+
Traceback (most recent call last):
55+
...
56+
ValueError: both inputs must be positive integers
57+
"""
58+
if number < 0 or shift_amount < 0:
59+
raise ValueError("both inputs must be positive integers")
60+
61+
binary_number = str(bin(number))[2:]
62+
if shift_amount >= len(binary_number):
63+
return "0b0"
64+
shifted_binary_number = binary_number[: len(binary_number) - shift_amount]
65+
return "0b" + shifted_binary_number
66+
67+
68+
def arithmetic_right_shift(number: int, shift_amount: int) -> str:
69+
"""
70+
Take in 2 integers.
71+
'number' is the integer to be arithmetically right shifted 'shift_amount' times.
72+
i.e. (number >> shift_amount)
73+
Return the shifted binary representation.
74+
75+
>>> arithmetic_right_shift(0, 1)
76+
'0b00'
77+
>>> arithmetic_right_shift(1, 1)
78+
'0b00'
79+
>>> arithmetic_right_shift(-1, 1)
80+
'0b11'
81+
>>> arithmetic_right_shift(17, 2)
82+
'0b000100'
83+
>>> arithmetic_right_shift(-17, 2)
84+
'0b111011'
85+
>>> arithmetic_right_shift(-1983, 4)
86+
'0b111110000100'
87+
"""
88+
if number >= 0: # Get binary representation of positive number
89+
binary_number = "0" + str(bin(number)).strip("-")[2:]
90+
else: # Get binary (2's complement) representation of negative number
91+
binary_number_length = len(bin(number)[3:]) # Find 2's complement of number
92+
binary_number = bin(abs(number) - (1 << binary_number_length))[3:]
93+
binary_number = (
94+
("1" + "0" * (binary_number_length - len(binary_number)) + binary_number)
95+
if number < 0
96+
else "0"
97+
)
98+
99+
if shift_amount >= len(binary_number):
100+
return "0b" + binary_number[0] * len(binary_number)
101+
return (
102+
"0b"
103+
+ binary_number[0] * shift_amount
104+
+ binary_number[: len(binary_number) - shift_amount]
105+
)
106+
107+
108+
if __name__ == "__main__":
109+
import doctest
110+
111+
doctest.testmod()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Information on 2's complement: https://en.wikipedia.org/wiki/Two%27s_complement
2+
3+
4+
def twos_complement(number: int) -> str:
5+
"""
6+
Take in a negative integer 'number'.
7+
Return the two's complement representation of 'number'.
8+
9+
>>> twos_complement(0)
10+
'0b0'
11+
>>> twos_complement(-1)
12+
'0b11'
13+
>>> twos_complement(-5)
14+
'0b1011'
15+
>>> twos_complement(-17)
16+
'0b101111'
17+
>>> twos_complement(-207)
18+
'0b100110001'
19+
>>> twos_complement(1)
20+
Traceback (most recent call last):
21+
...
22+
ValueError: input must be a negative integer
23+
"""
24+
if number > 0:
25+
raise ValueError("input must be a negative integer")
26+
binary_number_length = len(bin(number)[3:])
27+
twos_complement_number = bin(abs(number) - (1 << binary_number_length))[3:]
28+
twos_complement_number = (
29+
(
30+
"1"
31+
+ "0" * (binary_number_length - len(twos_complement_number))
32+
+ twos_complement_number
33+
)
34+
if number < 0
35+
else "0"
36+
)
37+
return "0b" + twos_complement_number
38+
39+
40+
if __name__ == "__main__":
41+
import doctest
42+
43+
doctest.testmod()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def get_set_bits_count(number: int) -> int:
2+
"""
3+
Count the number of set bits in a 32 bit integer
4+
>>> get_set_bits_count(25)
5+
3
6+
>>> get_set_bits_count(37)
7+
3
8+
>>> get_set_bits_count(21)
9+
3
10+
>>> get_set_bits_count(58)
11+
4
12+
>>> get_set_bits_count(0)
13+
0
14+
>>> get_set_bits_count(256)
15+
1
16+
>>> get_set_bits_count(-1)
17+
Traceback (most recent call last):
18+
...
19+
ValueError: the value of input must be positive
20+
"""
21+
if number < 0:
22+
raise ValueError("the value of input must be positive")
23+
result = 0
24+
while number:
25+
if number % 2 == 1:
26+
result += 1
27+
number = number >> 1
28+
return result
29+
30+
31+
if __name__ == "__main__":
32+
import doctest
33+
34+
doctest.testmod()

0 commit comments

Comments
 (0)