Loading...
More Python Posts
# Python program for implementation of Bubble Sortdef bubbleSort(arr):n = len(arr)# Traverse through all array elementsfor i in range(n-1):# range(n) also work but outer loop will repeat one time more than needed.# Last i elements are already in placefor j in range(0, n-i-1):# traverse the array from 0 to n-i-1# Swap if the element found is greater# than the next elementif arr[j] > arr[j+1] :arr[j], arr[j+1] = arr[j+1], arr[j]# Driver code to test abovearr = [64, 34, 25, 12, 22, 11, 90]bubbleSort(arr)print ("Sorted array is:")for i in range(len(arr)):print ("%d" %arr[i]),
# 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)
class Solution(object):def floodFill(self, image, sr, sc, newColor):R, C = len(image), len(image[0])color = image[sr][sc]if color == newColor: return imagedef dfs(r, c):if image[r][c] == color:image[r][c] = newColorif r >= 1: dfs(r-1, c)if r+1 < R: dfs(r+1, c)if c >= 1: dfs(r, c-1)if c+1 < C: dfs(r, c+1)dfs(sr, sc)return image
# Input for row and columnR = int(input())C = int(input())# Using list comprehension for inputmatrix = [[int(input()) for x in range (C)] for y in range(R)]
# 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 clamp_number(num, a, b):return max(min(num, max(a, b)), min(a, b))clamp_number(2, 3, 5) # 3clamp_number(1, -1, -5) # -1