This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import atexit | |
import time | |
import logging | |
import signal | |
import os | |
def sighandler(sigcode, x): | |
print("Signal Exit!!", flush=True) | |
print([sigcode, x], flush=True) | |
exitcall() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def heapalgo(items, n): | |
"""heap algorithm to generate all permutations in-place | |
Args: | |
items: list of items | |
n: initially the list of items, only permute on the first n-items | |
Generates: | |
a permutation of first n items of the list, in-place shuffle performed | |
""" | |
assert items, "cannot go with empty list" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Given $k$ sorted lists, each of size $n$, find a range $(p,q)$ such that: | |
1. q-p is minimized | |
2. for any list, there is an element $r$, such that $p <= r <= q$ | |
Example: | |
[4, 10, 15, 24] | |
[0, 9, 12, 20] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Selecton rank algorithm | |
""" | |
import random | |
def selrank(array, start, stop, k): | |
"""return the k-th largest element (0-th = max) in sliced list | |
`array[start:stop]` using the selection rank algorithm | |
This move the larger elements of array to lower indices such that at the end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from math import sin, cos, radians, gcd | |
from typing import Tuple, List | |
import numpy as np | |
from PIL import Image, ImageDraw | |
RADIANS = np.pi / 180 | |
DIMENSION = (500, 500) # coordinate origin at centre |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""Provide class of a generic ANN for classification (using binary cross entropy) | |
""" | |
import numpy as np | |
# define activation functions g(Z), and their first derivative, using numpy | |
# hold all activation function pairs in a global dict |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import socketserver | |
import threading | |
def sender(hostname, port, content): | |
"""Connect to a TCP port and send content, then wait for reply | |
""" | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | |
sock.connect((hostname, port)) | |
sock.sendall(content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def weighted_percentile(a, q, weights=None, interpolation='step'): | |
""" | |
Compute the qth percentile of the data a, optionally weight can be provided. | |
Returns the qth percentile(s) of the array elements. | |
Methodology | |
----------- | |
If weights are not provided, we set all `a` of equal weight of 1. Then we | |
normalize the weight by equal factor so that their sum is 1. Then, in sorted | |
ascending order of `a`, we plot the values as a curve from 0 to 1 and lookup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# typed from video: youtu.be/npw4s1QTmPg | |
# Raymond Gettinger, Modern Python Dictionaries: A confluence of a dozen great idea, PyCon 2017 | |
from __future__ import division, print_function | |
import array | |
import collections | |
import itertools | |
# Placeholder constants | |
FREE = -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sliding_window(a, win_size): | |
'''Slding window view of a 2D array a using numpy stride tricks. | |
For a given input array `a` and the output array `b`, we will have | |
`b[i] = a[i:i+w]` | |
Args: | |
a: numpy array of shape (N,M) | |
Returns: | |
numpy array of shape (K,w,M) where K=N-w+1 | |
''' |