This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from polygenerator import random_polygon | |
import matplotlib.pyplot as plt | |
def is_inside(edges, xp, yp): | |
cnt = 0 | |
for edge in edges: | |
(x1, y1), (x2, y2) = edge | |
if (yp < y1) != (yp < y2) and xp < x1 + ((yp-y1)/(y2-y1))*(x2-x1): | |
cnt += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def are_symmetric(root1, root2) | |
if root1 is None and root2 is None: | |
return True | |
elif ((root1 is None) != (root2 is None)) or root1.val != root2.val | |
return False | |
else: | |
return are_symmetric(root1.left, root2.right) and are_symmetric(root1.right, root2.left) | |
def is_symmetric(root): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_start(arr, target): | |
if arr[0] == target: | |
return 0 | |
left, right = 0, len(arr)-1 | |
while left <= right: | |
mid = (left+right)//2 | |
if arr[mid] == target and arr[mid-1] < target: | |
return mid | |
elif arr[mid] < target: | |
left = mid+1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def spiralOrder(self, matrix): | |
n, m = len(matrix), len(matrix[0]) | |
i, j = 0, 0 | |
delta = (0, 1) | |
direction_map = {(0, 1): (1, 0), (1, 0): (0, -1), (0, -1): (-1, 0), (-1, 0): (0, 1)} | |
spiral = [matrix[i][j]] | |
matrix[i][j] = None | |
while len(spiral) < n*m: | |
next_i, next_j = i+delta[0], j+delta[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import random | |
import cv2 | |
import numpy as np | |
from queue import Queue | |
class SnakeGame: | |
def __init__(self, h=600, w=810, game_speed=60): | |
self.cell_size = 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from queue import Queue | |
def flood_fill(board, i, j): | |
n, m = len(board), len(board[0]) | |
queue = Queue() | |
queue.put((i, j)) | |
board[i][j] = '-' | |
while not queue.empty(): | |
i, j = queue.get() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List, Any | |
class Node: | |
def __init__(self, key, nxt=None): | |
self.key: str = key | |
self.nxt: Node = nxt | |
def __repr__(self): | |
return str(self.key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HexGame: | |
def __init__(self, n=11): | |
self.n = n | |
self.board = [[0]*n for _ in range(n)] | |
self.cells = [(i, j) for i in range(n) for j in range(n)] | |
self.top_node = (-1, 0) | |
self.bottom_node = (n, 0) | |
self.left_node = (0, -1) | |
self.right_node = (0, n) | |
self.ds_red = DisjointSet(self.cells + [self.top_node, self.bottom_node]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
class DisjointSet: | |
def __init__(self, elems): | |
self.elems = elems | |
self.parent = {} | |
self.size = {} | |
self.graph = nx.DiGraph() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DisjointSet: | |
def __init__(self, elems): | |
self.elems = elems | |
self.parent = {} | |
self.size = {} | |
for elem in elems: | |
self.make_set(elem) | |
def make_set(self, x): | |
self.parent[x] = x |
NewerOlder