This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(n): | |
if n>0: | |
n = n * factorial(n-1) | |
else: | |
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(n): | |
if n==0 or n==1: | |
return n | |
else: | |
return fibonacci(n-1) + fibonacci(n-2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_opposite(string): | |
pos = len(string) | |
new_string = "" | |
while pos>0: | |
pos = pos - 1 | |
new_string = new_string + string[pos] | |
return new_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def repeat_speech(n,speech): | |
while n>=1: | |
print speech | |
n = n - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(n): | |
f = 1 | |
while n >=1: | |
f = f * n | |
n = n - 1 | |
return |