def multiply (x, y): if(y < 0 and x > 0): return -multiply(x, -y) if(x < 0 and y > 0): return -multiply(-x, y) if(x < 0 and y < 0): return multiply(-x, -y) if(y == 0 or x == 0): return 0 else: return x + multiply(x, y - 1)
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Thursday, March 18, 2010
Multiply
Saturday, September 26, 2009
The Fibonnaci Series
Here we have a relatively fast, though memory intensive, program to find the Nth Fibonnaci Number. This uses Prof. Edsgar W Dijkstra's algorithm as described in this paper. In brief the idea is:
F(2n-1) = F(n-1)2 + F(n)2Without further ado, here we are:
F(2n) = ( 2 F(n-1) + F(n) ) F(n)
# Program to compute the Nth term of the Fibonacci series.
# Rohit Krishna Kumar
# A big list to store the numbers we have already generated
fib_list = [None] * 10**5
def fib(n):
if(n == 1):
return 1
if(n == 0):
return 0
if(fib_list[n] != None):
return fib_list[n]
# By definition: Fib(n) = Fib(n-1) + Fib(n-2)
if(fib_list[n-1] != None and fib_list[n-2] != None):
fib_list[n] = fib_list[n-1] + fib_list[n-2]
return fib_list[n]
# In Honour Of Fibonnaci. Dijikstra's method.
if(n % 2 == 0):
fib_list[n] = (2 * fib((n/2) -1 ) + fib(n/2))*fib(n/2)
return fib_list[n]
else:
fib_list[n] = ((fib(((n+1)/2) - 1))**2 + (fib((n+1)/2))**2)
return fib_list[n]
Friday, September 15, 2006
Reversing lists
There is more than one way to do a thing, and more than one language :)
Program 1
Language: Python
Program 2
Language: Python
Program 3
Language: Python
Program 4
Language: elisp (!!)
Program 1
Language: Python
def reverse(l):
if(l==[]):
return []
else:
return (reverse(l[1:])+list(a[0]))
Program 2
Language: Python
def reverse(l):
return l[::-1]
Program 3
Language: Python
a=range(10) a.reverse()
Program 4
Language: elisp (!!)
(defun consx (l x)
"like cons but first arg is a list"
(if (eq l '())
(list x)
(cons (car l) (consx (cdr l) x))))
(defun reverse(l)
"reverse a list"
(if (eq l '())
()
(consx (reverse (cdr l)) (car l))))
(reverse '(1 2 3))
Monday, September 11, 2006
The quest for the ninja monkey.
I rechristened the site name, in the hopes of reviving something which was lost. Behold for I give you the random permutation.
Language: Python
Language: Python
# Deutsche bank's online banking page uses a O(n^3) algorithm to generate a
# random character array.
# This is a O(n) way to do the same, for any array.
def randomPermute(array):
import random
for i in range(len(array)):
randIndex=random.randint(i,len(array)-1)
(array[i],array[randIndex])=(array[randIndex],array[i])
return array
# Lets experiment with the english alphabet
import string
print randomPermute(list(string.ascii_letters[:26]))
Monday, October 31, 2005
Code to HTML
This python program replaces the special HTML characters (present in code) with their HTML codes. The listing below has been made from the program itself :)
print "Enter the file to read: "
name=raw_input()
in_file=file(name,"r")
buffer=in_file.read()
buffer=buffer.replace("&","&");
buffer=buffer.replace("\"",""");
buffer=buffer.replace("<","<")
buffer=buffer.replace(">",">")
out_file=file(name+".html","w+");
out_file.write(buffer);
Subscribe to:
Posts (Atom)