본문 바로가기
CS/algorithm

1620 pokemon (나는야 포켓몬 마스터 이다솜) with Set and Map

by 빠니몽 2023. 1. 27.

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.