Skip to content

Instantly share code, notes, and snippets.

@nattybear
Created May 30, 2017 02:22
Show Gist options
  • Select an option

  • Save nattybear/008ead46294e3630107988f9a408200e to your computer and use it in GitHub Desktop.

Select an option

Save nattybear/008ead46294e3630107988f9a408200e to your computer and use it in GitHub Desktop.
dic1 = { 'zero' : 0,
'one' : 1,
'two' : 2,
'three' : 3,
'four' : 4,
'five' : 5,
'six' : 6,
'seven' : 7,
'eight' : 8,
'nine' : 9,
'ten' : 10 }
dic2 = { 0 : set('zero'),
1 : set('one'),
2 : set('two'),
3 : set('three'),
4 : set('four'),
5 : set('five'),
6 : set('six'),
7 : set('seven'),
8 : set('eight'),
9 : set('nine'),
10 : set('ten') }
def func():
q = input()
q = q.split(' ')
a = dic1[q[0]]
o = q[1]
b = dic1[q[2]]
c = q[4]
d = 0
if o == '+':
d = add(a, b)
elif o == '-':
d = sub(a, b)
elif o == '*':
d = mul(a, b)
try:
if dic2[d] == set(c): print('Yes')
else: print('No')
except KeyError:
print('No')
def add(a, b):
return a + b
def sub(a, b):
return a - b
def mul(a, b):
return a * b
cnt = int(input())
for i in range(cnt):
func()
@nattybear

Copy link
Copy Markdown
Author

전체적으로 파이썬 딕셔너리 자료형을 이용함

  • 문자열에 해당하는 숫자를 매칭시켜야 하기 때문에 키와 값의 쌍으로 구성되어 있는 딕셔너리를 사용하기로 결정

아나그램 해결을 위해서 파이썬 SET 자료형을 이용

  • SET 자료형은 중복을 허용하지 않고 순서가 없기 때문에 자연스럽게 아나그램으로 사용이 가능함

KeyError를 이용해서 예외처리

  • 연산의 결과가 0보다 작거나 10보다 큰 경우에는 딕셔너리에 해당하는 키가 없기 때문에 KeyError가 발생하는 것을 역으로 이용함

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment