Skip to content

Instantly share code, notes, and snippets.

View bedekelly's full-sized avatar

Bede Kelly bedekelly

View GitHub Profile
@bedekelly
bedekelly / hundred_prisoners.py
Created July 6, 2022 16:52
Experiment for the 100 Prisoners riddle explained by Veritasium
"""
100 prisoners are each given a number.
A room is arranged with 100 boxes, each containing a unique prisoner's number.
Each prisoner must enter the room, open at most 50 boxes, and leave without
communicating anything. Their aim is to find their own number.
If *every* prisoner finds their own number, they succeed.
If a *single* prisoner fails to find their own number, they fail.
The naive approach of each prisoner opening a random 50 boxes results in an
incredibly low chance of success, (1/2)^100.
@bedekelly
bedekelly / rehash.sh
Created April 3, 2022 11:04
Rehash CSS files and replace their occurrences in HTML files.
for file in projects shared style
do
# Move the file to a name matching its hash.
echo mv css/$file*.css css/$file-`minihash css/$file*.css`.css
mv css/$file*.css css/$file-`minihash css/$file*.css`.css
# Grab the new filename and replace all occurrences in HTML files.
filePath=`ls css/$file-*.css`
newFile="$(basename $filePath)"
echo $filePath $newFile
@bedekelly
bedekelly / useRefState.ts
Created February 24, 2022 10:38
Reactive state coupled with an immediately-updating ref
import { MutableRef, useCallback, useRef, useState } from "preact/hooks";
type RefState<T> = [MutableRef<T>, T, (newVal: T) => void];
export default function useRefState<T>(initialValue: T): RefState<T> {
const ref = useRef<T>(initialValue);
const [value, setValue] = useState(initialValue);
const setValueAndUpdateRef = useCallback((value: T) => {
ref.current = value;
@bedekelly
bedekelly / useStateWithCallback.ts
Created July 19, 2021 09:17
Use state hook with an extra "getState" parameter
export function useStateWithCallback<T>(
defaultState: T | (() => T)
): readonly [T, Dispatch<SetStateAction<T>>, () => Promise<T>] {
const [state, setState] = useState<T>(defaultState);
const getState = useCallback(() => {
return new Promise<T>((resolve) => {
setState((oldValue) => {
resolve(oldValue);
return oldValue;
@bedekelly
bedekelly / Knob.svelte
Created November 5, 2020 13:26
Svelte implementation of a 2-way-binding knob control
<!-- App.svelte -->
<script>
import Knob from './Knob.svelte';
let value = 0;
const reset = () => { value = 50 };
</script>
<Knob bind:value={value} max={100} min={0} pixelRange={200}/>
<p>
@bedekelly
bedekelly / useSequence.js
Created August 22, 2020 15:09
Simple sequence hook for use with step-by-step forms etc
function useSequence({ values, onComplete }) {
const [index, setIndex] = useState(0);
function next() {
setIndex((oldIndex) => {
if (oldIndex + 1 >= values.length) {
setImmediate(onComplete);
return oldIndex;
} else {
return oldIndex + 1;
@bedekelly
bedekelly / useFocusOnKey.js
Last active August 22, 2020 13:18
Hook enabling focus-on-key behaviour for an element
import { useEffect, useRef } from "react";
/**
* Focus the given element when a key is pressed;
* and unfocus it when Escape is pressed.
*
* e.g.
* const ref = useFocusOnKey('/');
* return <input ref={ref} />
*/
@bedekelly
bedekelly / dynamodb-csv.py
Last active July 16, 2020 16:52
Import a CSV file into a DynamoDB table
import os
import ast
import sys
import csv
import boto3
AWS_ACCESS_KEY = "<YOUR ACCESS KEY HERE>"
AWS_SECRET_KEY = "<YOUR SECRET KEY HERE>"
@bedekelly
bedekelly / exploit.py
Created July 9, 2015 17:46
Microsoft Word Exploit - Original
import sys
import os
import warnings
import zlib
sys.path.append(os.getcwd() + '/' + "pylzma.egg")
import pylzma
import struct
import random
import shutil
from zipfile import ZipFile
@bedekelly
bedekelly / partials.py
Last active March 2, 2020 10:42
Graceful partial functions in Python. https://github.com/bedekelly/minitest for test library used.
import inspect
from minitest import case, tests
from functools import wraps, partial
def makepartial(fn):
"""
This function is a decorator which allows functions with a fixed
number of arguments gracefully to be transformed into partial functions
given too few arguments to execute.