티스토리 뷰

Algorithm/noj.am

[Python] 백준 1076번 - 저항

SweetDev 2020. 12. 31. 20:12
class Color(object):
    def __init__(self, name, value, multiply):
        self.name = name
        self.value = value
        self.multiply = multiply


colors = [Color("black", 0, 1), Color("brown", 1, 10), Color("red", 2, 100), Color("orange", 3, 1000),
          Color("yellow", 4, 10000), Color("green", 5, 100000), Color("blue", 6, 1000000), Color("violet", 7, 10000000),
          Color("grey", 8, 100000000), Color("white", 9, 1000000000)]

Myinput = []
output = ""

for i in range(3):
    lastInput = input()
    Myinput.append(lastInput)
    match = next(filter(lambda x: x.name == lastInput, colors))
    if i == 0 or i == 1:
        # 첫번째, 두번째 숫자 붙이기
        output = output + "%s" % match.value
    else:
        # 곱하기
        output = int(output) * match.multiply

print(output)

 

근데 딕셔너리로 하는게 훨씬 깔끔했을 것 같다. 딕셔너리는 색깔 이름이 접근자인데 (dic["black"] 이게 됨) class로 만들어서 filter를 한번 더 해줘야 했다 ㅠ

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함