Skip to content

Instantly share code, notes, and snippets.

@samrat
samrat / Physics problem Solver
Created December 1, 2010 09:04
Physics Problem Solver
'''Physics problem Solver
Finds Distance, Time or Speed according to input
'''
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
@samrat
samrat / tictac.py
Created March 24, 2011 18:23
tic tac toe
grid= [['','',''],
['','',''],
['','','']]
def x(y,x):
'''Player X's turn
'''
if not grid[y][x]:
grid[y][x]='x'
if check_status(grid)!=None:
#all the imports
import sqlite3
from flask import Flask, request,session, g, redirect, url_for, abort, render_template, flash
from contextlib import closing
#configuration
DATABASE= 'tmp/flaskr.db'
DEBUG= True
SECRET_KEY= 'development key' ##more difficult
USERNAME= 'admin'
@samrat
samrat / HN_grab.py
Created April 21, 2011 16:59
Grabs headlines and their links from Hacker news
import urllib2
from BeautifulSoup import BeautifulSoup
HN_url = "http://news.ycombinator.com"
def get_page():
page_html = urllib2.urlopen(HN_url)
return page_html
def get_stories(content):
@samrat
samrat / NaivePrime.py
Created July 2, 2011 16:21
a naive approach to prime number generation
def naiveprime(n):
primelist = []
for num in range(int(n+1)):
prime= True
for i in range(2,int(num)):
if (num%i)==0:
prime=False
break
if prime==True: primelist.append(num)
@samrat
samrat / SieveEratosthenes.py
Created July 2, 2011 16:22
Implemented with a bit of help from the Wikipedia page ;) Runs much faster than the naive approach.
def sieve(n):
"""Finds all prime numbers upto n
"""
primelist = []
intlist = list(range(int(n+1)))
fin = int(n**0.5)
for i in range(2,fin+1):
intlist[2*i::i] = [None] * len(intlist[2*i::i])
@samrat
samrat / insertion_sort.py
Created July 3, 2011 08:16
Insertion sort implementation in Python(probably not very efficient)
unsorted_list=[45,99,1,-22,7,3,294,10,36]
def insertion_sort(unsorted):
for i in range(1,len(unsorted)):
for j in range(i):
if unsorted[i]<unsorted[j]:
unsorted[i], unsorted[j] = unsorted[j], unsorted[i]
print unsorted
return unsorted
@samrat
samrat / quicksort.py
Created July 12, 2011 13:04
Crude implementation of Quicksort algorithm in Python.
def quicksort(list):
if len(list) <= 1: return list
pivot = list[0]
great = []
less = []
for item in list[1:]:
if item>=pivot:
great.append(item)
elif item<pivot:
function quicksort(a) {
if (a.length == 0) return [];
var great = [], less = [], pivot = a[0];
for (i=1; i<a.length; i++){
if (a[i]>pivot)
great.push(a[i]);
else {
@samrat
samrat / trapezium.py
Created August 27, 2011 07:40
Definite integration using the trapezium rule
from __future__ import division
def trapezium(f, n, a, b):
'''
f- function [f(x)]
n- number of trapeziums
a- lower limit
b- upper limit
Returns definite integral of f(x) from range a to b
'''