Skip to content

Instantly share code, notes, and snippets.

View timgianitsos's full-sized avatar

Tim Gianitsos timgianitsos

  • Palo Alto, CA
View GitHub Profile
@timgianitsos
timgianitsos / random_process_modes.py
Last active December 6, 2022 06:44
Random Process Modes
from math import comb
import numpy as np
import matplotlib.pyplot as plt
from tqdm import trange
'''
There are `N` items. We select `n` (NOT necessarily unique) items per round with replacement. Assume `1 <= n`.
Consider the Random Process X_0, X_1, X_2, ....
Each `X_i` represents the number of unique items selected through round `i`.
@timgianitsos
timgianitsos / cayley_tables.py
Last active October 30, 2022 00:42
Computes all the possible Cayley Tables for groups of size N
'''
Motivated by
https://www.youtube.com/watch?v=BwHspSCXFNM&list=PLi01XoE8jYoi3SgnnGorR_XOW3IcK-TP6&index=7
'''
__author__ = 'Tim Gianitsos'
from random import randint
import numpy as np
@timgianitsos
timgianitsos / display_img.py
Last active March 6, 2023 20:05
Display images on the command line using 4 shades of gray
import numpy as np
__author__ = 'Tim Gianitsos'
def display_img(img):
'''
img: a 2D array of ints
'''
dark_black = '\u001B[40m '
light_black = '\u001B[100m '
from itertools import chain, combinations
import json
import warnings
__author__ = 'Tim Gianitsos'
class PowerSetCounter:
'''
Author: Tim Gianitsos
Count all combinations of occurences of the given iterable and their attributes
@timgianitsos
timgianitsos / rsa.py
Created October 11, 2020 19:08 — forked from tbenjis/rsa.py
A simple RSA encryption in python using Miller-Rabin primality test. Outputs results in text files
# Compute RSA encryption in python
# Author: Tunde
import random
import math
import argparse
import re
KEY_INT = 100 # we define the near max value to get its random number for key
def main():
from time import time, sleep
__author__ = 'Tim Gianitsos'
class Stopwatch:
'''
Display loop progress
Wrap an iterable object, and obtain automatic updates on the progress of iteration.
Example:
Replace `for i in range(15):` with `for i in Stopwatch(range(15)):`
import java.math.BigInteger;
import java.util.Scanner;
/*
To run, use
`javac *.java && java -ea BinomialCoefficients`
Author: Tim Gianitsos
*/
public class BinomialCoefficients
@timgianitsos
timgianitsos / zipf.py
Last active April 8, 2022 07:37
Input a file to see how closely word frequencies correspond to the Zipf distribution
from collections import Counter
from string import punctuation
from os.path import split
import sys
__author__ = 'Tim Gianitsos'
def main(filename=None, num_display=30):
c = Counter(s for t in open(filename).read().split() if (s:=t.lower().replace(fr'“‘”’{punctuation}', '')).isalpha())
mc = c.most_common()
@timgianitsos
timgianitsos / UnionFind.py
Last active January 24, 2023 23:57
Union-Find data structure, (aka Disjoint-Set) https://en.wikipedia.org/wiki/Disjoint-set_data_structure
'''
Union-find data structure. Based on Josiah Carlson's code,
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/215912
with significant additional changes by D. Eppstein
https://www.ics.uci.edu/~eppstein/PADS/UnionFind.py
with additional changes by Tim Gianitsos
'''
class UnionFind: