Skip to main content

ZapFinder

0 likes • Jan 23, 2021 • 0 views
Python
Loading...

More Python Posts

LeetCode Flood Fill

0 likes • Oct 15, 2022 • 0 views
Python
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 image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if 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

Bitonic sort

0 likes • Nov 19, 2022 • 0 views
Python
# Python program for Bitonic Sort. Note that this program
# works only when size of input is a power of 2.
# The parameter dir indicates the sorting direction, ASCENDING
# or DESCENDING; if (a[i] > a[j]) agrees with the direction,
# then a[i] and a[j] are interchanged.*/
def compAndSwap(a, i, j, dire):
if (dire==1 and a[i] > a[j]) or (dire==0 and a[i] > a[j]):
a[i],a[j] = a[j],a[i]
# It recursively sorts a bitonic sequence in ascending order,
# if dir = 1, and in descending order otherwise (means dir=0).
# The sequence to be sorted starts at index position low,
# the parameter cnt is the number of elements to be sorted.
def bitonicMerge(a, low, cnt, dire):
if cnt > 1:
k = cnt/2
for i in range(low , low+k):
compAndSwap(a, i, i+k, dire)
bitonicMerge(a, low, k, dire)
bitonicMerge(a, low+k, k, dire)
# This funcion first produces a bitonic sequence by recursively
# sorting its two halves in opposite sorting orders, and then
# calls bitonicMerge to make them in the same order
def bitonicSort(a, low, cnt,dire):
if cnt > 1:
k = cnt/2
bitonicSort(a, low, k, 1)
bitonicSort(a, low+k, k, 0)
bitonicMerge(a, low, cnt, dire)
# Caller of bitonicSort for sorting the entire array of length N
# in ASCENDING order
def sort(a,N, up):
bitonicSort(a,0, N, up)
# Driver code to test above
a = [3, 7, 4, 8, 6, 2, 1, 5]
n = len(a)
up = 1
sort(a, n, up)
print ("\n\nSorted array is")
for i in range(n):
print("%d" %a[i]),

convert bytes to a string

0 likes • Jun 1, 2023 • 0 views
Python
bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second

Number guessing game

0 likes • Nov 19, 2022 • 0 views
Python
""" Number Guessing Game
----------------------------------------
"""
import random
attempts_list = []
def show_score():
if len(attempts_list) <= 0:
print("There is currently no high score, it's yours for the taking!")
else:
print("The current high score is {} attempts".format(min(attempts_list)))
def start_game():
random_number = int(random.randint(1, 10))
print("Hello traveler! Welcome to the game of guesses!")
player_name = input("What is your name? ")
wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name))
// Where the show_score function USED to be
attempts = 0
show_score()
while wanna_play.lower() == "yes":
try:
guess = input("Pick a number between 1 and 10 ")
if int(guess) < 1 or int(guess) > 10:
raise ValueError("Please guess a number within the given range")
if int(guess) == random_number:
print("Nice! You got it!")
attempts += 1
attempts_list.append(attempts)
print("It took you {} attempts".format(attempts))
play_again = input("Would you like to play again? (Enter Yes/No) ")
attempts = 0
show_score()
random_number = int(random.randint(1, 10))
if play_again.lower() == "no":
print("That's cool, have a good one!")
break
elif int(guess) > random_number:
print("It's lower")
attempts += 1
elif int(guess) < random_number:
print("It's higher")
attempts += 1
except ValueError as err:
print("Oh no!, that is not a valid value. Try again...")
print("({})".format(err))
else:
print("That's cool, have a good one!")
if __name__ == '__main__':
start_game()

Nodes and Trees

0 likes • Nov 18, 2022 • 0 views
Python
import random
class Node:
def __init__(self, c):
self.left = None
self.right = None
self.color = c
def SetColor(self,c) :
self.color = c
def PrintNode(self) :
print(self.color)
def insert(s, root, i, n):
if i < n:
temp = Node(s[i])
root = temp
root.left = insert(s, root.left,2 * i + 1, n)
root.right = insert(s, root.right,2 * i + 2, n)
return root
def MakeTree(s) :
list = insert(s,None,0,len(s))
return list
def MakeSet() :
s = []
count = random.randint(7,12)
for _ in range(count) :
color = random.randint(0,1) == 0 and "Red" or "White"
s.append(color)
return s
def ChangeColor(root) :
if (root != None) :
if (root.color == "White") :
root.SetColor("Red")
ChangeColor(root.left)
ChangeColor(root.right)
def PrintList(root) :
if root.left != None :
PrintList(root.left)
else :
root.PrintNode()
if root.right != None :
PrintList(root.right)
else :
root.PrintNode()
t1 = MakeTree(MakeSet())
print("Original Colors For Tree 1:\n")
PrintList(t1)
ChangeColor(t1)
print("New Colors For Tree 1:\n")
PrintList(t1)
t2 = MakeTree(MakeSet())
print("Original Colors For Tree 2:\n")
PrintList(t2)
ChangeColor(t2)
print("New Colors For Tree 2:\n")
PrintList(t2)
t3 = MakeTree(MakeSet())
print("Original Colors For Tree 3:\n")
PrintList(t3)
ChangeColor(t3)
print("New Colors For Tree 3:\n")
PrintList(t3)