Loading...
More Python Posts
# Python code to find the URL from an input string# Using the regular expressionimport redef Find(string):# findall() has been used# with valid conditions for urls in stringregex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"url = re.findall(regex,string)return [x[0] for x in url]# Driver Codestring = 'My Profile: https://codecatch.net'print("Urls: ", Find(string))
def Fibonacci(n):if n<0:print("Incorrect input")# First Fibonacci number is 0elif n==1:return 0# Second Fibonacci number is 1elif n==2:return 1else:return Fibonacci(n-1)+Fibonacci(n-2)# Driver Programprint(Fibonacci(9))
import itertoolsdef compute_permutations(string):# Generate all permutations of the stringpermutations = itertools.permutations(string)# Convert each permutation tuple to a stringpermutations = [''.join(permutation) for permutation in permutations]return permutations# Prompt the user for a stringstring = input("Enter a string: ")# Compute permutationspermutations = compute_permutations(string)# Display the permutationsprint("Permutations:")for permutation in permutations:print(permutation)
class Rectangle:passclass Square(Rectangle):passrectangle = Rectangle()square = Square()print(isinstance(rectangle, Rectangle)) # Trueprint(isinstance(square, Rectangle)) # Trueprint(isinstance(square, Square)) # Trueprint(isinstance(rectangle, Square)) # False
#Python program to print topological sorting of a DAGfrom collections import defaultdict#Class to represent a graphclass Graph:def __init__(self,vertices):self.graph = defaultdict(list) #dictionary containing adjacency Listself.V = vertices #No. of vertices# function to add an edge to graphdef addEdge(self,u,v):self.graph[u].append(v)# A recursive function used by topologicalSortdef topologicalSortUtil(self,v,visited,stack):# Mark the current node as visited.visited[v] = True# Recur for all the vertices adjacent to this vertexfor i in self.graph[v]:if visited[i] == False:self.topologicalSortUtil(i,visited,stack)# Push current vertex to stack which stores resultstack.insert(0,v)# The function to do Topological Sort. It uses recursive# topologicalSortUtil()def topologicalSort(self):# Mark all the vertices as not visitedvisited = [False]*self.Vstack =[]# Call the recursive helper function to store Topological# Sort starting from all vertices one by onefor i in range(self.V):if visited[i] == False:self.topologicalSortUtil(i,visited,stack)# Print contents of stackprint(stack)g= Graph(6)g.addEdge(5, 2);g.addEdge(5, 0);g.addEdge(4, 0);g.addEdge(4, 1);g.addEdge(2, 3);g.addEdge(3, 1);print("Following is a Topological Sort of the given graph")g.topologicalSort()
# Python code to demonstrate# method to remove i'th character# Naive Method# Initializing Stringtest_str = "CodeCatch"# Printing original stringprint ("The original string is : " + test_str)# Removing char at pos 3# using loopnew_str = ""for i in range(len(test_str)):if i != 2:new_str = new_str + test_str[i]# Printing string after removalprint ("The string after removal of i'th character : " + new_str)