• Nov 19, 2022 •CodeCatch
0 likes • 0 views
# Python program to reverse a linked list # Time Complexity : O(n) # Space Complexity : O(n) as 'next' #variable is getting created in each loop. # Node class class Node: # Constructor to initialize the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None # Function to reverse the linked list def reverse(self): prev = None current = self.head while(current is not None): next = current.next current.next = prev prev = current current = next self.head = prev # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Utility function to print the linked LinkedList def printList(self): temp = self.head while(temp): print temp.data, temp = temp.next # Driver program to test above functions llist = LinkedList() llist.push(20) llist.push(4) llist.push(15) llist.push(85) print "Given Linked List" llist.printList() llist.reverse() print "\nReversed Linked List" llist.printList()
# Python code to find the URL from an input string # Using the regular expression import re def Find(string): # findall() has been used # with valid conditions for urls in string regex = 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 Code string = 'My Profile: https://codecatch.net' print("Urls: ", Find(string))
0 likes • 1 view
from collections import defaultdict def collect_dictionary(obj): inv_obj = defaultdict(list) for key, value in obj.items(): inv_obj[value].append(key) return dict(inv_obj) ages = { 'Peter': 10, 'Isabel': 10, 'Anna': 9, } collect_dictionary(ages) # { 10: ['Peter', 'Isabel'], 9: ['Anna'] }
# Python code to demonstrate # method to remove i'th character # Naive Method # Initializing String test_str = "CodeCatch" # Printing original string print ("The original string is : " + test_str) # Removing char at pos 3 # using loop new_str = "" for i in range(len(test_str)): if i != 2: new_str = new_str + test_str[i] # Printing string after removal print ("The string after removal of i'th character : " + new_str)
0 likes • 2 views
# Input for row and column R = int(input()) C = int(input()) # Using list comprehension for input matrix = [[int(input()) for x in range (C)] for y in range(R)]
• May 31, 2023 •CodeCatch
class Rectangle: pass class Square(Rectangle): pass rectangle = Rectangle() square = Square() print(isinstance(rectangle, Rectangle)) # True print(isinstance(square, Rectangle)) # True print(isinstance(square, Square)) # True print(isinstance(rectangle, Square)) # False