Skip to content

Instantly share code, notes, and snippets.

@libswan
libswan / L12 Problem 7 - V1
Created April 11, 2013 08:40
V1. L12 Problem 7 - "present an angle of the data (words.txt) that you find interesting".
import random
import string
import pylab
WORDLIST_FILENAME = "words.txt"
def loadWords():
print "Loading word list from file..."
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0)
@libswan
libswan / L12 Problem 7 - V2
Last active December 16, 2015 02:19
V2. Attempt at converting into Classes. L12 Problem 7 - "present an angle of the data (words.txt) that you find interesting".
import random
import string
import pylab
WORDLIST_FILENAME = "words.txt"
def loadWords():
print "Loading word list from file..."
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0)
@libswan
libswan / gist:5376875
Last active December 16, 2015 04:28
L13 Problem 7
"""
This code will produce "The output of random.randint(1, 10) after a specific seed is shown below."
"""
import random
random.seed(9001)
print random.randint(1, 10)
print random.randint(1, 10)
print random.randint(1, 10)
print random.randint(1, 10)
print random.randint(1, 10)
@libswan
libswan / L15 Problem 5
Last active December 16, 2015 11:09
L15 Problem 5
import pylab
"""Rows 4 to 13 copied from l15-3.py - available in the lecture."""
#set line width
pylab.rcParams['lines.linewidth'] = 6
#set font size for titles
pylab.rcParams['axes.titlesize'] = 20
#set font size for labels on axes
pylab.rcParams['axes.labelsize'] = 20
#set size of numbers on x-axis
@libswan
libswan / gist:5494444
Last active December 16, 2015 20:39
PS9
# 6.00 Problem Set 9
import numpy
import random
import pylab
from ps8b_precompiled_27 import *
#
# PROBLEM 1
#
import pylab
import random
def LV(numBalls, numTrials):
finalSteps = []
for i in range(numTrials):
ballList = []
steps = 0
for i in range(numBalls):
i = random.choice(['white', 'black'])
import pylab
import random
def MC(numBalls, numTrials):
ballsStudiedList = []
for i in range(numTrials):
ballList = []
ballListCopy = []
@libswan
libswan / M2-P6-Incorrect
Last active December 17, 2015 05:39
My code. This didn't work.
def rabbitGrowth():
"""
rabbitGrowth is called once at the beginning of each time step.
It makes use of the global variables: CURRENTRABBITPOP and MAXRABBITPOP.
The global variable CURRENTRABBITPOP is modified by this procedure.
For each rabbit, based on the probabilities in the problem set write-up,
a new rabbit may be born.
@libswan
libswan / M2-P6-Correct
Created May 11, 2013 13:12
Correct code from kiwitrader
def rabbitGrowth():
"""
rabbitGrowth is called once at the beginning of each time step.
It makes use of the global variables: CURRENTRABBITPOP and MAXRABBITPOP.
The global variable CURRENTRABBITPOP is modified by this procedure.
For each rabbit, based on the probabilities in the problem set write-up,
a new rabbit may be born.
@libswan
libswan / M2-P6-Correct
Created May 12, 2013 09:01
Correct code from papashangu
def rabbitGrowth():
global CURRENTRABBITPOP
global MAXRABBITPOP
num = CURRENTRABBITPOP
for i in xrange(num):
if random.random() < (1 - (float(CURRENTRABBITPOP) / MAXRABBITPOP)):
CURRENTRABBITPOP += 1
def foxGrowth():
global CURRENTRABBITPOP
global CURRENTFOXPOP