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 struct | |
| def display_float(x): | |
| """ prints 64 bits in the representation of the float x """ | |
| if isinstance(x, float): | |
| q, = struct.unpack('Q', struct.pack("d", x)) | |
| full_bin = "{:064b}".format(q) | |
| sign= full_bin[0] | |
| exponent_plus = full_bin[1:12] | |
| fraction=full_bin[12:] |
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
| from random import * | |
| def NR(func, deriv, epsilon=10**(-8),n=100,x0=None): | |
| """ Given a real valued func and its real value derivative, | |
| deriv, NR attempts to find a zero of func, using the | |
| Newton-Raphson method. | |
| NR starts with a an initial x0 (default value is None | |
| which is replaced upon execusion by a random number distributed | |
| uniformly in (-100.,100.)), and performs n=100 iterations. | |
| If the absolute value derivative on some x_i is smaller |
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 random | |
| def hash_mod(key, mod, func=hash): | |
| return func(key) % mod | |
| def hash_table(m): | |
| # initial hash table, m empty entries | |
| return [[] for i in range(m)] | |
| def contained(key, list_of_items): |
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
| class Node(): | |
| def __init__(self, val): | |
| self.value = val | |
| self.next = None | |
| def __repr__(self): | |
| return str(self.value) | |
| class LinkedList(): | |
| def __init__(self): |
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 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 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 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 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 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 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 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 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
| ############################################################################# | |
| ######### 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 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
| ############################################################################ | |
| ### 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. |