• Nov 19, 2022 •CodeCatch
0 likes • 0 views
# 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))
import math def factorial(n): print(math.factorial(n)) return (math.factorial(n)) factorial(5) factorial(10) factorial(15)
• Nov 18, 2022 •AustinLeath
# @return a list of strings, [s1, s2] def letterCombinations(self, digits): if '' == digits: return [] kvmaps = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' } return reduce(lambda acc, digit: [x + y for x in acc for y in kvmaps[digit]], digits, [''])
• Jun 1, 2023 •CodeCatch
0 likes • 2 views
def calculate_values(): value1 = 10 value2 = 20 return value1, value2 result1, result2 = calculate_values() print("Result 1:", result1) print("Result 2:", result2)
• May 31, 2023 •CodeCatch
0 likes • 5 views
my_list = [1, 2, 3, 4, 5] removed_element = my_list.pop(2) # Remove and return element at index 2 print(removed_element) # 3 print(my_list) # [1, 2, 4, 5] last_element = my_list.pop() # Remove and return the last element print(last_element) # 5 print(my_list) # [1, 2, 4]
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'] }