Skip to content

Instantly share code, notes, and snippets.

@CalK16
CalK16 / 726-number-of-atoms.py
Created December 26, 2023 23:38
Among the solutions on the web, this is the most simple yet delightful code for solving leetcode 726-number-of-atoms I've ever seen.
class Solution:
def countOfAtoms(self, formula: str) -> str:
i = 0
ans = ""
def _countOfAtoms(formula):
nonlocal i
counts = defaultdict(int)
while i < len(formula):
if formula[i] == "(":
@CalK16
CalK16 / 51.N-Queens.py
Created July 23, 2023 23:39
51. N-Queens
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
leftright = [0] * (2 * n - 1)
rightleft = [0] * (2 * n - 1)
row = [0] * n
col = [0] * n
def valid(board, x, y):
return not (
row[x] or col[y]
@CalK16
CalK16 / expected_value.py
Last active May 6, 2023 06:44
HW: expected value calculator
# 加载 math 库,这个库有很多有用的模块可以用。你可以在本次作业中使用
# math.isclose(a, b, rel_tol=1e-10) 来比较 a 和 b。并且只比较到小数点后10位。
# 例如 math.isclose(5.0, 4.99998, rel_tol=1e-10) 会返回 True, 意思是 5.0 跟 4.99998 足够相似了。
# 如果你认为你不需要这个函数,可以忽略它。
import math
# 在此处定义你的 `expected_value` 函数
# def ....
# .....
@CalK16
CalK16 / tic-tac-toe.py
Created April 28, 2023 04:27
A tic-tac-toe in python
import os
GAME_BOARD = [[' '] * 3 for _ in range(3)]
COL = ROW = 3
def print_game_board():
count_row = 0
for row in GAME_BOARD:
a_row = ""
for col in row:
@CalK16
CalK16 / atm.py
Last active April 11, 2023 02:53
an ATM machine
BANK_ACCOUNT = "steven"
PASSWORD = "123456"
failed_attempts = 0
balance = 1000
def welcome():
print("Welcome to my ATM machine!")
def get_bank_account():
@CalK16
CalK16 / hashtable_external.c
Created December 26, 2022 05:12
Complete code for Understanding and implementing a Hash Table (in C) by Jacob Sorber
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#define MAX_NAME 256
#define TABLE_SIZE 10
typedef struct {
# So, you want to learn MinHeap. Here's an example of min heap.
# Sometimes you panic when you see so many lines of code, but trust me it is worth to read.
# Because there are only three operations: get peek, add item, and poll item.
# The functions are well divided in small functions, very simple, easy to understand.
class MinHeap():
def __init__(self):
self.arr = []
def peek(self):
if self._is_empty():
import os
import time
import shutil
import signal
import sys
############# Fill this Box ###################
# source_directory: folder which containers files to grab from
# target_directory: folder where to store files
@CalK16
CalK16 / get_qixi.py
Created September 17, 2020 17:52
get all qixi from 2020 to 2100
from ics import Calendar, Event
import datetime
from lunarcalendar import Converter, Solar, Lunar, DateNotExist
c = Calendar()
for i in range(2020, 2100):
lunar = Lunar(i, 7, 7)
d = Converter.Lunar2Solar(lunar)
str_d = d.to_date().strftime('%Y-%m-%d 00:00:00')
e = Event()