happy coding

[level 0] 암호 해독 본문

coding study/programmars

[level 0] 암호 해독

yeoonii 2024. 5. 27. 01:08

문제 설명

군 전략가 머쓱이는 전쟁 중 적군이 다음과 같은 암호 체계를 사용한다는 것을 알아냈습니다.

  • 암호화된 문자열 cipher를 주고받습니다.
  • 그 문자열에서 code의 배수 번째 글자만 진짜 암호입니다.

문자열 cipher와 정수 code가 매개변수로 주어질 때 해독된 암호 문자열을 return하도록 solution 함수를 완성해주세요.


제한사항
  • 1 ≤ cipher의 길이 ≤ 1,000
  • 1 ≤ code  cipher의 길이
  • cipher는 소문자와 공백으로만 구성되어 있습니다.
  • 공백도 하나의 문자로 취급합니다.

def solution(cipher, code):
    ans = ''
    leng = len(cipher) // code
    for i in range(1,leng+1):
        j = code * i
        ans = ans + cipher[j-1]
    return ans

 

다른 사람 풀이

def solution(cipher, code):
	return cipher[code-1::code]
def solution(cipher, code):
    answer = ''
    for i in range(len(cipher)):
        if (i+1) % code == 0:
            answer += cipher[i]
    return answer
def solution(cipher, code):
    return ''.join(cipher[i] for i in range(code-1,len(cipher),code))

'coding study > programmars' 카테고리의 다른 글

[level 0] 배열의 유사도  (0) 2024.05.27
[level 0] 대문자와 소문자  (0) 2024.05.27
[level 0] 숨어 있는 숫자의 덧셈(1)  (0) 2024.05.26
[level 0] 최댓값 만들기  (0) 2024.05.26
[level 0] 문자열 뒤집기  (0) 2024.05.26
Comments