Python Functions and Errors: Time, Triangle, Bonus, and Digits
Classified in Computers
Written on in English with a size of 4.09 KB
TIME DIFFERENCE
def time_difference(time1, time2):
time1=time_to_seconds(time1)
time2=time_to_seconds(time2)
diffinsecond=time2-time1
hours=diffinsecond//3600
minutes=(diffinsecond-(hours*3600))//60
seconds=diffinsecond-(hours*3600)-(minutes*60)
return(make_time_string(hours,minutes,seconds))
# Predefined helper functions. Do not edit them.
def time_to_seconds(time):
x = list(map(int, time.split(":"))
return x[0] * 3600 + x[1]*60 + x[2]
def make_time_string(hours, mins, seconds):
return "{:02d}:{:02d}:{:02d}".format(hours, mins, seconds)
TYPE OF TRIANGLE
def triangle(side1, side2, side3):
if side1+side2<>
return "Not a triangle"
elif side1==side2 and side2==side3:
return "Equilateral"
elif side1==side2 or side2==side3 or side1==side3:
return "Isosceles"
else:
return "Scalene"
33 to 40 days SGD$325 per Billable Day
def bonus(days):
if days<>
return 0
elif days<>
return (days-32)*325
elif days<>
return (days-40)*550+(325*8)
else:
return 7000+(days-48)*600
GET THE NTH DIGIT FROM STRING K AND MUST COUNT N FROM BACK
def get_nth_digit(k, n):
k=str(k)
return int(k[::-1][n-1])
def m(lst1, lst2):
a = [1, [2, 3]]
b = a[::-1] # slice from back to front
a[1][0], a[1][1] = b[0][1], b[0][0]
L={'a':5,'b':3}for i in L: if L[i]==max(L.values()):
print(i) (this is to find the corresponding key for max value)
{'a': 5, 'b': 3, 'c': 78}
LITTLE MOCK EXAM
2)