Skip to content

Instantly share code, notes, and snippets.

@markhamilton1
markhamilton1 / pi.py
Created March 23, 2014 00:48
pi is a Python script that computes each digit of the value of pi. As long as this script runs it continues to generate digits. As a matter of full disclosure, I did not write this. I am posting it here as a means of preserving the algorithm and making it available to others.
import sys
def calcPi():
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
while True:
if 4*q+r-t < n*t:
yield n
nr = 10*(r-n*t)
n = ((10*(3*q+r))//t)-10*n
q *= 10
@markhamilton1
markhamilton1 / prime.py
Created March 23, 2014 00:55
prime is a Python script that calculates prime numbers and then looks for prime types.
"""
Functions:
calc - calculate the specified number of primes starting with 2
findTypes - find the types in the current list of primes
"""
from math import sqrt
primeList = [2]
gapList = []
@markhamilton1
markhamilton1 / AlgoLinkedList.py
Created March 1, 2015 15:46
A basic linked list implementation.
import types
class Node(object):
"""
Used to hold the data used by the LinkedList class.
"""
def __init__(self, data=None, prev=None, next=None):
"""
@markhamilton1
markhamilton1 / AlgoLinkedListTests
Created March 1, 2015 15:47
Unit tests for AlgoLinkedList.py
import unittest
import AlgoLinkedList
class Test_LinkedList(unittest.TestCase):
def setUp(self):
pass
def test_list_new(self):
@markhamilton1
markhamilton1 / AlgoQueue.py
Created February 15, 2015 17:28
A simple implementation of a queue algorithm
class Element(object):
"""Used to hold a data item in the Queue."""
def __init__(self, data, prev):
"""Construct a new Element for the Queue.
data: the data to add to the Queue
prev: the previous Element in the Queue
"""
self.data = data
self.prev = prev
@markhamilton1
markhamilton1 / AlgoQueueTests.py
Created February 15, 2015 17:29
UnitTests to test the queue class.
import unittest
import AlgoQueue
class Test_AlgoQueue(unittest.TestCase):
def setUp(self):
self.queue = AlgoQueue.Queue()
@markhamilton1
markhamilton1 / AlgoStack.py
Last active August 29, 2015 14:15
A simple implementation of a stack in python
class Element(object):
"""Used to hold a data item on the Stack."""
def __init__(self, data, next):
"""Construct a new Element for the Stack.
data: the data to push onto the Stack
next: the next Element on the stack
"""
self.data = data
self.next = next
@markhamilton1
markhamilton1 / AlgoStackTests.py
Last active August 29, 2015 14:15
Unit tests for AlgoStack.py.
import unittest
import AlgoStack
class Test_AlgoStack(unittest.TestCase):
def setUp(self):
self.stack = AlgoStack.Stack()
def test_push(self):
@markhamilton1
markhamilton1 / astrodate.py
Created December 30, 2014 02:23
The date time module for my AstroScript library.
import math
import sio
import time
DATE_DELIMITER = ','
DATE_PROMPT = "Enter a date"
DATE_FORMAT = "yr, mo, dy"
DATE_HELP = "No help available."
DATE_INVALID = "The entered date is invalid!"
DATETIME_PROMPT = "Enter a date-time"
@markhamilton1
markhamilton1 / astrodatetests.py
Created December 30, 2014 02:25
Unit tests for astrodate.py.
import unittest
import astrodate
class Test_Date(unittest.TestCase):
def setUp(self):
self.lng0 = 64
self.zc0 = 4
self.dst0 = True
self.lct0 = (2012, 7, 23, 3, 37, 0, 'lct')