Determine the greatest common divisor (GCD) of two numbers (ie. the largest number that divides both of them without leaving a remainder).
def gcd(a, b):
while b != 0:
t = b
b = a % b
a = t
return a
gcd(2, 4)
2
gcd(15, 25)
5
gcd(1071, 462)
21
max_height = 0
new_height = int(input('New Height?'))
while new_height > 0:
if new_height > max_height:
max_height = new_height
new_height = int(input('New Height?'))
print 'Max Height is', max_height
New Height?1 New Height?2 New Height?3 New Height?1 New Height?2 New Height?0 Max Height is 3
# Basic
all_heights = [5, 4, 7, 0, 1]
max_height = 0
for height in all_heights:
if height > max_height:
max_height = height
print 'Max Height is', max_height
Max Height is 7
# Using built-in max
all_heights = [5, 4, 7, 0, 1]
max_height = max(all_heights)
print 'Max Height is', max_height
Max Height is 7
# With input
all_heights = []
new_height = 1
while new_height > 0:
new_height = int(input('New Height?'))
all_heights.append(new_height)
max_height = max(all_heights)
print 'Max Height is', max_height
New Height?1 New Height?2 New Height?4 New Height?6 New Height?3 New Height?10 New Height?0 Max Height is 10