본문 바로가기
인프런 스터디

[Python] 문제1 - 암호를 해독해라!

by KAYEON 2021. 7. 6.

text = ['   + -- + - + -   ',
	'   + --- + - +   ',
        '   + -- + - + -   ',
        '   + - + - + - +   ']

for i in text:
	print(int(i.strip().replace(' ', '').replace('+', '1').replace('-', '0')))

1. 암호를 text라는 리스트로 저장

2. strip() 함수로 앞뒤 공백 제거

3. replace(' ', '') -> 중간 공백 제거

4. replace('+', '1') -> +를 1로 변경

5. replace('-', '0') -> -를 0으로 변경

 

text = ['   + -- + - + -   ',
	'   + --- + - +   ',
        '   + -- + - + -   ',
        '   + - + - + - +   ']

for i in text:
	print(int(i.strip().replace(' ', '').replace('+', '1').replace('-', '0')))
    
l = []
for i in text:
	l.append(chr(int(i.strip().replace(' ', '').replace('+', '1').replace('-', '0'), 2)))
''.join(l)

1. l이라는 빈 리스트 선언

2. int(숫자, 2) -> 2진수를 10진수로 변경

3. chr() 함수로 숫자를 문자로 변경

4. append() 함수로 l리스트에 변경된 문자 추가

>> l = ['J', 'E', 'J', 'U']

5. ''.join() 함수로 하나의 문자열로 합치기

>> l = 'JEJU'

 

 

너무 짧게 쓴 듯.., 티스토리 어렵다

 

'인프런 스터디' 카테고리의 다른 글

[Python] 재귀함수  (0) 2021.07.20
[Python] 문제3 - 섬으로 건너가라!  (0) 2021.07.19