Skip to main content

Finding NULL values within set

Oct 7, 2022KETRICK
Loading...

More Python Posts

LeetCode Flood Fill

Oct 15, 2022CodeCatch

0 likes • 0 views

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

Lonely Integer

Feb 26, 2023wabdelh

0 likes • 0 views

#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77
# xor a list of integers to find the lonely integer
res = a[0]
for i in range(1,len(a)):
res = res ^ a[i]

Topological sort

Nov 19, 2022CodeCatch

0 likes • 0 views

#Python program to print topological sorting of a DAG
from collections import defaultdict
#Class to represent a graph
class Graph:
def __init__(self,vertices):
self.graph = defaultdict(list) #dictionary containing adjacency List
self.V = vertices #No. of vertices
# function to add an edge to graph
def addEdge(self,u,v):
self.graph[u].append(v)
# A recursive function used by topologicalSort
def topologicalSortUtil(self,v,visited,stack):
# Mark the current node as visited.
visited[v] = True
# Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
# Push current vertex to stack which stores result
stack.insert(0,v)
# The function to do Topological Sort. It uses recursive
# topologicalSortUtil()
def topologicalSort(self):
# Mark all the vertices as not visited
visited = [False]*self.V
stack =[]
# Call the recursive helper function to store Topological
# Sort starting from all vertices one by one
for i in range(self.V):
if visited[i] == False:
self.topologicalSortUtil(i,visited,stack)
# Print contents of stack
print(stack)
g= Graph(6)
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
print("Following is a Topological Sort of the given graph")
g.topologicalSort()

Create a Pascal’s Triangle

May 31, 2023CodeCatch

0 likes • 0 views

def generate_pascals_triangle(num_rows):
triangle = []
for row in range(num_rows):
# Initialize the row with 1
current_row = [1]
# Calculate the values for the current row
if row > 0:
previous_row = triangle[row - 1]
for i in range(len(previous_row) - 1):
current_row.append(previous_row[i] + previous_row[i + 1])
# Append 1 at the end of the row
current_row.append(1)
# Add the current row to the triangle
triangle.append(current_row)
return triangle
def display_pascals_triangle(triangle):
for row in triangle:
for number in row:
print(number, end=" ")
print()
# Prompt the user for the number of rows
num_rows = int(input("Enter the number of rows for Pascal's Triangle: "))
# Generate Pascal's Triangle
pascals_triangle = generate_pascals_triangle(num_rows)
# Display Pascal's Triangle
display_pascals_triangle(pascals_triangle)

delay time lambda

Nov 19, 2022CodeCatch

0 likes • 0 views

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

Sherlock Holmes Curious Lockbox Solver

Mar 12, 2021LeifMessinger

0 likes • 0 views

import copy
begining = [False,False,False,False,False,None,True,True,True,True,True]
#False = black True = white
its = [0]
def swap(layout, step):
layoutCopy = copy.deepcopy(layout)
layoutCopy[(step[0]+step[1])], layoutCopy[step[1]] = layoutCopy[step[1]], layoutCopy[(step[0]+step[1])]
return layoutCopy
def isSolved(layout):
for i in range(len(layout)):
if(layout[i] == False):
return (i >= (len(layout)/2))
def recurse(layout, its, steps = []):
if isSolved(layout):
its[0] += 1
print(layout,list(x[0] for x in steps))
return
step = None
for i in range(len(layout)):
if(layout[i] == None):
if(i >= 1): #If the empty space could have something to the left
if(layout[i - 1] == False):
step = [-1,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i > 1): #If the empty space could have something 2 to the left
if(layout[i - 2] == False):
step = [-2,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i < (len(layout)-1)): #If the empty space could have something to the right
if(layout[i + 1] == True):
step = [1,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i < (len(layout)-2)): #If the empty space could have something to the right
if(layout[i + 2] == True):
step = [2,i]
recurse(swap(layout,step), its, (steps+[step]))
its[0] += 1
#return None
recurse(begining,its,[])
print(its[0])