Untitled
unknown
plain_text
2 years ago
1.7 kB
13
Indexable
def addition(a, b):
try:
return a+b
except TypeError:
return 'Type error!'
def get_item(lst, index):
try:
print(f"{lst[index]}\nNo error!")
except IndexError:
print("This index is not in the list!")
else:
pass
finally:
print("The finally statement has executed!")
return ''
import math
class MyException(Exception):
pass
def find_leg(lst):
try:
angle, hypotenuse = lst[0], lst[1]
except ValueError:
return 0
if angle <= 0:
raise MyException('Such angle cannot exist in a right triangle!')
elif angle >= 90:
raise MyException('Such angle cannot exist in a right triangle!')
elif hypotenuse <= 0:
raise MyException('Such hypotenuse cannot exist in a right triangle!')
else:
adjacent = math.cos(math.radians(angle)) * hypotenuse
adjacent = round(adjacent, 5)
return adjacent
def solve(inp):
data = inp.split(';')
n = int(data[0])
hierarchy = {}
for i in range(1, n+1):
pair = data[i].split(' : ')
if len(pair) == 1:
pair.append(None)
hierarchy[pair[0]] = pair[1]
m = int(data[n+1])
exceptions = data[n+2:]
alrdy_caught = set()
answer = []
for k in range(m):
exc = exceptions[k]
parents = []
parent = hierarchy[exc]
while parent is not None:
parents.append(parent)
parent = hierarchy[parent]
if (len(alrdy_caught.intersection(parents)) > 0):
answer.append(exc)
for p in parents:
alrdy_caught.add(p)
alrdy_caught.add(exc)
return ','.join(answer)
Editor is loading...