Python Functions and Errors: Time, Triangle, Bonus, and Digits

Classified in Computers

Written at on 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])

more([1,2,3,4,5]) -->[1,3,6,10,15]
def more(lst):
newlist=[]
tmp=0
tmp=tmp+num
newlist.append(tmp)
return newlst

def m(lst1, lst2):

l = [ ]
for i in lst2:
l = lst1.append(i)
return l
print(m([1, 2, 3], [4, 5, 6])) List.append returns None


a = [[1, 2], [[3, 4], 5]]
b = a.copy()
a[0][1], b[1][0][0] = b[1][0], a[1][1]
print(a) -->[[1, [5, 4]], [[5, 4], 5]] (a and b will be same because deep copy and apparently sb will also cause a to change. do the 2nd part first and change b first, then do first part and change a because b affects a.


a = [1, [2, 3]]
b = a[::-1] # slice from back to front
a[1][0], a[1][1] = b[0][1], b[0][0]

print(a, b) -->[1, [3, 2]] [[3, 2], 1] since b is taken from a, do the step 2 last?
max({'a':4,'b':2}) will be max of the key!!! which is 'b'


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}

print(L.pop('c')) --> 78


class Minion(): class OneEyeMinion(Minion):
def __init__(self, name, num_eyes): def __init__(self, name):
self.name = name super().__init__(name, 1)
self.num_eyes = num_eyes


syntax error(While True print('hi')) no colon after True
zerodivision error (you divide by 0)
name error (you didnt define hi or spam when u used it)
typeerror (adding a int to string)
valueerror(problem with the object you try to assign a value to) (int(one))
index error(out of range in a list)(L=[3,4] and you do L[2]

LITTLE MOCK EXAM

1)n = int(input())
i = 1
while i<>
if (i%2==0): when the number is 321, count of evens will be 321//2=160. so the number of count of evens equal to the number of times you go through BUT BE CAREFUL WHETHER i IS 0 OR 1
if (i%4==0): the number will be evens divide by 2 i.e. 160/2=80
if (i % 3 == 0): 0 1 3 5 7 9 11 13 15 17 19 21 since 0 wasnt included, total numbers divisible by 3 is odds(321-160=161) minus 2(the 1 and 3) ÷3 then the answer +1 for the 3

2)

Entradas relacionadas: