RUBY
리스트 reverse() 함수(파이썬) 본문
reverse() 함수
reverse() 함수를 이용하면 아이템을 순서를 뒤집을 수 있다.
다음은 전쟁에서 사용되는 암호이다. 암호를 해독하는 프로그램을 만들어보자.
* 먼저 암호의 규칙을 알아보자
→ 2 * 7 = 14
→ 1 * 5 = 5
→ 6 * 2 = 12
→ 3 * 1 = 3
즉,
14는 2 * 7
5는 1 * 5
12는 6* 2
3은 3* 1
뒤부터 적어나가면 13326125157214의 순으로 적을 수 있다.
secret = '27156231'
secretList = []
solvedList = ''
for cha in secret:
secretList.append(int(cha))
secretList.reverse()
val = secretList[0] * secretList[1]
secretList.insert(2, val)
val = secretList[3] * secretList[4]
secretList.insert(5, val)
val = secretList[6] * secretList[7]
secretList.insert(8, val)
val = secretList[9] * secretList[10]
secretList.insert(11, val)
print(secretList)
[1, 3, 3, 2, 6, 12, 5, 1, 5, 7, 2, 14]
'프로그래밍 언어 > Python' 카테고리의 다른 글
리스트 곱셈연산, index(), count(), del 키워드(파이썬) (0) | 2023.01.23 |
---|---|
리스트 슬라이싱[n:m](파이썬) (0) | 2023.01.23 |
리스트 sort() 함수(파이썬) (0) | 2023.01.23 |
리스트 extend() 함수 (파이썬) (0) | 2023.01.22 |
리스트 pop() 함수(파이썬) (0) | 2023.01.22 |
Comments