Skip to content

Commit 990375d

Browse files
committed
Merge branch 'master' of https://github.com/TheAlgorithms/Python into issue#817
2 parents 59858fe + e2d9953 commit 990375d

File tree

89 files changed

+1706
-943
lines changed

Some content is hidden

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

89 files changed

+1706
-943
lines changed

.github/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: TheAlgorithms
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: ['http://paypal.me/TheAlgorithms/1000', 'https://donorbox.org/thealgorithms']

.lgtm.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.travis.yml

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
language: python
2+
dist: xenial # required for Python >= 3.7
3+
python: 3.7
24
cache: pip
3-
python:
4-
- 2.7
5-
- 3.6
6-
#- nightly
7-
#- pypy
8-
#- pypy3
9-
matrix:
10-
allow_failures:
11-
- python: nightly
12-
- python: pypy
13-
- python: pypy3
14-
install:
15-
#- pip install -r requirements.txt
16-
- pip install flake8 # pytest # add another testing frameworks later
5+
install: pip install -r requirements.txt
176
before_script:
18-
# stop the build if there are Python syntax errors or undefined names
19-
- flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics
20-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
21-
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
7+
- black --check . || true
8+
- flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
229
script:
23-
- true # pytest --capture=sys # add other tests here
24-
notifications:
25-
on_success: change
26-
on_failure: change # `always` will be the setting once code changes slow down
10+
- mypy --ignore-missing-imports .
11+
- pytest --doctest-modules ./ciphers ./other ./searches ./sorts ./strings
12+
after_success:
13+
- python ./~script.py
14+
- cat DIRECTORY.md

CONTRIBUTING.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ We appreciate any contribution, from fixing a grammar mistake in a comment to im
2828
We want your work to be readable by others; therefore, we encourage you to note the following:
2929

3030
- Please write in Python 3.x.
31+
- Please consider running [__python/black__](https://github.com/python/black) on your Python file(s) before submitting your pull request. This is not a requirement but it does make your code more readable. There are other code formatters (autopep8, yapf) but the __black__ style is now the recommendation of the Python core team. To use it,
32+
```bash
33+
pip3 install black # only required the first time
34+
black my-submission.py
35+
```
36+
37+
- All submissions will need to pass the test __flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics__ before they will be accepted so if possible, try this test locally on your Python file(s) before submitting your pull request.
3138

3239
- If you know [PEP 8](https://www.python.org/dev/peps/pep-0008/) already, you will have no problem in coding style, though we do not follow it strictly. Read the remaining section and have fun coding!
3340

@@ -72,13 +79,19 @@ We want your work to be readable by others; therefore, we encourage you to note
7279

7380
- Write tests to illustrate your work.
7481

75-
The following "testing" approaches are not encouraged:
82+
The following "testing" approaches are **not** encouraged:
7683

7784
```python
7885
input('Enter your input:')
7986
# Or even worse...
8087
input = eval(raw_input("Enter your input: "))
8188
```
89+
90+
However, if your code uses __input()__ then we encourage you to gracefully deal with leading and trailing whitespace in user input by adding __.strip()__ to the end as in:
91+
92+
```python
93+
starting_value = int(input("Please enter a starting value: ").strip())
94+
```
8295

8396
Please write down your test case, like the following:
8497

@@ -92,30 +105,32 @@ We want your work to be readable by others; therefore, we encourage you to note
92105
print("1 + 2 = ", sumab(1,2)) # 1+2 = 3
93106
print("6 + 4 = ", sumab(6,4)) # 6+4 = 10
94107
```
108+
109+
Better yet, if you know how to write [__doctests__](https://docs.python.org/3/library/doctest.html), please consider adding them.
95110

96-
- Avoid importing external libraries for basic algorithms. Use those libraries for complicated algorithms.
111+
- Avoid importing external libraries for basic algorithms. Only use those libraries for complicated algorithms.
97112

98113
#### Other Standard While Submitting Your Work
99114

100-
- File extension for code should be `.py`.
115+
- File extension for code should be `.py`. Jupiter notebook files are acceptable in machine learning algorithms.
101116

102-
- Please file your work to let others use it in the future. Here are the examples that are acceptable:
103-
104-
- Camel cases
105-
- `-` Hyphenated names
106-
- `_` Underscore-separated names
117+
- Strictly use snake case (underscore separated) in your file name, as it will be easy to parse in future using scripts.
107118

108119
If possible, follow the standard *within* the folder you are submitting to.
109120

110121
- If you have modified/added code work, make sure the code compiles before submitting.
111122

112-
- If you have modified/added documentation work, make sure your language is concise and contains no grammar mistake.
123+
- If you have modified/added documentation work, ensure your language is concise and contains no grammar errors.
124+
125+
- Do not update the README.md or DIRECTORY.md file which will be periodically autogenerated by our Travis CI processes.
113126

114127
- Add a corresponding explanation to [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation) (Optional but recommended).
115128

129+
- All submissions will be tested with [__mypy__](http://www.mypy-lang.org) so we encourage to add [__Python type hints__](https://docs.python.org/3/library/typing.html) where it makes sense to do so.
130+
116131
- Most importantly,
117132

118-
- **Be consistent with this guidelines while submitting.**
133+
- **Be consistent in the use of these guidelines when submitting.**
119134
- **Join** [Gitter](https://gitter.im/TheAlgorithms) **now!**
120135
- Happy coding!
121136

DIRECTORY.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
## Analysis
2-
* Compression Analysis
3-
* [psnr](https://github.com/TheAlgorithms/Python/blob/master/analysis/compression_analysis/psnr.py)
41
## Arithmetic Analysis
52
* [bisection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/bisection.py)
63
* [intersection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/intersection.py)
@@ -39,17 +36,19 @@
3936
* [xor cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/xor_cipher.py)
4037
## Compression
4138
* [huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py)
39+
## Compression Analysis
40+
* [psnr](https://github.com/TheAlgorithms/Python/blob/master/compression_analysis/psnr.py)
4241
## Data Structures
4342
* [arrays](https://github.com/TheAlgorithms/Python/blob/master/data_structures/arrays.py)
4443
* [avl](https://github.com/TheAlgorithms/Python/blob/master/data_structures/avl.py)
4544
* [LCA](https://github.com/TheAlgorithms/Python/blob/master/data_structures/LCA.py)
4645
* Binary Tree
47-
* [AVL tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary%20tree/AVL_tree.py)
48-
* [binary search tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary%20tree/binary_search_tree.py)
49-
* [fenwick tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary%20tree/fenwick_tree.py)
50-
* [lazy segment tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary%20tree/lazy_segment_tree.py)
51-
* [segment tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary%20tree/segment_tree.py)
52-
* [treap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary%20tree/treap.py)
46+
* [AVL tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/AVL_tree.py)
47+
* [binary search tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_search_tree.py)
48+
* [fenwick tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/fenwick_tree.py)
49+
* [lazy segment tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/lazy_segment_tree.py)
50+
* [segment tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/segment_tree.py)
51+
* [treap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/treap.py)
5352
* Hashing
5453
* [double hash](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/double_hash.py)
5554
* [hash table](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/hash_table.py)
@@ -192,8 +191,9 @@
192191
* Tests
193192
* [test fibonacci](https://github.com/TheAlgorithms/Python/blob/master/maths/tests/test_fibonacci.py)
194193
## Matrix
195-
* [matrix multiplication addition](https://github.com/TheAlgorithms/Python/blob/master/matrix/matrix_multiplication_addition.py)
194+
* [matrix operation](https://github.com/TheAlgorithms/Python/blob/master/matrix/matrix_operation.py)
196195
* [searching in sorted matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/searching_in_sorted_matrix.py)
196+
* [spiralPrint](https://github.com/TheAlgorithms/Python/blob/master/matrix/spiralPrint.py)
197197
## Networking Flow
198198
* [ford fulkerson](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/ford_fulkerson.py)
199199
* [minimum cut](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/minimum_cut.py)
@@ -294,6 +294,8 @@
294294
* [p022 names](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/p022_names.txt)
295295
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/sol1.py)
296296
* [sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/sol2.py)
297+
* Problem 234
298+
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py)
297299
* Problem 24
298300
* [sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_24/sol1.py)
299301
* Problem 25

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# The Algorithms - Python <!-- [![Build Status](https://travis-ci.org/TheAlgorithms/Python.svg)](https://travis-ci.org/TheAlgorithms/Python) -->
2+
23
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/TheAlgorithms/100) &nbsp;
34
[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/TheAlgorithms) &nbsp;
45
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/TheAlgorithms/Python)
@@ -7,6 +8,17 @@
78

89
These implementations are for learning purposes. They may be less efficient than the implementations in the Python standard library.
910

11+
## Owners
12+
13+
Anup Kumar Panwar
14+
&nbsp; [[Gmail](mailto:[email protected]?Subject=The%20Algorithms%20-%20Python)
15+
&nbsp; [GitHub](https://github.com/anupkumarpanwar)
16+
&nbsp; [LinkedIn](https://www.linkedin.com/in/anupkumarpanwar/)]
17+
18+
Chetan Kaushik
19+
&nbsp; [[Gmail](mailto:[email protected]?Subject=The%20Algorithms%20-%20Python)
20+
&nbsp; [GitHub](https://github.com/dynamitechetan)
21+
&nbsp; [LinkedIn](https://www.linkedin.com/in/chetankaushik/)]
1022

1123
## Contribution Guidelines
1224

@@ -15,3 +27,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
1527
## Community Channel
1628

1729
We're on [Gitter](https://gitter.im/TheAlgorithms)! Please join us.
30+
31+
## Algorithms
32+
33+
See our [directory](DIRECTORY.md).

backtracking/all_permutations.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
In this problem, we want to determine all possible permutations
3+
of the given sequence. We use backtracking to solve this problem.
4+
5+
Time complexity: O(n! * n),
6+
where n denotes the length of the given sequence.
7+
'''
8+
9+
10+
def generate_all_permutations(sequence):
11+
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
12+
13+
14+
def create_state_space_tree(sequence, current_sequence, index, index_used):
15+
'''
16+
Creates a state space tree to iterate through each branch using DFS.
17+
We know that each state has exactly len(sequence) - index children.
18+
It terminates when it reaches the end of the given sequence.
19+
'''
20+
21+
if index == len(sequence):
22+
print(current_sequence)
23+
return
24+
25+
for i in range(len(sequence)):
26+
if not index_used[i]:
27+
current_sequence.append(sequence[i])
28+
index_used[i] = True
29+
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
30+
current_sequence.pop()
31+
index_used[i] = False
32+
33+
34+
'''
35+
remove the comment to take an input from the user
36+
37+
print("Enter the elements")
38+
sequence = list(map(int, input().split()))
39+
'''
40+
41+
sequence = [3, 1, 2, 4]
42+
generate_all_permutations(sequence)
43+
44+
sequence = ["A", "B", "C"]
45+
generate_all_permutations(sequence)

backtracking/all_subsequences.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
In this problem, we want to determine all possible subsequences
3+
of the given sequence. We use backtracking to solve this problem.
4+
5+
Time complexity: O(2^n),
6+
where n denotes the length of the given sequence.
7+
'''
8+
9+
10+
def generate_all_subsequences(sequence):
11+
create_state_space_tree(sequence, [], 0)
12+
13+
14+
def create_state_space_tree(sequence, current_subsequence, index):
15+
'''
16+
Creates a state space tree to iterate through each branch using DFS.
17+
We know that each state has exactly two children.
18+
It terminates when it reaches the end of the given sequence.
19+
'''
20+
21+
if index == len(sequence):
22+
print(current_subsequence)
23+
return
24+
25+
create_state_space_tree(sequence, current_subsequence, index + 1)
26+
current_subsequence.append(sequence[index])
27+
create_state_space_tree(sequence, current_subsequence, index + 1)
28+
current_subsequence.pop()
29+
30+
31+
'''
32+
remove the comment to take an input from the user
33+
34+
print("Enter the elements")
35+
sequence = list(map(int, input().split()))
36+
'''
37+
38+
sequence = [3, 1, 2, 4]
39+
generate_all_subsequences(sequence)
40+
41+
sequence = ["A", "B", "C"]
42+
generate_all_subsequences(sequence)

backtracking/minimax.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import math
2+
3+
''' Minimax helps to achieve maximum score in a game by checking all possible moves
4+
depth is current depth in game tree.
5+
nodeIndex is index of current node in scores[].
6+
if move is of maximizer return true else false
7+
leaves of game tree is stored in scores[]
8+
height is maximum height of Game tree
9+
'''
10+
11+
def minimax (Depth, nodeIndex, isMax, scores, height):
12+
13+
if Depth == height:
14+
return scores[nodeIndex]
15+
16+
if isMax:
17+
return (max(minimax(Depth + 1, nodeIndex * 2, False, scores, height),
18+
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)))
19+
return (min(minimax(Depth + 1, nodeIndex * 2, True, scores, height),
20+
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)))
21+
22+
if __name__ == "__main__":
23+
24+
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
25+
height = math.log(len(scores), 2)
26+
27+
print("Optimal value : ", end = "")
28+
print(minimax(0, 0, True, scores, height))

0 commit comments

Comments
 (0)