2023-01-27
0. Background
This problem is included in Set and Map category. Through the several time of try, there were some things I wanted to write down.
There are more things you can learn and I leared. If you want more about this, visit the link.
https://blog.loginradius.com/identity
Identity | LoginRadius Blog
Identity Blog is a place for developers to share their expertise, find solutions for development problems, and become more efficient.
blog.loginradius.com
1. Try-Except vs If Else
1-1. Code before getting rid of Try-Except
There wasn't that big difference except for existence of Try-Except. Let me show you my code.
n, m = list(map(int, input().split(" ")))
result = []
d_key = {}
d_val = {}
for i in range(1,n+1):
s = input()
d_key[s] = i
d_val[i] = s
t = (input() for i in range(m))
for i in t:
try:
num = int(i)
result.append(d_val[num])
except:
result.append(d_key[i])
for i in result:
print(i)
1-2. Code afterawards
n, m = list(map(int, input().split(" ")))
result = []
d_key = {}
d_val = {}
for i in range(1,n+1):
s = input()
d_key[s] = i
d_val[i] = s
t = (input() for i in range(m))
for _ in range(m):
i = input()
if i.isdigit():
result.append(d_val[int(i)])
else:
result.append(d_key[i])
for i in result:
print(i)
1-3. Why did the first code throw timeout failure?
1-3-1. Is Try-Except slower than If Else?
The answer is, NO. Try-Except is fater than If Else statement. Here is the other link I got some info about this from.
https://www.geeksforgeeks.org/try-except-vs-if-in-python/
try-except vs If in Python - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
In the web-page, the person said Try-Except statement is fater till it meets with an exception. In the other word, handling exceptions takes more time than If statement.
1-3-2. Why is it like that?
Because most of people follows EAFP Python style (Easier to Ask for Forgiveness than Permission) as Python recommends it, which means:
EAFP, This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.
LBYL: Look Before You Leap
2. Conclusion
In the previous code, I intentionally used Try-Except on my code to throw errors and to distingish with them if a data is digit or not as I did not come up with isdigit() function. As you see, the code must've run into a lot of exceptions and risen a number of timeout. To sum up, use of Try-Exception should be only when it would meet with the tiny amount of exception if you want to speed your code up.
'CS > algorithm' 카테고리의 다른 글
18258 Queue2(큐2) with Python3 (0) | 2023.02.24 |
---|---|
2839 Sugar Delivery (설탕배달) with Greedy Algorithm (0) | 2023.02.14 |
10815_숫자 카드(Cards) with Set and Map (0) | 2023.01.24 |
19532 수학은 온라인 수업입니다 with 완전 탐색(Brute Force) 알고리즘 (0) | 2021.09.27 |
10818 최소, 최소값 구하기 with python3 (0) | 2021.01.28 |