Created
December 3, 2014 17:10
-
-
Save dlt/25a5ef245b8bf3a64ee5 to your computer and use it in GitHub Desktop.
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
print "oi" | |
def square(n): | |
return n * n | |
def abs(n): | |
if n > 0: | |
return n | |
elif n < 0: | |
return -n | |
elif n == 0: | |
return n | |
print "abs:" | |
print(abs(5)) | |
print(abs(-5)) | |
print(abs(0)) | |
def greater_or_equal(x, y): | |
return x > y or x == y | |
print greater_or_equal(4, 4) | |
def mult(n, times): | |
total = 0 | |
while times > 0: | |
total = n + total | |
times = times - 1 | |
return total | |
print "mult(2, 5): " | |
print mult(2, 5) | |
def mult2(n, times): | |
if times == 1: | |
return n | |
else: | |
return n + mult2(n, times - 1) | |
print "mult2:" | |
print mult2(2, 5) | |
def rest(l): | |
return l[1:] | |
def is_member(el, numbers): | |
is_in_list = False | |
for n in numbers: | |
if n == el: | |
is_in_list = True | |
return is_in_list | |
print "test is_member:" | |
print is_member(5, [1, 2, 3]) | |
print is_member(5, [1, 2, 3, 5]) | |
def is_member_recur(el, numbers): | |
if len(numbers) == 0: | |
return False | |
else: | |
if el == numbers[0]: | |
return True | |
else: | |
return is_member_recur(el, rest(numbers)) | |
print "test is_member_recur:" | |
print is_member_recur(5, [1, 2, 3]) | |
print is_member_recur(5, [1, 2, 3, 5]) | |
def count_member(n, li): | |
counter = 0 | |
for el in li: | |
if el == n: | |
counter = counter + 1 | |
return counter | |
print 'count_member:' | |
print count_member(1, [1, 2, 3, 1, 2, 3, 4, 5, 6, 2, 1, 1]) | |
# Quarta aula | |
def remainder(n, m): | |
return n % m | |
def average(a, b): | |
return (a + b) / 2.0 | |
def improve(guess, x): | |
return average(guess, (x / guess)) | |
def good_enough(guess, x): | |
return abs(square(guess) - x) < 0.00000001 | |
def sqrt(x): | |
def sqrt_iter(guess, x): | |
if good_enough(guess, x): | |
return guess | |
else: | |
return sqrt_iter(improve(guess, x), x) | |
return sqrt_iter(1.0, x) | |
print sqrt(9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment