diff --git a/graphs/dfs.py b/graphs/dfs.py deleted file mode 100644 index f183eae73fef..000000000000 --- a/graphs/dfs.py +++ /dev/null @@ -1,44 +0,0 @@ -"""pseudo-code""" - -""" -DFS(graph G, start vertex s): -// all nodes initially unexplored -mark s as explored -for every edge (s, v): - if v unexplored: - DFS(G, v) -""" - - -def dfs(graph, start): - """The DFS function simply calls itself recursively for every unvisited child of its argument. We can emulate that - behaviour precisely using a stack of iterators. Instead of recursively calling with a node, we'll push an iterator - to the node's children onto the iterator stack. When the iterator at the top of the stack terminates, we'll pop - it off the stack.""" - explored, stack = set(), [start] - while stack: - v = ( - stack.pop() - ) # one difference from BFS is to pop last element here instead of first one - - if v in explored: - continue - - explored.add(v) - - for w in graph[v]: - if w not in explored: - stack.append(w) - return explored - - -G = { - "A": ["B", "C"], - "B": ["A", "D", "E"], - "C": ["A", "F"], - "D": ["B"], - "E": ["B", "F"], - "F": ["C", "E"], -} - -print(dfs(G, "A")) diff --git a/maths/factorial_python.py b/maths/factorial_python.py index ab97cd41e681..278921de86f5 100644 --- a/maths/factorial_python.py +++ b/maths/factorial_python.py @@ -1,3 +1,4 @@ +import math def factorial(input_number: int) -> int: """ Non-recursive algorithm of finding factorial of the @@ -8,14 +9,12 @@ def factorial(input_number: int) -> int: 720 >>> factorial(0) 1 + >>> factorial(23) + 25852016738884976640000 """ if input_number < 0: raise ValueError("Input input_number should be non-negative") - elif input_number == 0: - return 1 - else: - result = 1 - for i in range(input_number): - result = result * (i + 1) - return result + + return math.factorial(input_number) + diff --git a/maths/find_max.py b/maths/find_max.py index 7cc82aacfb09..002828e7b106 100644 --- a/maths/find_max.py +++ b/maths/find_max.py @@ -2,11 +2,8 @@ def find_max(nums): - max = nums[0] - for x in nums: - if x > max: - max = x - print(max) + maxi=max(nums) + print(maxi) def main(): diff --git a/searches/quick_select.py b/searches/quick_select.py index 6b70562bd78f..08a3e8969c41 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -26,6 +26,16 @@ def _partition(data, pivot): def quickSelect(list, k): + """ + >>> quickSelect([2,4,5,7,899,54,32], 5) + 54 + >>> quickSelect([2,4,5,7,899,54,32], 1) + 4 + >>> quickSelect([5,4,3,2], 2) + 4 + >>> quickSelect([3,5,7,10,2,12],3) + 7 + """ # k = len(list) // 2 when trying to find the median (index that value would be when list is sorted) # invalid input