(a + b == c or a + c == b or b + c == a)를 제일 짧게 써주세요
조회수 2037회
발생하는 문제 및 실행환경
a + b == c or a + c == b or b + c == a
제일 짧은 코드로 표현하려고 합니다.
제가 생각할 수 있는건 여기까지가 한계인데 근데 제가 짠게 맞긴 한건지 좀 불안하기도하고 그래서 더 좋은 방법이 있을지 궁금합니다
소스코드
a + b + c in (2*a, 2*b, 2*c)
1 답변
-
제가 추천하는 건
a + b == c or a + c == b or b + c == a
를 그냥 그대로 쓰는 겁니다.저 코드는 짧지만 pythonic 코드가 아니기 때문입니다. python zen을 보면(this 모듈에 있습니다)
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
$ 파이썬 코드는 간단하고, 간결하고, 설명하기 쉬워야 합니다.
a + b + c in (2*a, 2*b, 2*c)
가 맞는진 확인하지 않았으나, 설령 맞다고 하더라도 다른 사람이 이 코드를 보고 뭘 하는건지 이해할 수 있을까요?짧은 코드가 파이썬에 장점이지만, 그렇다고 해서 너무 길이에 집착하지 않아도 됩니다.
댓글 입력