Loading...
More Python Posts
https://codecatch.net/post/06c9f5b7-1e00-40dc-b436-b8cccc4b69be
# Python program for implementation of Bogo Sortimport random# Sorts array a[0..n-1] using Bogo sortdef bogoSort(a):n = len(a)while (is_sorted(a)== False):shuffle(a)# To check if array is sorted or notdef is_sorted(a):n = len(a)for i in range(0, n-1):if (a[i] > a[i+1] ):return Falsereturn True# To generate permuatation of the arraydef shuffle(a):n = len(a)for i in range (0,n):r = random.randint(0,n-1)a[i], a[r] = a[r], a[i]# Driver code to test abovea = [3, 2, 4, 1, 0, 5]bogoSort(a)print("Sorted array :")for i in range(len(a)):print ("%d" %a[i]),
def sum_of_powers(end, power = 2, start = 1):return sum([(i) ** power for i in range(start, end + 1)])sum_of_powers(10) # 385sum_of_powers(10, 3) # 3025sum_of_powers(10, 3, 5) # 2925
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="yourusername",password="yourpassword")mycursor = mydb.cursor()mycursor.execute("CREATE DATABASE mydatabase")
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
""" Binary Search Algorithm----------------------------------------"""#iterative implementation of binary search in Pythondef binary_search(a_list, item):"""Performs iterative binary search to find the position of an integer in a given, sorted, list.a_list -- sorted list of integersitem -- integer you are searching for the position of"""first = 0last = len(a_list) - 1while first <= last:i = (first + last) / 2if a_list[i] == item:return ' found at position '.format(item=item, i=i)elif a_list[i] > item:last = i - 1elif a_list[i] < item:first = i + 1else:return ' not found in the list'.format(item=item)#recursive implementation of binary search in Pythondef binary_search_recursive(a_list, item):"""Performs recursive binary search of an integer in a given, sorted, list.a_list -- sorted list of integersitem -- integer you are searching for the position of"""first = 0last = len(a_list) - 1if len(a_list) == 0:return ' was not found in the list'.format(item=item)else:i = (first + last) // 2if item == a_list[i]:return ' found'.format(item=item)else:if a_list[i] < item:return binary_search_recursive(a_list[i+1:], item)else:return binary_search_recursive(a_list[:i], item)