Skip to content

Instantly share code, notes, and snippets.

@pamelafox
pamelafox / recursive_approaches.py
Created Mar 19, 2021
Recursive problem solving approaches
View recursive_approaches.py
# Recursion + number + return
def sum_digits(n):
"""
>>> sum_digits(1234)
10
"""
if n < 10:
return n
return n % 10 + sum_digits(n // 10)
View tree_class.py
class Tree:
"""A tree."""
def __init__(self, label, branches=[]):
self.label = label
for branch in branches:
assert isinstance(branch, Tree)
self.branches = list(branches)
def __repr__(self):
if self.branches:
View link_class.py
View link_iter.py
@pamelafox
pamelafox / warandpeace.py
Created Mar 8, 2021
War and Peace Insertion Showdown
View warandpeace.py
import timeit
class Link:
empty = ()
def __init__(self, first, rest=empty):
self.first = first
self.rest = rest
def insert_at_start(self, value):
View gist:5a0ea6cf2b2bd391d1864e0369ac0f88
import dis
def for_version():
y = 0
for x in [1, 2, 3]:
y += x * 2
def iter_version():
_gen_ = iter([1, 2, 3])
y = 0
@pamelafox
pamelafox / pairing.py
Last active Jan 12, 2021
Pairing aging distant pop stars
View pairing.py
""" Check your work by running python -m doctest -v popstar.py """
def gen_popstar_name(last_name, street, last_food):
"""Your pop star name is your last name followed
by the name of the street your grew up on followed
by the last food you ate.
The only whitespace should be between each of the
three parts, and the name should be uppercase.
>>> gen_popstar_name("fox", "city lights", "bananas")
@pamelafox
pamelafox / fibonacci.html
Created Sep 17, 2020
Animated Fibonacci SVG
View fibonacci.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Fibonacci (Animated)</title>
<style>
body {
background: #f3f3f3;
}
@pamelafox
pamelafox / astronauts.sql
Created Mar 20, 2020
NASA astronauts, 1959-Present
View astronauts.sql
/* Source:
https://www.kaggle.com/nasa/astronaut-yearbook
*/
CREATE TABLE astronauts(
Name TEXT PRIMARY KEY,
Year INTEGER,
GroupNum INTEGER,
Status TEXT,
Birth_Date TEXT,
Birth_Place TEXT,
@pamelafox
pamelafox / nba_players.sql
Created Mar 20, 2020
NBA players of the week
View nba_players.sql
/*
Source:
https://www.kaggle.com/jacobbaruch/nba-player-of-the-week
*/
CREATE TABLE players(
Player TEXT,
Team TEXT,
Conference TEXT,
Date TEXT,
Position TEXT,