Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Archives
Today
Total
관리 메뉴

RUBY

리스트 reverse() 함수(파이썬) 본문

프로그래밍 언어/Python

리스트 reverse() 함수(파이썬)

ruby-jieun 2023. 1. 23. 00:17

 

 

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]

 

 

Comments