From f5341774382071179d41a18831b9cf28d364bab2 Mon Sep 17 00:00:00 2001 From: percy07 <56677891+percy07@users.noreply.github.com> Date: Fri, 25 Oct 2019 01:11:51 +0530 Subject: [PATCH 1/6] Update find_max.py optimized the code for find_max. --- maths/find_max.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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(): From d05e4225763bae8fc3bc294194fb3887686f60ea Mon Sep 17 00:00:00 2001 From: percy07 <56677891+percy07@users.noreply.github.com> Date: Fri, 25 Oct 2019 01:36:28 +0530 Subject: [PATCH 2/6] Update dfs.py Optimized the DFS Algorithm.It is also working for Disconnected Graph. --- graphs/dfs.py | 104 +++++++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 44 deletions(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index f183eae73fef..e0d547822fca 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -1,44 +1,60 @@ -"""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")) +# Python program to print DFS traversal for complete graph +from collections import defaultdict + +# This class represents a directed graph using adjacency +# list representation +class Graph: + + # Constructor + def __init__(self): + + # default dictionary to store graph + self.graph = defaultdict(list) + + # function to add an edge to graph + def addEdge(self,u,v): + self.graph[u].append(v) + + # A function used by DFS + def DFSUtil(self, v, visited): + + # Mark the current node as visited and print it + visited[v]= True + print(v) + + + # Recur for all the vertices adjacent to + # this vertex + for i in self.graph[v]: + if visited[i] == False: + self.DFSUtil(i, visited) + + + # The function to do DFS traversal. It uses + # recursive DFSUtil() + def DFS(self): + V = len(self.graph) #total vertices + + # Mark all the vertices as not visited + visited =[False]*(V) + + # Call the recursive helper function to print + # DFS traversal starting from all vertices one + # by one + for i in range(V): + if visited[i] == False: + self.DFSUtil(i, visited) + + +# Driver code +# Create a graph given in the above diagram +g = Graph() +g.addEdge(0, 1) +g.addEdge(0, 2) +g.addEdge(1, 2) +g.addEdge(2, 0) +g.addEdge(2, 3) +g.addEdge(3, 3) + +print("Following is Depth First Traversal") +g.DFS() From 86222ea0a7fa0991087563eb855c7f067aae6168 Mon Sep 17 00:00:00 2001 From: percy07 <56677891+percy07@users.noreply.github.com> Date: Fri, 25 Oct 2019 10:56:31 +0530 Subject: [PATCH 3/6] Update factorial_python.py --- maths/factorial_python.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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) + From 246eecb1e2d6f5fc308cdae979418a00f45c3c91 Mon Sep 17 00:00:00 2001 From: percy07 <56677891+percy07@users.noreply.github.com> Date: Fri, 25 Oct 2019 11:00:05 +0530 Subject: [PATCH 4/6] Update factorial_python.py From 885039bf976d675ca0c3c2dd8d5acfadddfcd6b9 Mon Sep 17 00:00:00 2001 From: percy07 <56677891+percy07@users.noreply.github.com> Date: Fri, 25 Oct 2019 14:31:20 +0530 Subject: [PATCH 5/6] Delete dfs.py --- graphs/dfs.py | 60 --------------------------------------------------- 1 file changed, 60 deletions(-) delete mode 100644 graphs/dfs.py diff --git a/graphs/dfs.py b/graphs/dfs.py deleted file mode 100644 index e0d547822fca..000000000000 --- a/graphs/dfs.py +++ /dev/null @@ -1,60 +0,0 @@ -# Python program to print DFS traversal for complete graph -from collections import defaultdict - -# This class represents a directed graph using adjacency -# list representation -class Graph: - - # Constructor - def __init__(self): - - # default dictionary to store graph - self.graph = defaultdict(list) - - # function to add an edge to graph - def addEdge(self,u,v): - self.graph[u].append(v) - - # A function used by DFS - def DFSUtil(self, v, visited): - - # Mark the current node as visited and print it - visited[v]= True - print(v) - - - # Recur for all the vertices adjacent to - # this vertex - for i in self.graph[v]: - if visited[i] == False: - self.DFSUtil(i, visited) - - - # The function to do DFS traversal. It uses - # recursive DFSUtil() - def DFS(self): - V = len(self.graph) #total vertices - - # Mark all the vertices as not visited - visited =[False]*(V) - - # Call the recursive helper function to print - # DFS traversal starting from all vertices one - # by one - for i in range(V): - if visited[i] == False: - self.DFSUtil(i, visited) - - -# Driver code -# Create a graph given in the above diagram -g = Graph() -g.addEdge(0, 1) -g.addEdge(0, 2) -g.addEdge(1, 2) -g.addEdge(2, 0) -g.addEdge(2, 3) -g.addEdge(3, 3) - -print("Following is Depth First Traversal") -g.DFS() From 81f4caf7bb256f027bceb221c565d1c8f3581560 Mon Sep 17 00:00:00 2001 From: percy07 <56677891+percy07@users.noreply.github.com> Date: Wed, 30 Oct 2019 14:52:18 +0530 Subject: [PATCH 6/6] Update quick_select.py Add Doctests. --- searches/quick_select.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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