Skip to content

Instantly share code, notes, and snippets.

# From 2011-B sample.
# Q2
def transpose_graph(G):
GT = {}
for node in G:
GT[node] = set()
for node in G:
for neighbor in G[node]:
import threading
import datetime
import time
import random
class SimpleThreadClass(threading.Thread):
def run(self):
for i in range(3):
now = datetime.datetime.now()
print("I am: ")
@metula
metula / ecc.py
Last active August 29, 2015 14:00
def check_digit(id8):
total = 0
for i in range(8):
val = int(id8[i]) # converts to numeric value
if i % 2 == 0:
total = total + val
else:
if val < 5:
total = total + 2 * val
else:
############################################################################
### image processing
############################################################################
# importing out own Matrix class.
from matrix import *
import random
def add_gauss(mat, sigma=10):
''' Generates Gaussian noise with mean 0 and SD sigma.
#############################################################################
######### class Matrix with display() function for image visualization
#############################################################################
class Matrix:
"""
Represents a rectangular matrix with n rows and m columns.
"""
def __init__(self, n, m, val=0):
@metula
metula / LZ.py
Last active August 6, 2016 14:16
import math
import random
def str_to_ascii(text):
""" Gets rid of on ASCII characters in text"""
return ''.join(ch for ch in text if ord(ch)<128)
def maxmatch(T, p, w=2**12-1, max_length=2**5-1):
""" finds a maximum match of length k<=2**5-1 in a
w long window, T[p:p+k] with T[p-m:p-m+k].
import http.client
import random
def random_string(n):
return "".join(chr(random.randrange(128)) for i in range(n))
def str_to_bin(text):
""" translates text to binary reprersentation using
7 bits per character. Non ASCII characters are discarded"""
return ''.join('{:07b}'.format(ord(ch)) for ch in text if ord(ch) < 128)
@metula
metula / trees.py
Last active August 6, 2016 13:58
import random
class TreeNode:
def __init__(self, key, value, left=None, right=None):
self.key = key
self.value = value
self.left = left
self.right = right
def __repr__(self):
def natural():
"""A generator for all natural numbers."""
n = 1
while True:
yield n
n += 1
def fib():
"""A generator for all Fibonacci numbers."""
class Node():
def __init__(self, val):
self.value = val
self.next = None
def __repr__(self):
return str(self.value)
class LinkedList():
def __init__(self):