Skip to content

Instantly share code, notes, and snippets.

@rvcx
rvcx / dalle.py
Created November 2, 2023 00:30
simple python CLI for DALL-E
#! /usr/bin/env python3
import argparse
import json
import os
import requests
import subprocess
import sys
import urllib.request
from rich.progress import (Progress, SpinnerColumn, TimeElapsedColumn)
@rvcx
rvcx / sun.py
Created January 16, 2023 21:11
Compute sunrise and sunset times based on latitude, longitude, and date
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import datetime
import math
import zoneinfo
def rise_set(lat, long, fordate, tz=None):
refdate = datetime.datetime(2000, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)
n = (fordate - refdate.date()).days
@rvcx
rvcx / morse.py
Last active August 15, 2022 10:41
Python dictionary of morse code characters
codes = {'.-' : 'a',
'-...' : 'b',
'-.-.' : 'c',
'-..' : 'd',
'.' : 'e',
'..-.' : 'f',
'--.' : 'g',
'....' : 'h',
'..' : 'i',
'.---' : 'j',
def myhash(fs, gs, text):
out = [0] * len(text)
out[0] = gs[(fs[text[0]] + fs[text[-1]]) % 10]
for i in range(1, len(out)):
out[i] = gs[(out[i-1] + fs[text[i]]) % 10]
return "".join(str(d) for d in out)
letters = 'abcdefghijklmnopqrstuvwxyz'
def crack(c):
@rvcx
rvcx / ny-rcv.py
Created June 30, 2021 02:22
New York's ranked-choice tabulation algorithm
from collections import defaultdict
def winner(ballots):
eliminated = []
while True:
tally = defaultdict(int)
for b in ballots:
pruned = [c for c in b if c not in eliminated]
if pruned:
tally[pruned[0]] += 1
totals = [(votes, candidate) for (candidate, votes) in tally.items()]
@rvcx
rvcx / ellipticals.py
Created May 5, 2019 19:14
compute (and visualize) ellipses passing through two points
import random
from math import sin, cos, radians, sqrt, atan2, degrees, asin, pi
import sys
import contextlib
scale = 100
@contextlib.contextmanager
def svg(f=sys.stdout, h=(scale * 2), w=(scale * 2)):
print('<html><body><svg height="{}" width="{}">'.format(h, w), file=f)
// Quick brute-force solution to
// https://fivethirtyeight.com/features/to-solve-the-eccentric-billionaires-puzzle-you-must-first-defeat-the-banker/
#include <iostream>
#include <random>
#include <functional>
auto roll = std::bind(std::uniform_int_distribution<int>(0, 5),
std::default_random_engine());
#!/usr/bin/env python3
import argparse
import sys
import collections
def main():
parser = argparse.ArgumentParser()
parser.add_argument('scorefile')
args = parser.parse_args()
#!/usr/bin/env python3
import argparse
import sys
import collections
def main():
areas = { 'a' : 12,
'b' : 12,
'c' : 12,
#!/usr/bin/env python3
import argparse
import sys
def main():
for pop in range(101):
for v1 in range(pop + 1):
v2 = pop - v1
if v1 // 10 == v2 % 10 and v1 % 10 == v2 // 10: