Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
363858e
hyphen_files = [file for file in filepaths if "-" in file] (#2447)
cclauss Sep 19, 2020
697495b
Fix copy / paste oversight (#2448)
cclauss Sep 19, 2020
b22596c
bin_to_octal (#2431)
mohammadreza490 Sep 19, 2020
b05081a
Update and rename bin_to_octal.py to binary_to_octal.py (#2449)
cclauss Sep 19, 2020
9b73884
Added a function that checks if given string can be rearranged to for…
susmith98 Sep 19, 2020
ea0759d
Create problem_54 in project Euler (#2451)
dhruvmanila Sep 20, 2020
718be54
Update sol1.py (#2455)
atarax665 Sep 20, 2020
a1ea76b
Optimization problem_10 in project_euler (#2453)
realDuYuanChao Sep 22, 2020
6e6a49d
Config Travis CI for two jobs (#2463)
dhruvmanila Sep 23, 2020
9200a2e
from __future__ import annotations (#2464)
cclauss Sep 23, 2020
5f9be0a
Add Python type hints and doctests to other/two_sum.py (#2467)
spamegg1 Sep 23, 2020
4a3b8d6
Added binary_xor_operator.py and binary_and_operator.py (#2433)
Sep 24, 2020
902fe1c
Fixed reverse words algorithm (#2469)
realDuYuanChao Sep 24, 2020
3a275ca
Fixed remove duplicate (#2470)
realDuYuanChao Sep 24, 2020
08eb1ef
Add solution() for problem 54 of Project Euler (#2472)
dhruvmanila Sep 24, 2020
f564c9d
Wiggle sort (#2419)
grochedix Sep 25, 2020
daa1b4d
Created problem_97 in project euler (#2476)
Kush1101 Sep 25, 2020
53c2a24
added type hints and doctests for minimax algorithm (#2478)
Abdujabbar Sep 25, 2020
a196a36
Fixed bugs (#2474)
realDuYuanChao Sep 25, 2020
18f1dcd
Updated singly_linked_list (#2477)
realDuYuanChao Sep 25, 2020
b81fcef
Fixed linked list bug (#2481)
realDuYuanChao Sep 25, 2020
e92cd9d
Update morse_code_implementation.py (#2386)
Mango0x45 Sep 25, 2020
72fe611
Updated lower and upper (#2468)
realDuYuanChao Sep 25, 2020
1816dab
Use self-documenting option instead of cryptic option
cclauss Sep 26, 2020
96fd002
updating DIRECTORY.md
Sep 26, 2020
4f28f43
Blame algorithm in Python
hackprot1 Oct 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- run: pip install codespell flake8
- run: |
SKIP="./.*,./other/dictionary.txt,./other/words,./project_euler/problem_22/p022_names.txt"
codespell -L ans,fo,hist,iff,secant,tim --skip=$SKIP --quiet-level=2
codespell --ignore-words-list=ans,fo,hist,iff,secant,som,tim --skip=$SKIP --quiet-level=2
- name: Codespell comment
if: ${{ failure() }}
uses: plettich/python_codespell_action@master
27 changes: 17 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ python: 3.8
cache: pip
before_install: pip install --upgrade pip setuptools six
install: pip install black flake8
jobs:
include:
- name: Build
before_script:
- black --check . || true
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=88 --statistics --count .
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
script:
- mypy --ignore-missing-imports . || true # https://github.com/python/mypy/issues/7907
- pytest --doctest-modules --ignore=project_euler/ --durations=10 --cov-report=term-missing:skip-covered --cov=. .
- name: Project Euler
before_script: pip install pytest-cov
script:
- pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/
after_success:
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
notifications:
webhooks: https://www.travisbuddy.com/
on_success: never
before_script:
- black --check . || true
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=88 --statistics --count .
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
script:
- mypy --ignore-missing-imports .
- pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=. .
after_success:
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
21 changes: 17 additions & 4 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
* [Sum Of Subsets](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sum_of_subsets.py)

## Bit Manipulation
* [Binary And Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_and_operator.py)
* [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py)
* [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py)

## Blockchain
* [Chinese Remainder Theorem](https://github.com/TheAlgorithms/Python/blob/master/blockchain/chinese_remainder_theorem.py)
Expand Down Expand Up @@ -86,6 +88,7 @@

## Conversions
* [Binary To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_decimal.py)
* [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py)
* [Decimal To Any](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_any.py)
* [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py)
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py)
Expand Down Expand Up @@ -131,6 +134,7 @@
* [Deque Doubly](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/deque_doubly.py)
* [Doubly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/doubly_linked_list.py)
* [From Sequence](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/from_sequence.py)
* [Has Loop](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/has_loop.py)
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/is_palindrome.py)
* [Middle Element Of Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/middle_element_of_linked_list.py)
* [Print Reverse](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/print_reverse.py)
Expand All @@ -141,6 +145,7 @@
* [Circular Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/circular_queue.py)
* [Double Ended Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/double_ended_queue.py)
* [Linked Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/linked_queue.py)
* [Priority Queue Using List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/priority_queue_using_list.py)
* [Queue On List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_list.py)
* [Queue On Pseudo Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_pseudo_stack.py)
* Stacks
Expand Down Expand Up @@ -303,7 +308,7 @@
## Linear Algebra
* Src
* [Lib](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/lib.py)
* [Polynom-For-Points](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/polynom-for-points.py)
* [Polynom For Points](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/polynom_for_points.py)
* [Power Iteration](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/power_iteration.py)
* [Rayleigh Quotient](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/rayleigh_quotient.py)
* [Test Linear Algebra](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/test_linear_algebra.py)
Expand Down Expand Up @@ -518,6 +523,7 @@
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol2.py)
* [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol3.py)
* [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol4.py)
* [Test Solutions](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/test_solutions.py)
* Problem 07
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol1.py)
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol2.py)
Expand Down Expand Up @@ -625,6 +631,9 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_52/sol1.py)
* Problem 53
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_53/sol1.py)
* Problem 54
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_54/sol1.py)
* [Test Poker Hand](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_54/test_poker_hand.py)
* Problem 55
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_55/sol1.py)
* Problem 551
Expand All @@ -637,6 +646,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py)
* Problem 76
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py)
* Problem 97
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_97/sol1.py)
* Problem 99
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_99/sol1.py)

Expand All @@ -648,14 +659,15 @@
## Searches
* [Binary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/binary_search.py)
* [Double Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/double_linear_search.py)
* [Double Linear Search Recursion](https://github.com/TheAlgorithms/Python/blob/master/searches/double_linear_search_recursion.py)
* [Fibonacci Search](https://github.com/TheAlgorithms/Python/blob/master/searches/fibonacci_search.py)
* [Hill Climbing](https://github.com/TheAlgorithms/Python/blob/master/searches/hill_climbing.py)
* [Interpolation Search](https://github.com/TheAlgorithms/Python/blob/master/searches/interpolation_search.py)
* [Jump Search](https://github.com/TheAlgorithms/Python/blob/master/searches/jump_search.py)
* [Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/linear_search.py)
* [Quick Select](https://github.com/TheAlgorithms/Python/blob/master/searches/quick_select.py)
* [Sentinel Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/sentinel_linear_search.py)
* [Simple-Binary-Search](https://github.com/TheAlgorithms/Python/blob/master/searches/simple-binary-search.py)
* [Simple Binary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/simple_binary_search.py)
* [Simulated Annealing](https://github.com/TheAlgorithms/Python/blob/master/searches/simulated_annealing.py)
* [Tabu Search](https://github.com/TheAlgorithms/Python/blob/master/searches/tabu_search.py)
* [Ternary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/ternary_search.py)
Expand Down Expand Up @@ -689,9 +701,9 @@
* [Radix Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/radix_sort.py)
* [Random Normal Distribution Quicksort](https://github.com/TheAlgorithms/Python/blob/master/sorts/random_normal_distribution_quicksort.py)
* [Random Pivot Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/random_pivot_quick_sort.py)
* [Recursive-Quick-Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive-quick-sort.py)
* [Recursive Bubble Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_bubble_sort.py)
* [Recursive Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_insertion_sort.py)
* [Recursive Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_quick_sort.py)
* [Selection Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py)
* [Shell Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py)
* [Stooge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/stooge_sort.py)
Expand All @@ -703,8 +715,9 @@
* [Wiggle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/wiggle_sort.py)

## Strings
* [Aho-Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho-corasick.py)
* [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py)
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
* [Can String Be Rearranged As Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/can_string_be_rearranged_as_palindrome.py)
* [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py)
* [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py)
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)
Expand Down
4 changes: 2 additions & 2 deletions arithmetic_analysis/in_static_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
mypy : passed
"""

from typing import List
from __future__ import annotations

from numpy import array, cos, cross, radians, sin # type: ignore


def polar_force(
magnitude: float, angle: float, radian_mode: bool = False
) -> List[float]:
) -> list[float]:
"""
Resolves force along rectangular components.
(force, angle) => (force_x, force_y)
Expand Down
8 changes: 4 additions & 4 deletions backtracking/coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
"""
from typing import List
from __future__ import annotations


def valid_coloring(
neighbours: List[int], colored_vertices: List[int], color: int
neighbours: list[int], colored_vertices: list[int], color: int
) -> bool:
"""
For each neighbour check if coloring constraint is satisfied
Expand All @@ -35,7 +35,7 @@ def valid_coloring(


def util_color(
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
) -> bool:
"""
Pseudo-Code
Expand Down Expand Up @@ -86,7 +86,7 @@ def util_color(
return False


def color(graph: List[List[int]], max_colors: int) -> List[int]:
def color(graph: list[list[int]], max_colors: int) -> list[int]:
"""
Wrapper function to call subroutine called util_color
which will either return True or False.
Expand Down
8 changes: 4 additions & 4 deletions backtracking/hamiltonian_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
"""
from typing import List
from __future__ import annotations


def valid_connection(
graph: List[List[int]], next_ver: int, curr_ind: int, path: List[int]
graph: list[list[int]], next_ver: int, curr_ind: int, path: list[int]
) -> bool:
"""
Checks whether it is possible to add next into path by validating 2 statements
Expand Down Expand Up @@ -47,7 +47,7 @@ def valid_connection(
return not any(vertex == next_ver for vertex in path)


def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) -> bool:
def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) -> bool:
"""
Pseudo-Code
Base Case:
Expand Down Expand Up @@ -108,7 +108,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
return False


def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
def hamilton_cycle(graph: list[list[int]], start_index: int = 0) -> list[int]:
r"""
Wrapper function to call subroutine called util_hamilton_cycle,
which will either return array of vertices indicating hamiltonian cycle
Expand Down
10 changes: 5 additions & 5 deletions backtracking/knight_tour.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Knight Tour Intro: https://www.youtube.com/watch?v=ab_dY3dZFHM

from typing import List, Tuple
from __future__ import annotations


def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
def get_valid_pos(position: tuple[int], n: int) -> list[tuple[int]]:
"""
Find all the valid positions a knight can move to from the current position.

Expand Down Expand Up @@ -32,7 +32,7 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
return permissible_positions


def is_complete(board: List[List[int]]) -> bool:
def is_complete(board: list[list[int]]) -> bool:
"""
Check if the board (matrix) has been completely filled with non-zero values.

Expand All @@ -46,7 +46,7 @@ def is_complete(board: List[List[int]]) -> bool:
return not any(elem == 0 for row in board for elem in row)


def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int) -> bool:
def open_knight_tour_helper(board: list[list[int]], pos: tuple[int], curr: int) -> bool:
"""
Helper function to solve knight tour problem.
"""
Expand All @@ -66,7 +66,7 @@ def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int)
return False


def open_knight_tour(n: int) -> List[List[int]]:
def open_knight_tour(n: int) -> list[list[int]]:
"""
Find the solution for the knight tour problem for a board of size n. Raises
ValueError if the tour cannot be performed for the given size.
Expand Down
61 changes: 50 additions & 11 deletions backtracking/minimax.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import math

""" Minimax helps to achieve maximum score in a game by checking all possible moves
Expand All @@ -9,26 +11,63 @@
"""


def minimax(Depth, nodeIndex, isMax, scores, height):
def minimax(
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
) -> int:
"""
>>> import math
>>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]
>>> height = math.log(len(scores), 2)
>>> minimax(0, 0, True, scores, height)
65
>>> minimax(-1, 0, True, scores, height)
Traceback (most recent call last):
...
ValueError: Depth cannot be less than 0
>>> minimax(0, 0, True, [], 2)
Traceback (most recent call last):
...
ValueError: Scores cannot be empty
>>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
>>> height = math.log(len(scores), 2)
>>> minimax(0, 0, True, scores, height)
12
>>> minimax('1', 2, True, [], 2 )
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
"""

if depth < 0:
raise ValueError("Depth cannot be less than 0")

if len(scores) == 0:
raise ValueError("Scores cannot be empty")

if Depth == height:
return scores[nodeIndex]
if depth == height:
return scores[node_index]

if isMax:
if is_max:
return max(
minimax(Depth + 1, nodeIndex * 2, False, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height),
minimax(depth + 1, node_index * 2, False, scores, height),
minimax(depth + 1, node_index * 2 + 1, False, scores, height),
)

return min(
minimax(Depth + 1, nodeIndex * 2, True, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height),
minimax(depth + 1, node_index * 2, True, scores, height),
minimax(depth + 1, node_index * 2 + 1, True, scores, height),
)


if __name__ == "__main__":

def main():
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
height = math.log(len(scores), 2)

print("Optimal value : ", end="")
print(minimax(0, 0, True, scores, height))


if __name__ == "__main__":
import doctest

doctest.testmod()
main()
10 changes: 5 additions & 5 deletions backtracking/n_queens_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@
for another one or vice versa.

"""
from typing import List
from __future__ import annotations


def depth_first_search(
possible_board: List[int],
diagonal_right_collisions: List[int],
diagonal_left_collisions: List[int],
boards: List[List[str]],
possible_board: list[int],
diagonal_right_collisions: list[int],
diagonal_left_collisions: list[int],
boards: list[list[str]],
n: int,
) -> None:
"""
Expand Down
Loading