Skip to content

Commit

Permalink
Improved hopefully script run_tests.sh (called previously automate.sh)
Browse files Browse the repository at this point in the history
- Fixed a few coding issues reported by coach
  • Loading branch information
nbro committed Feb 14, 2017
1 parent 1e9214f commit aeb2e2f
Show file tree
Hide file tree
Showing 21 changed files with 256 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ venv/
env/
*.egg-info/
__pycache__/
/scripts/
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ First things first, you should fork the [`ands` repository from my `Github`'s ac

Before that though, you should write tests to test what you've done. I would like that every algorithm and data structure is tested (which is not the case right now), in order to reduce the number of bugs as much as possible. Read the next section to know more about testing.

Furthermore, before committing your changes, I recommended you to run the script [`./automate.sh`](./automate.sh), which is responsible for doing the following tasks:
Furthermore, before committing your changes, I recommended you to run the script [`./run_tests.sh`](./run_tests.sh), which is responsible for doing the following tasks:

1. Format all the source code using the tool `autopep8`

Expand Down Expand Up @@ -120,16 +120,16 @@ This last one should also report you the amount of code covered by the tests, af

coverage report

## [`automate.sh`](./automate.sh)
## [`run_tests.sh`](./run_tests.sh)

I created this script to automate the tasks of creating the virtual environment, installing dependencies, running the tests and reporting its results.
I created this script to automate the tasks of creating the virtual environment, installing dependencies, running the tests and reporting its results, which includes reporting code coverage.
If you want to run the script by running all tests (which takes some time), from inside the main folder, execute the following command on the terminal

./automate.sh
./run_tests.sh

On the other hand, if you want to run a specific single-class test, e.g. [`test_DSForests.py`](./tests/ds/test_DSForests.py), execute

./automate.sh -st ds test_DSForests.py
./run_tests.sh -st ds test_DSForests.py

## References

Expand Down
16 changes: 8 additions & 8 deletions ands/algorithms/dp/longest_common_subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
given two strings x = x_1 x_2 .. x_m and y = y_1 y_2 .. y_n,
what is (the length of) the longest common subsequence between strings `x` and `y`,
where characters in the subsequences are not necessarily contiguous?
The solution is not necessarily unique.
You can find a recursive and a two dynamic programming implementations for the lcs problem.
You can find just one implementation using dynamic programming that actually returns the lcs,
Expand Down Expand Up @@ -95,7 +95,7 @@ def recursive_lcs_length(s1: str, s2: str) -> int:
### Goal
lcs(n, m), where n = length(x) and m = length(y).
### Algorithm
### Algorithm
If x(i) or y(j) is empty, lcs(i, j) = 0.
Expand Down Expand Up @@ -171,10 +171,10 @@ def memoized_recursive_lcs_length(s1: str, s2: str) -> int:

def bottom_up_lcs_length(s1: str, s2: str, matrix: bool=False):
"""Returns the length of the longest common subsequence between strings s1 and s2,
if `matrix` is set to `False`,
if `matrix` is set to `False`,
else it returns the matrix used to calculate the length of the lcs of sub-problems.
If n = length(s1) and m = length(s2),
If n = length(s1) and m = length(s2),
then the following are the asymptotic complexities of this algorithm.
**Time complexity:** O(n*m)
Expand All @@ -190,7 +190,7 @@ def bottom_up_lcs_length(s1: str, s2: str, matrix: bool=False):
# note that i and j start from 1,
# thus we index s1 and s2 using i - 1 and respectively j - 1,
# instead of simply i and j.
if s1[i - 1] == s2[j - 1]:
if s1[i - 1] == s2[j - 1]:
m[i][j] = m[i - 1][j - 1] + 1
else:
m[i][j] = max(m[i - 1][j], m[i][j - 1])
Expand All @@ -204,7 +204,7 @@ def bottom_up_lcs_length_partial(s1: str, s2: str, c1: str, c2: str, partial_wei
and thus instead of adding +1 to the length being computed `partial_weight` is added.
**Time complexity:** O(n*m)
**Space complexity:** O(n*m)
**Space complexity:** O(n*m)
"""

m = _get_lcs_length_matrix(s1, s2)
Expand All @@ -220,7 +220,7 @@ def bottom_up_lcs_length_partial(s1: str, s2: str, c1: str, c2: str, partial_wei
elif (s1[i - 1] == c1 and s2[j - 1] == c2) or (s1[i - 1] == c2 and s2[j - 1] == c1):
m[i][j] = max(m[i - 1][j], m[i][j - 1], m[i - 1][j - 1] + partial_weight)

else:
else:
m[i][j] = max(m[i - 1][j], m[i][j - 1])

return m[-1][-1] if not matrix else m
Expand Down Expand Up @@ -279,7 +279,7 @@ def bottom_up_lcs(s1: str, s2: str):
for a, b in examples:
print("a =", a, ", b =", b)
print(recursive_lcs_length(a, b))
print(bottom_up_lcs_length(a, b))
print(bottom_up_lcs_length(a, b))
print(memoized_recursive_lcs_length(a, b))
print(bottom_up_lcs_length_partial(a, b, 'a', 'e'))
print()
Expand Down
8 changes: 4 additions & 4 deletions ands/algorithms/dp/rod_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def memoized_rod_cut(prices, n):

# optimal first cut for rods of length 0..n
s = [0] * (n + 1)

return _memoized_rod_cut_aux(prices, n, revenues, s), s


Expand All @@ -139,7 +139,7 @@ def bottom_up_rod_cut(prices, n):
revenues = [-sys.maxsize] * (n + 1)
revenues[0] = 0 # Revenue for rod of length 0 is 0.

for i in range(1, n + 1):
for i in range(1, n + 1):
max_revenue = -sys.maxsize

for j in range(1, i + 1): # Find the max cut position for length i
Expand Down Expand Up @@ -194,7 +194,7 @@ def extended_bottom_up_rod_cut(prices, n):
else:
# revenue[i] (current) uses a rod of length j
# left most cut is at j
s[i] = [j]
s[i] = [j]

revenues[i] = max_revenue

Expand Down Expand Up @@ -222,7 +222,7 @@ def test0():
def test1():
r, s = memoized_rod_cut(p1, len(p1) - 1)
print("Revenue:", r)
print("s:", s)
print("s:", s)
rod_cut_solution_print(p1, len(p1) - 1, s)
print("--------------------------------------------")

Expand Down
2 changes: 1 addition & 1 deletion ands/ds/TST.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def count(self) -> int:
This method recursively passes through all the nodes
and counts the ones which have a non None value.
You should clearly use size instead:
You should clearly use size instead:
this method is here only for the fun of writing code!
**Time complexity:** O(n), where n is the number of nodes in this TST."""
Expand Down
145 changes: 0 additions & 145 deletions automate.sh

This file was deleted.

Loading

0 comments on commit aeb2e2f

Please sign in to comment.