Skip to content

Instantly share code, notes, and snippets.

View Isweet's full-sized avatar

Ian Sweet Isweet

  • College Park, MD
View GitHub Profile
@Isweet
Isweet / ecc_examples.py
Created May 12, 2014 04:16
Everyone Can Code! Tutorial Script
import random
import turtle
# 1. Hello World!
# This is designed as a first introduction to the language.
# One print statement and done!
def print_hello_world():
print "Hello, World!"
@Isweet
Isweet / triples.py
Last active August 29, 2015 14:06
Generator for all pythagorean triples s.t. r < bound
# Generator for all pythagorean triples s.t. r < bound
def pyth_triples(bound):
for r in range(2, bound):
for s in range(1, r):
yield [r*r - s*s, 2*r*s, r*r + s*s]
@Isweet
Isweet / phi.py
Created November 3, 2014 01:39
Cleaner Euler Phi Function
import collections
def next_div(n):
for i in xrange(2, n):
if (n % i == 0):
return i
return n
# d | n, d is prime
@Isweet
Isweet / prime_gen.py
Created November 17, 2014 23:43
Generate an arbitrary number of primes
import itertools
def prime_gen():
yield 2
curr = 3
seen = [2]
while (True):
yield curr
@Isweet
Isweet / prisoners.py
Last active August 29, 2015 14:16
A solution to the 100 prisoners problem. Directions for trying out your own strategy are included.
#!/usr/bin/python
# For more information: http://www.cut-the-knot.org/Probability/LightBulbs.shtml
# To implement your own strategy, simply implement a Prisoner
# class which has a single method visit_room() which returns boolean
# according to the answer provided to the warden.
# Global variables n (number of prisoners for simulation) and
# lightbulb (the lightbulb where True is on and False is off)
@Isweet
Isweet / rederiv.hs
Last active August 29, 2015 14:20
Super toy implementation of regular expression derivatives
data Lang = DEmpty | DEps | DChar String | DCat Lang Lang | DAlt Lang Lang | DRep Lang
stringOfLang l = case l of
DEmpty -> "DEmpty"
DEps -> "DEps"
DChar x -> "DChar (" ++ x ++ ")"
DCat lx ly -> "DCat (" ++ (stringOfLang lx) ++ ", " ++ (stringOfLang ly) ++ ")"
DAlt lx ly -> "DAlt (" ++ (stringOfLang lx) ++ ", " ++ (stringOfLang ly) ++ ")"
DRep lx -> "DRep (" ++ (stringOfLang lx) ++ ")"
@Isweet
Isweet / func_comp.ml
Created October 6, 2015 21:22
Functions as values in OCaml
let comp = fun f -> (fun g -> (fun x -> f (g x)));;
let doublesquare = comp (fun x -> x + x) (fun y -> y * y);;
print_int (doublesquare 5); print_newline ();;
(* expect 50 *)
@Isweet
Isweet / ternary.sh
Created October 22, 2015 00:56
A powerful demonstration of the ternary operator!
#!/bin/bash
cat << EOF > ./ternary.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
int a, b, c, d, e, f, g;
@Isweet
Isweet / fib.hs
Created November 13, 2015 23:37
A couple different Fibonacci's
import Data.Function.Memoize
-- SLOW
fib :: Int -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
-- Memoized / FAST
fib' :: Int -> Integer
@Isweet
Isweet / haskell-ex.sh
Last active December 30, 2015 05:04
Baby Haskell examples for Sean
#! /bin/bash
cat << EOF > ./RandAscii.hs
module Main (main) where
import Control.Monad
import System.Random
import qualified Data.Char as Char