Skip to content

Instantly share code, notes, and snippets.

View yoavram's full-sized avatar

Yoav Ram yoavram

View GitHub Profile
def smallest_divisor(n):
div=2
while n%div!=0:
div = div+1
return div
print "Smallest divisor of",2011,"is",smallest_divisor(2011)
@yoavram
yoavram / pygame_demo.py
Created November 17, 2011 09:09
a demo of a PyGame car
# http://richard.cgpublisher.com/product/pub.84/prod.11
# INTIALISATION
import pygame, math, sys
from pygame.locals import *
TURN_SPEED = 5
ACCELERATION = 2
MAX_FORWARD_SPEED = 10
MAX_REVERSE_SPEED = 5
BG= (0,0,0)
@yoavram
yoavram / 2_cars_pygame.py
Created November 20, 2011 14:01
2 players control 2 cars and try to avoid colissions
# http://richard.cgpublisher.com/product/pub.84/prod.11
# INTIALISATION
import pygame, math, sys, random
from pygame.locals import *
TURN_SPEED = 5
ACCELERATION = 2
MAX_FORWARD_SPEED = 10
MAX_REVERSE_SPEED = 5
BG= (0,0,0)
@yoavram
yoavram / farm.py
Created November 27, 2011 15:12
An OOP example of Old McDonanld Had a Farm
class Farm:
def __init__(self):
self.animals = []
def add(self, animal):
self.animals.append(animal)
def __str__(self):
s = ''
for animal in self.animals:
@yoavram
yoavram / Multimap.py
Created November 27, 2011 15:22
A simple implementation of Multimap in Pyhthon, used as an example for OOP
class Multimap:
def __init__(self):
'''Create an empty Multimap'''
self.inner = inner
def get(self, key):
'''Return list of values associated with key'''
return self.inner.get(key, [])
def count(self, key):
@yoavram
yoavram / simple_http_server.py
Created November 27, 2011 15:56
A simple HTTP Server used to show the power of OOP and Python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from time import asctime
from socket import gethostbyname, gethostname
import cgi
# http://fragments.turtlemeat.com/pythonwebserver.php
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
the_time = asctime()
self.wfile.write('Time: '+ the_time)
@yoavram
yoavram / entry_example.py
Created December 27, 2011 12:06
An example of Swampi entry widget
from swampy.Gui import *
def go():
g.la(text=entry.get())
g=Gui()
g.title('My GUI')
entry = g.en(text="Enter text here")
button = g.bu(text="Go", command=go)
label = g.la()
@yoavram
yoavram / text_example.py
Created December 27, 2011 12:24
An example of Swampi text widget
from swampy.Gui import *
def go():
g.la(text=text.get(0.0,END))
text.delete(0.0,END)
g=Gui()
g.title('My GUI')
text = g.te(width=50, height=5)
{
"metadata": {
"name": "ma_analysis"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{