mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Tighten up psf/black and flake8 (#2024)
* Tighten up psf/black and flake8
* Fix some tests
* Fix some E741
* Fix some E741
* updating DIRECTORY.md
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -18,7 +18,7 @@ def valid_coloring(
|
||||
|
||||
>>> neighbours = [0,1,0,1,0]
|
||||
>>> colored_vertices = [0, 2, 1, 2, 0]
|
||||
|
||||
|
||||
>>> color = 1
|
||||
>>> valid_coloring(neighbours, colored_vertices, color)
|
||||
True
|
||||
@@ -37,11 +37,11 @@ def valid_coloring(
|
||||
def util_color(
|
||||
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
|
||||
) -> bool:
|
||||
"""
|
||||
"""
|
||||
Pseudo-Code
|
||||
|
||||
Base Case:
|
||||
1. Check if coloring is complete
|
||||
1. Check if coloring is complete
|
||||
1.1 If complete return True (meaning that we successfully colored graph)
|
||||
|
||||
Recursive Step:
|
||||
@@ -60,7 +60,7 @@ def util_color(
|
||||
>>> max_colors = 3
|
||||
>>> colored_vertices = [0, 1, 0, 0, 0]
|
||||
>>> index = 3
|
||||
|
||||
|
||||
>>> util_color(graph, max_colors, colored_vertices, index)
|
||||
True
|
||||
|
||||
@@ -87,11 +87,11 @@ def util_color(
|
||||
|
||||
|
||||
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.
|
||||
If True is returned colored_vertices list is filled with correct colorings
|
||||
|
||||
|
||||
>>> graph = [[0, 1, 0, 0, 0],
|
||||
... [1, 0, 1, 0, 1],
|
||||
... [0, 1, 0, 1, 0],
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
|
||||
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
|
||||
through a graph that visits each node exactly once.
|
||||
Determining whether such paths and cycles exist in graphs
|
||||
Determining whether such paths and cycles exist in graphs
|
||||
is the 'Hamiltonian path problem', which is NP-complete.
|
||||
|
||||
|
||||
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
|
||||
"""
|
||||
from typing import List
|
||||
@@ -18,7 +18,7 @@ def valid_connection(
|
||||
2. Next vertex should not be in path
|
||||
If both validations succeeds we return true saying that it is possible to connect this vertices
|
||||
either we return false
|
||||
|
||||
|
||||
Case 1:Use exact graph as in main function, with initialized values
|
||||
>>> graph = [[0, 1, 0, 1, 0],
|
||||
... [1, 0, 1, 1, 1],
|
||||
@@ -56,11 +56,11 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
|
||||
Recursive Step:
|
||||
2. Iterate over each vertex
|
||||
Check if next vertex is valid for transiting from current vertex
|
||||
2.1 Remember next vertex as next transition
|
||||
2.1 Remember next vertex as next transition
|
||||
2.2 Do recursive call and check if going to this vertex solves problem
|
||||
2.3 if next vertex leads to solution return True
|
||||
2.4 else backtrack, delete remembered vertex
|
||||
|
||||
|
||||
Case 1: Use exact graph as in main function, with initialized values
|
||||
>>> graph = [[0, 1, 0, 1, 0],
|
||||
... [1, 0, 1, 1, 1],
|
||||
@@ -111,12 +111,12 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
|
||||
Wrapper function to call subroutine called util_hamilton_cycle,
|
||||
which will either return array of vertices indicating hamiltonian cycle
|
||||
or an empty list indicating that hamiltonian cycle was not found.
|
||||
Case 1:
|
||||
Following graph consists of 5 edges.
|
||||
Case 1:
|
||||
Following graph consists of 5 edges.
|
||||
If we look closely, we can see that there are multiple Hamiltonian cycles.
|
||||
For example one result is when we iterate like:
|
||||
For example one result is when we iterate like:
|
||||
(0)->(1)->(2)->(4)->(3)->(0)
|
||||
|
||||
|
||||
(0)---(1)---(2)
|
||||
| / \ |
|
||||
| / \ |
|
||||
@@ -130,10 +130,10 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
|
||||
... [0, 1, 1, 1, 0]]
|
||||
>>> hamilton_cycle(graph)
|
||||
[0, 1, 2, 4, 3, 0]
|
||||
|
||||
Case 2:
|
||||
|
||||
Case 2:
|
||||
Same Graph as it was in Case 1, changed starting index from default to 3
|
||||
|
||||
|
||||
(0)---(1)---(2)
|
||||
| / \ |
|
||||
| / \ |
|
||||
@@ -147,11 +147,11 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
|
||||
... [0, 1, 1, 1, 0]]
|
||||
>>> hamilton_cycle(graph, 3)
|
||||
[3, 0, 1, 2, 4, 3]
|
||||
|
||||
|
||||
Case 3:
|
||||
Following Graph is exactly what it was before, but edge 3-4 is removed.
|
||||
Result is that there is no Hamiltonian Cycle anymore.
|
||||
|
||||
|
||||
(0)---(1)---(2)
|
||||
| / \ |
|
||||
| / \ |
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import math
|
||||
|
||||
""" Minimax helps to achieve maximum score in a game by checking all possible moves
|
||||
depth is current depth in game tree.
|
||||
depth is current depth in game tree.
|
||||
nodeIndex is index of current node in scores[].
|
||||
if move is of maximizer return true else false
|
||||
leaves of game tree is stored in scores[]
|
||||
leaves of game tree is stored in scores[]
|
||||
height is maximum height of Game tree
|
||||
"""
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
|
||||
The nqueens problem is of placing N queens on a N * N
|
||||
The nqueens problem is of placing N queens on a N * N
|
||||
chess board such that no queen can attack any other queens placed
|
||||
on that chess board.
|
||||
This means that one queen cannot have any other queen on its horizontal, vertical and
|
||||
This means that one queen cannot have any other queen on its horizontal, vertical and
|
||||
diagonal lines.
|
||||
|
||||
"""
|
||||
@@ -12,7 +12,7 @@ solution = []
|
||||
|
||||
def isSafe(board, row, column):
|
||||
"""
|
||||
This function returns a boolean value True if it is safe to place a queen there considering
|
||||
This function returns a boolean value True if it is safe to place a queen there considering
|
||||
the current state of the board.
|
||||
|
||||
Parameters :
|
||||
@@ -40,13 +40,13 @@ def isSafe(board, row, column):
|
||||
|
||||
def solve(board, row):
|
||||
"""
|
||||
It creates a state space tree and calls the safe function until it receives a
|
||||
False Boolean and terminates that branch and backtracks to the next
|
||||
It creates a state space tree and calls the safe function until it receives a
|
||||
False Boolean and terminates that branch and backtracks to the next
|
||||
possible solution branch.
|
||||
"""
|
||||
if row >= len(board):
|
||||
"""
|
||||
If the row number exceeds N we have board with a successful combination
|
||||
If the row number exceeds N we have board with a successful combination
|
||||
and that combination is appended to the solution list and the board is printed.
|
||||
|
||||
"""
|
||||
@@ -56,9 +56,9 @@ def solve(board, row):
|
||||
return
|
||||
for i in range(len(board)):
|
||||
"""
|
||||
For every row it iterates through each column to check if it is feasible to place a
|
||||
For every row it iterates through each column to check if it is feasible to place a
|
||||
queen there.
|
||||
If all the combinations for that particular branch are successful the board is
|
||||
If all the combinations for that particular branch are successful the board is
|
||||
reinitialized for the next possible combination.
|
||||
"""
|
||||
if isSafe(board, row, i):
|
||||
|
||||
Reference in New Issue
Block a user