Skip to content

Commit abd5cfc

Browse files
committed
Merge branch 'master' of git://github.com/TheAlgorithms/Python
2 parents a65f73a + e313141 commit abd5cfc

File tree

157 files changed

+5402
-2761
lines changed

Some content is hidden

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

157 files changed

+5402
-2761
lines changed

Diff for: .travis.yml

+10-24
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,17 @@ before_install: pip install --upgrade pip setuptools
66
install: pip install -r requirements.txt
77
before_script:
88
- black --check . || true
9-
- flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
9+
- flake8 . --count --select=E9,F401,F63,F7,F82 --show-source --statistics
1010
script:
11+
- scripts/validate_filenames.py # no uppercase and no spaces
1112
- mypy --ignore-missing-imports .
12-
#- IGNORE="data_structures,file_transfer_protocol,graphs,machine_learning,maths,neural_network,project_euler"
13-
#- pytest . --doctest-modules --ignore=${IGNORE}
14-
- pytest --doctest-modules
15-
arithmetic_analysis
16-
backtracking
17-
boolean_algebra
18-
ciphers
19-
compression
20-
conversions
21-
digital_image_processing
22-
divide_and_conquer
23-
dynamic_programming
24-
hashes
25-
linear_algebra_python
26-
matrix
27-
networking_flow
28-
other
29-
searches
30-
sorts
31-
strings
32-
traversals
33-
13+
- pytest . --doctest-modules
14+
--ignore=file_transfer_protocol/ftp_send_receive.py
15+
--ignore=file_transfer_protocol/ftp_client_server.py
16+
--ignore=machine_learning/linear_regression.py
17+
--ignore=machine_learning/perceptron.py
18+
--ignore=machine_learning/random_forest_classification/random_forest_classification.py
19+
--ignore=machine_learning/random_forest_regression/random_forest_regression.py
3420
after_success:
35-
- python scripts/build_directory_md.py
21+
- scripts/build_directory_md.py > DIRECTORY.md
3622
- cat DIRECTORY.md

Diff for: CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ We want your work to be readable by others; therefore, we encourage you to note
6969
"""
7070
This function sums two integers a and b
7171
Return: a + b
72-
"""
72+
"""
7373
return a + b
7474
```
7575

Diff for: DIRECTORY.md

+24-20
Large diffs are not rendered by default.

Diff for: README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# The Algorithms - Python <!-- [![Build Status](https://travis-ci.org/TheAlgorithms/Python.svg)](https://travis-ci.org/TheAlgorithms/Python) -->
2-
3-
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/TheAlgorithms/100) &nbsp;
4-
[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/TheAlgorithms) &nbsp;
5-
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/TheAlgorithms/Python)
6-
2+
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg?logo=paypal&style=flat-square)](https://www.paypal.me/TheAlgorithms/100)&nbsp;
3+
[![Build Status](https://img.shields.io/travis/TheAlgorithms/Python.svg?label=Travis%20CI&logo=travis&style=flat-square)](https://travis-ci.org/TheAlgorithms/Python)&nbsp;
4+
[![LGTM](https://img.shields.io/lgtm/alerts/github/TheAlgorithms/Python.svg?label=LGTM&logo=LGTM&style=flat-square)](https://lgtm.com/projects/g/TheAlgorithms/Python/alerts)&nbsp;
5+
[![Gitter chat](https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square)](https://gitter.im/TheAlgorithms)&nbsp;
6+
[![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md)&nbsp;
7+
![](https://img.shields.io/github/repo-size/TheAlgorithms/Python.svg?label=Repo%20size&style=flat-square)&nbsp;
8+
<!--[![Tested on Python 3.7](https://img.shields.io/badge/Tested%20-Python%203.7-blue.svg?logo=python&style=flat-square)]( https://www.python.org/downloads) &nbsp;-->
79
### All algorithms implemented in Python (for education)
810

911
These implementations are for learning purposes. They may be less efficient than the implementations in the Python standard library.
@@ -24,6 +26,8 @@ Chetan Kaushik
2426

2527
Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
2628

29+
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg?style=flat-square)](https://gitpod.io/#https://github.com/TheAlgorithms/Python)
30+
2731
## Community Channel
2832

2933
We're on [Gitter](https://gitter.im/TheAlgorithms)! Please join us.

Diff for: arithmetic_analysis/image_data/2D_problems.JPG

57.4 KB
Loading

Diff for: arithmetic_analysis/image_data/2D_problems_1.JPG

40.4 KB
Loading

Diff for: arithmetic_analysis/in_static_equilibrium.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Checks if a system of forces is in static equilibrium.
3+
4+
python/black : true
5+
flake8 : passed
6+
mypy : passed
7+
"""
8+
9+
from numpy import array, cos, sin, radians, cross # type: ignore
10+
from typing import List
11+
12+
13+
def polar_force(
14+
magnitude: float, angle: float, radian_mode: bool = False
15+
) -> List[float]:
16+
"""
17+
Resolves force along rectangular components.
18+
(force, angle) => (force_x, force_y)
19+
>>> polar_force(10, 45)
20+
[7.0710678118654755, 7.071067811865475]
21+
>>> polar_force(10, 3.14, radian_mode=True)
22+
[-9.999987317275394, 0.01592652916486828]
23+
"""
24+
if radian_mode:
25+
return [magnitude * cos(angle), magnitude * sin(angle)]
26+
return [magnitude * cos(radians(angle)), magnitude * sin(radians(angle))]
27+
28+
29+
def in_static_equilibrium(
30+
forces: array, location: array, eps: float = 10 ** -1
31+
) -> bool:
32+
"""
33+
Check if a system is in equilibrium.
34+
It takes two numpy.array objects.
35+
forces ==> [
36+
[force1_x, force1_y],
37+
[force2_x, force2_y],
38+
....]
39+
location ==> [
40+
[x1, y1],
41+
[x2, y2],
42+
....]
43+
>>> force = array([[1, 1], [-1, 2]])
44+
>>> location = array([[1, 0], [10, 0]])
45+
>>> in_static_equilibrium(force, location)
46+
False
47+
"""
48+
# summation of moments is zero
49+
moments: array = cross(location, forces)
50+
sum_moments: float = sum(moments)
51+
return abs(sum_moments) < eps
52+
53+
54+
if __name__ == "__main__":
55+
# Test to check if it works
56+
forces = array(
57+
[
58+
polar_force(718.4, 180 - 30),
59+
polar_force(879.54, 45),
60+
polar_force(100, -90)
61+
])
62+
63+
location = array([[0, 0], [0, 0], [0, 0]])
64+
65+
assert in_static_equilibrium(forces, location)
66+
67+
# Problem 1 in image_data/2D_problems.jpg
68+
forces = array(
69+
[
70+
polar_force(30 * 9.81, 15),
71+
polar_force(215, 180 - 45),
72+
polar_force(264, 90 - 30),
73+
]
74+
)
75+
76+
location = array([[0, 0], [0, 0], [0, 0]])
77+
78+
assert in_static_equilibrium(forces, location)
79+
80+
# Problem in image_data/2D_problems_1.jpg
81+
forces = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]])
82+
83+
location = array([[0, 0], [6, 0], [10, 0], [12, 0]])
84+
85+
assert in_static_equilibrium(forces, location)
86+
87+
import doctest
88+
89+
doctest.testmod()

Diff for: arithmetic_analysis/newton_raphson_method.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Implementing Newton Raphson method in Python
2-
# Author: Haseeb
2+
# Author: Syed Haseeb Shah (github.com/QuantumNovice)
33

44
from sympy import diff
55
from decimal import Decimal
@@ -30,7 +30,3 @@ def NewtonRaphson(func, a):
3030

3131
# Exponential Roots
3232
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
33-
34-
35-
36-

Diff for: backtracking/all_combinations.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
In this problem, we want to determine all possible combinations of k
5+
numbers out of 1 ... n. We use backtracking to solve this problem.
6+
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!)))
7+
"""
8+
9+
10+
def generate_all_combinations(n: int, k: int) -> [[int]]:
11+
"""
12+
>>> generate_all_combinations(n=4, k=2)
13+
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
14+
"""
15+
16+
result = []
17+
create_all_state(1, n, k, [], result)
18+
return result
19+
20+
21+
def create_all_state(increment, total_number, level, current_list, total_list):
22+
if level == 0:
23+
total_list.append(current_list[:])
24+
return
25+
26+
for i in range(increment, total_number - level + 2):
27+
current_list.append(i)
28+
create_all_state(i + 1, total_number, level - 1, current_list, total_list)
29+
current_list.pop()
30+
31+
32+
def print_all_state(total_list):
33+
for i in total_list:
34+
print(*i)
35+
36+
37+
if __name__ == '__main__':
38+
n = 4
39+
k = 2
40+
total_list = generate_all_combinations(n, k)
41+
print_all_state(total_list)

0 commit comments

Comments
 (0)