import math
def approxpi():
besta = 0
bestb = 0
bestdif = 1000000
for a in range(1, 100):
for b in range(1, 100):
diff = abs(a/b-math.pi)
if diff < bestdif:
besta = a
bestb = b
bestdif = diff
print("The best approximation is ", besta ,"/", bestb, "with a difference of",
bestdif)
def approxe():
besta = 0
bestb = 0
bestdif = 1000000
for a in range(1, 100):
for b in range(1, 100):
diff = abs(a/b-math.e)
if diff < bestdif:
besta = a
bestb = b
bestdif = diff
print("The best approximation is ", besta ,"/", bestb, "with a difference of",
bestdif)
def piapp(n):
accu = 0
for k in range(0, n+1):
num = (-1/3)**k
den = 2*k+1
accu += num/den
return math.sqrt(12)*accu
def compare_pi_app():
print("True value is", math.pi)
for n in [5, 10, 20, 30]:
print("Approx with", n, "is", piapp(n))
def double_factorial(k):
accu = 1
for i in range(1, 2*k, 2):
accu *= i
return accu
def even_vs_odd(lista):
count_even = 0
count_odd = 0
for i in lista:
if i%2 == 0:
count_even += 1
else:
count_odd += 1
return count_even < count_odd
def variance(lista):
mean = sum(lista)/len(lista)
accu = 0
for element in lista:
accu += (element-mean)**2
return accu
def my_max(lista):
largest_seen = lista[0]
for element in lista:
if element > largest_seen:
largest_seen = element
return largest_seen
def range_of(lista):
largest_seen = lista[0]
smallest_seen = lista[0]
for element in lista:
if element > largest_seen:
largest_seen = element
if element < smallest_seen:
smallest_seen = element
return largest_seen - smallest_seen
def cons_over_vows(word):
count_cons = 0
count_vowels = 0
for element in word:
if element in "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXyYzZ":
count_cons += 1
elif element in 'aAeEiIoOuU':
count_vowels += 1
return count_vowels/count_cons
def s_z(word):
result = []
for letter in word:
if letter == 's':
result.append('z')
elif letter == 'z':
result.append('s')
else:
result.append(letter)
return ''.join(result)
def most_frequ_vowel(word):
count_a = 0
count_e = 0
count_i = 0
count_o = 0
count_u = 0
for letter in word:
if letter in 'aA':
count_a +=1
elif letter in 'eE':
count_e += 1
elif letter in 'oO':
count_o += 1
elif letter in 'iI':
count_i += 1
elif letter in 'uU':
count_u +=1
#Now we determine the winners
if count_a >= count_e and count_a >= count_i and count_a >= count_o and count_a >= count_u:
return 'a'
if count_a < count_e and count_e >= count_i and count_e >= count_o and count_e >= count_u:
return 'e'
if count_i > count_a and count_i > count_e and count_i >= count_o and count_i >= count_u:
return 'i'
if count_o > count_a and count_o > count_e and count_o > count_i and count_o >= count_u:
return 'o'
return 'u'