Skip to content

Instantly share code, notes, and snippets.

View arakban's full-sized avatar

Arka Purkayastha arakban

  • @VenusAndMarsDating
  • London,uk
View GitHub Profile
@arakban
arakban / Code for factorial using a recursive procedure
Created March 22, 2014 19:16
Calculating a factorial using a recursive procedure
def factorial(n):
if n>0:
n = n * factorial(n-1)
else:
return 1
@arakban
arakban / Code for Fibonacci series with a recursive procedure
Created March 22, 2014 19:09
To find a number in Fibonacci series with a recursive procedure
def fibonacci(n):
if n==0 or n==1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
def fibonacci(input):
if input == 0 or input == 1:
return 1
result = 0
sequence = [0,1]
counter = 1
while counter<input:
result = sequence[1] + sequence[0]
sequence[0] = sequence[1]
sequence[1] = result
@arakban
arakban / Finding the factor
Last active August 29, 2015 13:56
This piece of code the multiples of the numbers a and b, from 0 to the number set as the limit by the user.
def multiples(limit,a,b):
n = 0
while n<limit:
if n%a == 0 and n%b == 0:
print str(n) + ' is a multiple of ' + str(a) + ' and ' + str(b)
n = n + 1
elif n % a == 0:
print str(n) + ' is a multiple of ' + str(a)
n = n + 1
elif n % b == 0:
@arakban
arakban / FizzBuzz
Created February 20, 2014 17:52
This is a program that prints the numbers from 1 to 100. But for multiples of three it prints "Fizzes" instead of the number and for the multiples of five it prints "Buzzes". For numbers which are multiples of both three and five the program prints "Fizzy Buzzy".
def fizzbuzz(limitno):
n = 1
while n<=limitno:
if n%5 == 0 and n%3 == 0:
print 'Fizzy Bizzy'
n = n + 1
elif n%3 == 0:
print 'Fizzes'
n = n + 1
elif n%5 == 0:
@arakban
arakban / Web crawler
Last active August 29, 2015 13:56
Web crawler
def compute_ranks(graph):
d = 0.8 # damping factor
numloops = 10
ranks = {}
npages = len(graph)
for page in graph:
ranks[page] = 1.0 / npages
for i in range(0, numloops):
@arakban
arakban / Make string opposite
Last active January 2, 2016 00:19
Code to turn strings the other way round
def print_opposite(string):
pos = len(string)
new_string = ""
while pos>0:
pos = pos - 1
new_string = new_string + string[pos]
return new_string
@arakban
arakban / Collatz conjecture
Created December 27, 2013 13:56
Collatz conjecture
def collatz(n):
seq = [n]
while n > 1:
if n%2 == 1:
n = n*3 + 1
seq.append(n)
else:
n = n/2
seq.append(n)
return seq
@arakban
arakban / Repeat sorry
Last active December 31, 2015 13:49
Code to say sorry 500 times
def repeat_speech(n,speech):
while n>=1:
print speech
n = n - 1
@arakban
arakban / Calculating factorial
Last active December 31, 2015 13:49
Easy code for calculating factorial in python
def factorial(n):
f = 1
while n >=1:
f = f * n
n = n - 1
return