This file contains 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
# 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]: |
This file contains 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 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: ") |
This file contains 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 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: |
This file contains 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
############################################################################ | |
### 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. |
This file contains 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
############################################################################# | |
######### 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): |
This file contains 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 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]. |
This file contains 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 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) |
This file contains 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 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): |
This file contains 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 natural(): | |
"""A generator for all natural numbers.""" | |
n = 1 | |
while True: | |
yield n | |
n += 1 | |
def fib(): | |
"""A generator for all Fibonacci numbers.""" |
This file contains 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
class Node(): | |
def __init__(self, val): | |
self.value = val | |
self.next = None | |
def __repr__(self): | |
return str(self.value) | |
class LinkedList(): | |
def __init__(self): |
NewerOlder