Skip to content

Commit bef8610

Browse files
committed
Merge branch 'master' of https://github.com/niranjanhegde144/Python-1 into web_programming_contribution
2 parents 10c7f4f + 72fe611 commit bef8610

File tree

107 files changed

+3017
-501
lines changed

Some content is hidden

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

107 files changed

+3017
-501
lines changed

Diff for: .travis.yml

+17-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ python: 3.8
55
cache: pip
66
before_install: pip install --upgrade pip setuptools six
77
install: pip install black flake8
8+
jobs:
9+
include:
10+
- name: Build
11+
before_script:
12+
- black --check . || true
13+
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=88 --statistics --count .
14+
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
15+
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
16+
script:
17+
- mypy --ignore-missing-imports . || true # https://github.com/python/mypy/issues/7907
18+
- pytest --doctest-modules --ignore=project_euler/ --durations=10 --cov-report=term-missing:skip-covered --cov=. .
19+
- name: Project Euler
20+
before_script: pip install pytest-cov
21+
script:
22+
- pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/
23+
after_success:
24+
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
825
notifications:
926
webhooks: https://www.travisbuddy.com/
1027
on_success: never
11-
before_script:
12-
- black --check . || true
13-
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=88 --statistics --count .
14-
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
15-
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
16-
script:
17-
- mypy --ignore-missing-imports .
18-
- pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=. .
19-
after_success:
20-
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md

Diff for: DIRECTORY.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
* [Sum Of Subsets](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sum_of_subsets.py)
2626

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

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

8789
## Conversions
8890
* [Binary To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_decimal.py)
91+
* [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py)
8992
* [Decimal To Any](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_any.py)
9093
* [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py)
9194
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py)
@@ -131,6 +134,7 @@
131134
* [Deque Doubly](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/deque_doubly.py)
132135
* [Doubly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/doubly_linked_list.py)
133136
* [From Sequence](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/from_sequence.py)
137+
* [Has Loop](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/has_loop.py)
134138
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/is_palindrome.py)
135139
* [Middle Element Of Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/middle_element_of_linked_list.py)
136140
* [Print Reverse](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/print_reverse.py)
@@ -141,6 +145,7 @@
141145
* [Circular Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/circular_queue.py)
142146
* [Double Ended Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/double_ended_queue.py)
143147
* [Linked Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/linked_queue.py)
148+
* [Priority Queue Using List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/priority_queue_using_list.py)
144149
* [Queue On List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_list.py)
145150
* [Queue On Pseudo Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_pseudo_stack.py)
146151
* Stacks
@@ -303,7 +308,7 @@
303308
## Linear Algebra
304309
* Src
305310
* [Lib](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/lib.py)
306-
* [Polynom-For-Points](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/polynom-for-points.py)
311+
* [Polynom For Points](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/polynom_for_points.py)
307312
* [Power Iteration](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/power_iteration.py)
308313
* [Rayleigh Quotient](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/rayleigh_quotient.py)
309314
* [Test Linear Algebra](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/test_linear_algebra.py)
@@ -518,6 +523,7 @@
518523
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol2.py)
519524
* [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol3.py)
520525
* [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol4.py)
526+
* [Test Solutions](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/test_solutions.py)
521527
* Problem 07
522528
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol1.py)
523529
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol2.py)
@@ -625,6 +631,9 @@
625631
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_52/sol1.py)
626632
* Problem 53
627633
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_53/sol1.py)
634+
* Problem 54
635+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_54/sol1.py)
636+
* [Test Poker Hand](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_54/test_poker_hand.py)
628637
* Problem 55
629638
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_55/sol1.py)
630639
* Problem 551
@@ -648,14 +657,15 @@
648657
## Searches
649658
* [Binary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/binary_search.py)
650659
* [Double Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/double_linear_search.py)
660+
* [Double Linear Search Recursion](https://github.com/TheAlgorithms/Python/blob/master/searches/double_linear_search_recursion.py)
651661
* [Fibonacci Search](https://github.com/TheAlgorithms/Python/blob/master/searches/fibonacci_search.py)
652662
* [Hill Climbing](https://github.com/TheAlgorithms/Python/blob/master/searches/hill_climbing.py)
653663
* [Interpolation Search](https://github.com/TheAlgorithms/Python/blob/master/searches/interpolation_search.py)
654664
* [Jump Search](https://github.com/TheAlgorithms/Python/blob/master/searches/jump_search.py)
655665
* [Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/linear_search.py)
656666
* [Quick Select](https://github.com/TheAlgorithms/Python/blob/master/searches/quick_select.py)
657667
* [Sentinel Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/sentinel_linear_search.py)
658-
* [Simple-Binary-Search](https://github.com/TheAlgorithms/Python/blob/master/searches/simple-binary-search.py)
668+
* [Simple Binary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/simple_binary_search.py)
659669
* [Simulated Annealing](https://github.com/TheAlgorithms/Python/blob/master/searches/simulated_annealing.py)
660670
* [Tabu Search](https://github.com/TheAlgorithms/Python/blob/master/searches/tabu_search.py)
661671
* [Ternary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/ternary_search.py)
@@ -689,9 +699,9 @@
689699
* [Radix Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/radix_sort.py)
690700
* [Random Normal Distribution Quicksort](https://github.com/TheAlgorithms/Python/blob/master/sorts/random_normal_distribution_quicksort.py)
691701
* [Random Pivot Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/random_pivot_quick_sort.py)
692-
* [Recursive-Quick-Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive-quick-sort.py)
693702
* [Recursive Bubble Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_bubble_sort.py)
694703
* [Recursive Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_insertion_sort.py)
704+
* [Recursive Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_quick_sort.py)
695705
* [Selection Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py)
696706
* [Shell Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py)
697707
* [Stooge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/stooge_sort.py)
@@ -703,8 +713,9 @@
703713
* [Wiggle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/wiggle_sort.py)
704714

705715
## Strings
706-
* [Aho-Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho-corasick.py)
716+
* [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py)
707717
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
718+
* [Can String Be Rearranged As Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/can_string_be_rearranged_as_palindrome.py)
708719
* [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py)
709720
* [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py)
710721
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)

Diff for: arithmetic_analysis/in_static_equilibrium.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
mypy : passed
77
"""
88

9-
from typing import List
9+
from __future__ import annotations
1010

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

1313

1414
def polar_force(
1515
magnitude: float, angle: float, radian_mode: bool = False
16-
) -> List[float]:
16+
) -> list[float]:
1717
"""
1818
Resolves force along rectangular components.
1919
(force, angle) => (force_x, force_y)

Diff for: backtracking/coloring.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
66
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
77
"""
8-
from typing import List
8+
from __future__ import annotations
99

1010

1111
def valid_coloring(
12-
neighbours: List[int], colored_vertices: List[int], color: int
12+
neighbours: list[int], colored_vertices: list[int], color: int
1313
) -> bool:
1414
"""
1515
For each neighbour check if coloring constraint is satisfied
@@ -35,7 +35,7 @@ def valid_coloring(
3535

3636

3737
def util_color(
38-
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
38+
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
3939
) -> bool:
4040
"""
4141
Pseudo-Code
@@ -86,7 +86,7 @@ def util_color(
8686
return False
8787

8888

89-
def color(graph: List[List[int]], max_colors: int) -> List[int]:
89+
def color(graph: list[list[int]], max_colors: int) -> list[int]:
9090
"""
9191
Wrapper function to call subroutine called util_color
9292
which will either return True or False.

Diff for: backtracking/hamiltonian_cycle.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
77
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
88
"""
9-
from typing import List
9+
from __future__ import annotations
1010

1111

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

4949

50-
def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) -> bool:
50+
def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) -> bool:
5151
"""
5252
Pseudo-Code
5353
Base Case:
@@ -108,7 +108,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
108108
return False
109109

110110

111-
def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
111+
def hamilton_cycle(graph: list[list[int]], start_index: int = 0) -> list[int]:
112112
r"""
113113
Wrapper function to call subroutine called util_hamilton_cycle,
114114
which will either return array of vertices indicating hamiltonian cycle

Diff for: backtracking/knight_tour.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Knight Tour Intro: https://www.youtube.com/watch?v=ab_dY3dZFHM
22

3-
from typing import List, Tuple
3+
from __future__ import annotations
44

55

6-
def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
6+
def get_valid_pos(position: tuple[int], n: int) -> list[tuple[int]]:
77
"""
88
Find all the valid positions a knight can move to from the current position.
99
@@ -32,7 +32,7 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
3232
return permissible_positions
3333

3434

35-
def is_complete(board: List[List[int]]) -> bool:
35+
def is_complete(board: list[list[int]]) -> bool:
3636
"""
3737
Check if the board (matrix) has been completely filled with non-zero values.
3838
@@ -46,7 +46,7 @@ def is_complete(board: List[List[int]]) -> bool:
4646
return not any(elem == 0 for row in board for elem in row)
4747

4848

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

6868

69-
def open_knight_tour(n: int) -> List[List[int]]:
69+
def open_knight_tour(n: int) -> list[list[int]]:
7070
"""
7171
Find the solution for the knight tour problem for a board of size n. Raises
7272
ValueError if the tour cannot be performed for the given size.

Diff for: backtracking/minimax.py

+50-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import math
24

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

1113

12-
def minimax(Depth, nodeIndex, isMax, scores, height):
14+
def minimax(
15+
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
16+
) -> int:
17+
"""
18+
>>> import math
19+
>>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]
20+
>>> height = math.log(len(scores), 2)
21+
>>> minimax(0, 0, True, scores, height)
22+
65
23+
>>> minimax(-1, 0, True, scores, height)
24+
Traceback (most recent call last):
25+
...
26+
ValueError: Depth cannot be less than 0
27+
>>> minimax(0, 0, True, [], 2)
28+
Traceback (most recent call last):
29+
...
30+
ValueError: Scores cannot be empty
31+
>>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
32+
>>> height = math.log(len(scores), 2)
33+
>>> minimax(0, 0, True, scores, height)
34+
12
35+
>>> minimax('1', 2, True, [], 2 )
36+
Traceback (most recent call last):
37+
...
38+
TypeError: '<' not supported between instances of 'str' and 'int'
39+
"""
40+
41+
if depth < 0:
42+
raise ValueError("Depth cannot be less than 0")
43+
44+
if len(scores) == 0:
45+
raise ValueError("Scores cannot be empty")
1346

14-
if Depth == height:
15-
return scores[nodeIndex]
47+
if depth == height:
48+
return scores[node_index]
1649

17-
if isMax:
50+
if is_max:
1851
return max(
19-
minimax(Depth + 1, nodeIndex * 2, False, scores, height),
20-
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height),
52+
minimax(depth + 1, node_index * 2, False, scores, height),
53+
minimax(depth + 1, node_index * 2 + 1, False, scores, height),
2154
)
55+
2256
return min(
23-
minimax(Depth + 1, nodeIndex * 2, True, scores, height),
24-
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height),
57+
minimax(depth + 1, node_index * 2, True, scores, height),
58+
minimax(depth + 1, node_index * 2 + 1, True, scores, height),
2559
)
2660

2761

28-
if __name__ == "__main__":
29-
62+
def main():
3063
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
3164
height = math.log(len(scores), 2)
32-
3365
print("Optimal value : ", end="")
3466
print(minimax(0, 0, True, scores, height))
67+
68+
69+
if __name__ == "__main__":
70+
import doctest
71+
72+
doctest.testmod()
73+
main()

Diff for: backtracking/n_queens_math.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@
7575
for another one or vice versa.
7676
7777
"""
78-
from typing import List
78+
from __future__ import annotations
7979

8080

8181
def depth_first_search(
82-
possible_board: List[int],
83-
diagonal_right_collisions: List[int],
84-
diagonal_left_collisions: List[int],
85-
boards: List[List[str]],
82+
possible_board: list[int],
83+
diagonal_right_collisions: list[int],
84+
diagonal_left_collisions: list[int],
85+
boards: list[list[str]],
8686
n: int,
8787
) -> None:
8888
"""

0 commit comments

Comments
 (0)