Skip to content

Commit 6e5fe3a

Browse files
authored
Merge branch 'master' into tabs-to-spaces
2 parents 00f7602 + e8aa812 commit 6e5fe3a

File tree

100 files changed

+5491
-6959
lines changed

Some content is hidden

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

100 files changed

+5491
-6959
lines changed

.coveragerc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[report]
2+
sort = Cover
3+
omit =
4+
.env/*

.github/workflows/autoblack.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# GitHub Action that uses Black to reformat Python code (if needed) when doing a git push.
2+
# If all Python code in the repo is complient with Black then this Action does nothing.
3+
# Otherwise, Black is run and its changes are committed to the repo.
4+
# https://github.com/cclauss/autoblack
5+
6+
name: autoblack_push
7+
on: [push]
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v1
13+
- uses: actions/setup-python@v1
14+
- run: pip install black
15+
- run: black --check .
16+
- name: If needed, commit black changes to a new pull request
17+
if: failure()
18+
run: |
19+
black .
20+
git config --global user.name github-actions
21+
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
22+
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
23+
git commit -am "fixup! Format Python code with psf/black push"
24+
git push --force origin HEAD:$GITHUB_REF
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The objective of this GitHub Action is to update the DIRECTORY.md file (if needed)
2+
# when doing a git push
3+
name: directory_writer
4+
on: [push]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: actions/setup-python@v1
11+
with:
12+
python-version: 3.x
13+
- name: Update DIRECTORY.md
14+
run: |
15+
scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
16+
git config --global user.name github-actions
17+
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
18+
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
19+
git commit -am "updating DIRECTORY.md" || true
20+
git push --force origin HEAD:$GITHUB_REF || true

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ before_install: pip install --upgrade pip setuptools
55
install: pip install -r requirements.txt
66
before_script:
77
- black --check . || true
8-
- flake8 . --count --select=E9,F4,F63,F7,F82 --show-source --statistics
8+
- flake8 . --count --select=E101,E9,F4,F63,F7,F82,W191 --show-source --statistics
99
script:
1010
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
1111
- mypy --ignore-missing-imports .
12-
- pytest . --doctest-modules
12+
- pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=. .
1313
after_success:
14-
- scripts/build_directory_md.py > DIRECTORY.md
15-
- cat DIRECTORY.md
14+
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md

DIRECTORY.md

Lines changed: 484 additions & 409 deletions
Large diffs are not rendered by default.

Travis_CI_tests_are_failing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Travis CI test are failing
2+
### How do I find out what is wrong with my pull request?
3+
1. In your PR look for the failing test and click the `Details` link: ![Travis_CI_fail_1.png](images/Travis_CI_fail_1.png)
4+
2. On the next page, click `The build failed` link: ![Travis_CI_fail_2.png](images/Travis_CI_fail_2.png)
5+
3. Now scroll down and look for `red` text describing the error(s) in the test log.
6+
7+
Pull requests will __not__ be merged if the Travis CI tests are failing.
8+
9+
If anything is unclear, please read through [CONTRIBUTING.md](CONTRIBUTING.md) and attempt to run the failing tests on your computer before asking for assistance.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Gaussian elimination method for solving a system of linear equations.
3+
Gaussian elimination - https://en.wikipedia.org/wiki/Gaussian_elimination
4+
"""
5+
6+
7+
import numpy as np
8+
9+
10+
def retroactive_resolution(coefficients: np.matrix, vector: np.array) -> np.array:
11+
"""
12+
This function performs a retroactive linear system resolution
13+
for triangular matrix
14+
15+
Examples:
16+
2x1 + 2x2 - 1x3 = 5 2x1 + 2x2 = -1
17+
0x1 - 2x2 - 1x3 = -7 0x1 - 2x2 = -1
18+
0x1 + 0x2 + 5x3 = 15
19+
>>> gaussian_elimination([[2, 2, -1], [0, -2, -1], [0, 0, 5]], [[5], [-7], [15]])
20+
array([[2.],
21+
[2.],
22+
[3.]])
23+
>>> gaussian_elimination([[2, 2], [0, -2]], [[-1], [-1]])
24+
array([[-1. ],
25+
[ 0.5]])
26+
"""
27+
28+
rows, columns = np.shape(coefficients)
29+
30+
x = np.zeros((rows, 1), dtype=float)
31+
for row in reversed(range(rows)):
32+
sum = 0
33+
for col in range(row + 1, columns):
34+
sum += coefficients[row, col] * x[col]
35+
36+
x[row, 0] = (vector[row] - sum) / coefficients[row, row]
37+
38+
return x
39+
40+
41+
def gaussian_elimination(coefficients: np.matrix, vector: np.array) -> np.array:
42+
"""
43+
This function performs Gaussian elimination method
44+
45+
Examples:
46+
1x1 - 4x2 - 2x3 = -2 1x1 + 2x2 = 5
47+
5x1 + 2x2 - 2x3 = -3 5x1 + 2x2 = 5
48+
1x1 - 1x2 + 0x3 = 4
49+
>>> gaussian_elimination([[1, -4, -2], [5, 2, -2], [1, -1, 0]], [[-2], [-3], [4]])
50+
array([[ 2.3 ],
51+
[-1.7 ],
52+
[ 5.55]])
53+
>>> gaussian_elimination([[1, 2], [5, 2]], [[5], [5]])
54+
array([[0. ],
55+
[2.5]])
56+
"""
57+
# coefficients must to be a square matrix so we need to check first
58+
rows, columns = np.shape(coefficients)
59+
if rows != columns:
60+
return []
61+
62+
# augmented matrix
63+
augmented_mat = np.concatenate((coefficients, vector), axis=1)
64+
augmented_mat = augmented_mat.astype("float64")
65+
66+
# scale the matrix leaving it triangular
67+
for row in range(rows - 1):
68+
pivot = augmented_mat[row, row]
69+
for col in range(row + 1, columns):
70+
factor = augmented_mat[col, row] / pivot
71+
augmented_mat[col, :] -= factor * augmented_mat[row, :]
72+
73+
x = retroactive_resolution(
74+
augmented_mat[:, 0:columns], augmented_mat[:, columns : columns + 1]
75+
)
76+
77+
return x
78+
79+
80+
if __name__ == "__main__":
81+
import doctest
82+
83+
doctest.testmod()

arithmetic_analysis/secant_method.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Implementing Secant method in Python
2+
# Author: dimgrichr
3+
4+
5+
from math import exp
6+
7+
8+
def f(x):
9+
"""
10+
>>> f(5)
11+
39.98652410600183
12+
"""
13+
return 8 * x - 2 * exp(-x)
14+
15+
16+
def SecantMethod(lower_bound, upper_bound, repeats):
17+
"""
18+
>>> SecantMethod(1, 3, 2)
19+
0.2139409276214589
20+
"""
21+
x0 = lower_bound
22+
x1 = upper_bound
23+
for i in range(0, repeats):
24+
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
25+
return x1
26+
27+
28+
print(f"The solution is: {SecantMethod(1, 3, 2)}")

backtracking/all_combinations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
22

33
"""
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)!)))
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)!)))
77
"""
88

99

backtracking/all_permutations.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
In this problem, we want to determine all possible permutations
3-
of the given sequence. We use backtracking to solve this problem.
2+
In this problem, we want to determine all possible permutations
3+
of the given sequence. We use backtracking to solve this problem.
44
5-
Time complexity: O(n! * n),
6-
where n denotes the length of the given sequence.
5+
Time complexity: O(n! * n),
6+
where n denotes the length of the given sequence.
77
"""
88

99

@@ -13,10 +13,10 @@ def generate_all_permutations(sequence):
1313

1414
def create_state_space_tree(sequence, current_sequence, index, index_used):
1515
"""
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-
"""
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+
"""
2020

2121
if index == len(sequence):
2222
print(current_sequence)
@@ -32,7 +32,7 @@ def create_state_space_tree(sequence, current_sequence, index, index_used):
3232

3333

3434
"""
35-
remove the comment to take an input from the user
35+
remove the comment to take an input from the user
3636
3737
print("Enter the elements")
3838
sequence = list(map(int, input().split()))

backtracking/all_subsequences.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
In this problem, we want to determine all possible subsequences
3-
of the given sequence. We use backtracking to solve this problem.
2+
In this problem, we want to determine all possible subsequences
3+
of the given sequence. We use backtracking to solve this problem.
44
5-
Time complexity: O(2^n),
6-
where n denotes the length of the given sequence.
5+
Time complexity: O(2^n),
6+
where n denotes the length of the given sequence.
77
"""
88

99

@@ -13,10 +13,10 @@ def generate_all_subsequences(sequence):
1313

1414
def create_state_space_tree(sequence, current_subsequence, index):
1515
"""
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-
"""
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+
"""
2020

2121
if index == len(sequence):
2222
print(current_subsequence)
@@ -29,7 +29,7 @@ def create_state_space_tree(sequence, current_subsequence, index):
2929

3030

3131
"""
32-
remove the comment to take an input from the user
32+
remove the comment to take an input from the user
3333
3434
print("Enter the elements")
3535
sequence = list(map(int, input().split()))

backtracking/sum_of_subsets.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
The sum-of-subsetsproblem states that a set of non-negative integers, and a value M,
3-
determine all possible subsets of the given set whose summation sum equal to given M.
2+
The sum-of-subsetsproblem states that a set of non-negative integers, and a value M,
3+
determine all possible subsets of the given set whose summation sum equal to given M.
44
5-
Summation of the chosen numbers must be equal to given number M and one number can
6-
be used only once.
5+
Summation of the chosen numbers must be equal to given number M and one number can
6+
be used only once.
77
"""
88

99

@@ -18,12 +18,12 @@ def generate_sum_of_subsets_soln(nums, max_sum):
1818

1919
def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum):
2020
"""
21-
Creates a state space tree to iterate through each branch using DFS.
22-
It terminates the branching of a node when any of the two conditions
23-
given below satisfy.
24-
This algorithm follows depth-fist-search and backtracks when the node is not branchable.
21+
Creates a state space tree to iterate through each branch using DFS.
22+
It terminates the branching of a node when any of the two conditions
23+
given below satisfy.
24+
This algorithm follows depth-fist-search and backtracks when the node is not branchable.
2525
26-
"""
26+
"""
2727
if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum:
2828
return
2929
if sum(path) == max_sum:
@@ -41,7 +41,7 @@ def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nu
4141

4242

4343
"""
44-
remove the comment to take an input from the user
44+
remove the comment to take an input from the user
4545
4646
print("Enter the elements")
4747
nums = list(map(int, input().split()))

0 commit comments

Comments
 (0)