Skip to content

Instantly share code, notes, and snippets.

View rongpenl's full-sized avatar
🏠
Working from home

Ron Li rongpenl

🏠
Working from home
View GitHub Profile
@rongpenl
rongpenl / egyption_fraction_expansion.py
Created April 17, 2024 16:52
A sample scene for KorigamiK
# https://youtu.be/AtjXJVEgwqg?si=361RqZqCvRvJure0
from manim import *
class Extension4AnyRationalNumberQ2(Scene):
# what's the shortest n
# https://en.wikipedia.org/wiki/Greedy_algorithm_for_Egyptian_fractions#:~:text=An%20Egyptian%20fraction%20is%20a,%3D%2012%20%2B%2013.
# https://r-knott.surrey.ac.uk/Fractions/egyptian.html#section5.2
def construct(self):
self.camera.background_color = "#F1F3F8"
question = Tex(
# Ron and Math: https://www.youtube.com/post/Ugkx-VJIlzGcL15mA4wX1kpkuKZq3634vYrh
def frog_jump():
steps = {(0,0): 1}
for x in range(5):
for y in range(5):
if (x,y) not in steps:
steps[(x,y)] = steps.get((x-1,y),0) + steps.get((x-2,y),0) + steps.get((x,y-1),0) + steps.get((x,y-2),0)
return steps[(4,4)] # 556
@rongpenl
rongpenl / mail_delivery.py
Last active March 22, 2024 17:14
Answer for the mail delivery problem using dynamic programming
# ron and math community post: https://www.youtube.com/post/Ugkx321bNyuBEYuPthOIEKpbaqz04Ujat1f_
def a(n):
if n < 3:
return 1
if n not in memo_a:
memo_a[n] = a(n - 2) + a(n - 3)
return memo_a[n]
def b(n):
if n not in memo_b:
@rongpenl
rongpenl / dragon.py
Created February 27, 2024 19:00
dragon heads logic
def head_1(statement: bool):
if statement:
return True
else:
return False
def head_2(statement: bool):
if statement:
return False
else:
@rongpenl
rongpenl / A.py
Created November 9, 2023 06:05
The function to calculate A(n,i) for the video https://www.youtube.com/watch?v=sjcle2vIueU
def A(n,i):
# calculate using a simulation approach.
i = i-1 # convert to 0-based index.
urinals = np.zeros(n)
urinals[i] = 1 # the ith is occupied.
while True:
# find the furtherest urinal that is away from any occupied urinal.
candidates = {} # key: the index of the urinal, value: the distance to the nearest occupied urinal.
for j in range(n):
if urinals[j] == 1:
@rongpenl
rongpenl / context_manager_use_cases.py
Created May 29, 2023 04:25
Cracking Intermediate Python Episode 1: Context Manager
from threading import Lock
import sqlite3
import requests
# thread lock example
lock = Lock()
with lock:
# Critical section of code
print("Critical section running")
@rongpenl
rongpenl / class_context_manager.py
Created May 29, 2023 04:25
Cracking Intermediate Python Episode 1: Context Manager
from coach import Coach
class ActivityManagedCoach(Coach):
def __enter__(self):
self.warm_up_players()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.host_retro_meeting()
print(f"Exception type: {exc_type}")
@rongpenl
rongpenl / open_file.py
Created May 29, 2023 04:23
Cracking Intermediate Python Episode 1: Context Manager
from contextlib import contextmanager
@contextmanager
def open_file(path, mode):
try:
file = open(path, mode)
yield file
finally:
file.close()
@rongpenl
rongpenl / decorator_context_manager.py
Created May 29, 2023 04:22
Cracking Intermediate Python Episode 1: Context Manager
from contextlib import contextmanager
from coach import Coach
@contextmanager
def activity_management(coach: Coach):
try:
coach.warm_up_players()
yield coach
finally:
coach.host_retro_meeting()
@rongpenl
rongpenl / routine_refactoring.py
Created May 29, 2023 04:21
Cracking Intermediate Python Episode 1: Context Manager
from typing import Callable
class Coach:
def warm_up_players(self):
print("The coach is warming up the players.")
def host_retro_meeting(self):
print("The coach is having a retro meeting after the game.")
def coach_game(self):