Skip to content

Instantly share code, notes, and snippets.

View yoniLavi's full-sized avatar
:octocat:
Figuring things out

Yoni Lavi yoniLavi

:octocat:
Figuring things out
View GitHub Profile
@yoniLavi
yoniLavi / settings.json
Last active April 8, 2025 21:13
My VS Code settings
{
"css.format.newlineBetweenRules": false,
"debug.onTaskErrors": "debugAnyway",
"diffEditor.codeLens": true,
"editor.accessibilitySupport": "off",
"editor.autoClosingBrackets": "never",
"editor.autoClosingQuotes": "never",
"editor.copyWithSyntaxHighlighting": false,
"editor.dragAndDrop": false,
"editor.formatOnType": true,
@yoniLavi
yoniLavi / mocked_func_decorator.py
Created September 13, 2023 01:09
An educational example of a decorator that mocks a function
from contextlib import contextmanager
@contextmanager
def mocked_func(name, func=None, value=None):
"""Within this context, the function with name `func` will act as `func`, or as the literal `value`."""
def inner(*args, **kwargs):
return func(*args, **kwargs) if func else value
backup_globals = globals().copy()
globals()[name] = inner
@yoniLavi
yoniLavi / glob_match.py
Last active August 2, 2023 15:53
An alternative single function implementation for the challenge in https://third-bit.com/sdxpy/glob/
def match(text: str, pattern: str) -> bool:
"""Determine whether the text matches the glob pattern.
>>> match('', '')
True
>>> match('abc', 'abc')
True
>>> match('abcd', 'abc')
False
>>> match('aabc', 'abc')
def factorial_row(n, length):
"""Create 1d array whose product is the factorial of n, padded to length"""
return np.pad(np.arange(1, n + 1), (0, length - n), 'constant', constant_values=1)
def sum_factorial(nums):
"""Sum the factorials of the numbers in nums."""
factorial_matrix = np.vstack([factorial_row(n, max(nums)) for n in nums])
return factorial_matrix.prod(axis=1).sum()
@yoniLavi
yoniLavi / satisfied_demands.py
Last active December 6, 2023 13:39
A class to manage the marking of multiple demands as satisfied
import logging
LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(logging.StreamHandler())
LOGGER.setLevel('INFO')
class Demands:
"""A way to mark off satisfied demands"""
def __init__(self, demands):
"""Initialize a sequence of given demands as unsatisfied.
@yoniLavi
yoniLavi / eloquent_js_chapter16_game.html
Created May 27, 2023 13:37
A single-file version of the code from chapter 16 of Eloquent Javascript (Platform Game)
<!DOCTYPE html>
<!-- This is a single-file version of the code from chapter 16 of Eloquent Javascript
https://eloquentjavascript.net/16_game.html ("Project: A Platform Game") -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Platform Game</title>
<style>
.background {
background: rgb(52, 166, 251);
@yoniLavi
yoniLavi / fibonacci.js
Created May 20, 2023 10:30
Fibonacci number generator in JS
function* fib(upTo) {
let a = b = 1;
while (a < upTo) {
yield a;
[a, b] = [b, a + b];
}
}
console.log(...fib(1e10));
@yoniLavi
yoniLavi / karel_final.js
Created April 27, 2023 20:36
Stanford Karel final exercise solution
//Solution to Final Karel excercise - https://stanford.edu/~cpiech/karel/lessons.html#/english/unit12/lesson1
function goStraight() {
while (frontIsClear()) {
move();
}
}
function putDiagonally() {
putBeeper();
while (frontIsClear()) {
# My playing around with dataclass addition, to see how I'd implement this example without a static method:
# https://stackoverflow.com/a/49269322/493553
from copy import copy
from dataclasses import dataclass
@dataclass
class Stats:
strength: int = 0
speed: int = 0
intellect: int = 0
@yoniLavi
yoniLavi / physician_assistant.py
Created March 8, 2023 22:50
ChatGPT Physician's Assistant
"""A first try at a ChatGPT diagnosing bot; it's definitely not production ready, but I'm amazed that this works"""
import json
import openai
openai.api_key = "?"
completion = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=[