Skip to content

Instantly share code, notes, and snippets.

@py-in-the-sky
py-in-the-sky / recursive_generator_function.js
Last active August 29, 2015 14:28
Recursive Generator Function in JavaScript
// Works in Chrome devtools console
function* downToZero (n) {
yield n; // yield "first" element of the "list"
if (n > 0) // yield each element of the "rest" of the "list"
for (var n2 of downToZero(n - 1))
yield n2;
}
var tenToZero = downToZero(10);
@py-in-the-sky
py-in-the-sky / lazy_flatten.js
Created August 23, 2015 21:14
Lazily flatten a nested array with generator functions
// Works in Chrome devtools console.
/* Yields each non-array element from a potentially nested array. */
function* lazyFlatten (element) {
if (!Array.isArray(element))
yield element; // yield non-array element
else
for (var element2 of element) // unpack array into sub-elements
for (var element3 of lazyFlatten(element2)) // flatten each sub-element...
yield element3; // and yield its elements
}
@py-in-the-sky
py-in-the-sky / mockPromise.js
Last active January 21, 2016 23:51
Mock a no-op promise.
/*
Needed to mock a promise for a test just so that chaining `then`s
and `catch`es wouldn't throw an error. The actual execution of
those `then`s and `catches` could be no-ops since the test wasn't
concerned with their functionality.
The mocked promise (`returnMockThenable()`) was returned from a mock
`fetch` call.
*/
@py-in-the-sky
py-in-the-sky / very_simple_json_parser.py
Last active May 6, 2024 09:09
Very simple JSON parser in Python
"""
The parser below is a predictive, recursive-descent parser. The use of a predictive parser
is possible because JSON has an LL(k) grammar. Specifically, JSON has an LL(1) grammar,
a fact that is hinted at in the `parse_json` function: `parse_json` looks ahead by only
one character (not even a whole JSON token) in order to decide how to parse the string.
See: https://en.wikipedia.org/wiki/Recursive_descent_parser
JSON definition: http://www.json.org/
JSON-Python type mapping: https://docs.python.org/2/library/json.html#encoders-and-decoders
"""
Very Simple Integer-arithmetic Evaluator
Grammar:
ARITHMETIC => TERM | TERM [+-] ARITHMETIC
TERM => FACTOR | FACTOR [*/] TERM
FACTOR => INT | '(' ARITHMETIC ')' | [+-] FACTOR
INT => [1-9][0-9]*
@py-in-the-sky
py-in-the-sky / bisect_and_binary_search.py
Last active March 18, 2023 20:37
Bisect and Binary Search
"""
My variation of the Python bisect functions, inspired by my reading
of chapter 4 of Beautiful Code.
Studying the source code of Python's bisect module has been
very enlightening for me, both about binary search and about
setting up and reasoning about loop invariants in order to
ensure the correctness of a program.
Chapter 4 of Beautiful Code introduced me to slightly different
@py-in-the-sky
py-in-the-sky / convert.py
Created December 30, 2017 15:38
Convert a number from base 10 to another base.
"""
Written after seeing a similar program in the MIT/EDX course
Advanced Software Construction in Java.
"""
def convert(n, base):
assert 2 <= base <= 16
if n == 0:
@py-in-the-sky
py-in-the-sky / best_trade.py
Last active January 28, 2018 19:30
Best Trade
"""
Best Trade
Given a list of prices of some stock, determine the maximum
money that could be made from buying and then selling once.
That is, find the value (prices[sell_index] - prices[buy_index])
such that len(prices) > sell_index >= buy_index >= 0 and for all
other such index pairs (sell_index_2, buy_index_2), it is
the case that:
@py-in-the-sky
py-in-the-sky / knapsack_branch_and_bound.py
Last active January 29, 2018 22:19
Knapsack Branch-and-bound
"""
My implementation of a branch-and-bound solution to the Knapsack problem.
See section 3.8 of the book Computer Science Distilled by Wladston Ferreira Filho.
"""
from my_abstract_data_types import *
@py-in-the-sky
py-in-the-sky / lucky_dip.py
Last active March 20, 2018 03:35
Solution to Google Code Jam Problem "Lucky Dip"
"""
Lucky Dip: https://codejam.withgoogle.com/codejam/contest/9234486/dashboard#s=p1
Concept Inventory:
* Optimal Play: To play optimally, you choose to redip if your current
dip's value is less than the expected value of redipping. Otherwise,
you stick with your current draw.
* Expected Value of a Dip: The expected value of a dip, given that the player
plays optimally, depends on the probability of drawing a value that is less