Algorithm/COS
[COS Pro 1급, Python] 3차 2번 : 팰린드롬 문제
JMcunst
2022. 2. 25. 12:07
728x90
반응형
문제 유형
빈칸
난이도
easy
Note
1. 팰린드롬 수 : 앞뒤를 뒤집어도 똑같은 문자열
2. start_idx를 정하는 2중 for문 로직 기억.
Code
# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean
# 3차 2번
def func_a(arr, s):
return s in arr
def func_b(s):
length = len(s)
for i in range(length // 2):
if s[i] != s[length - i - 1]:
return False
return True
def func_c(palindromes, k):
palindromes = sorted(palindromes)
if len(palindromes) < k:
return "NULL"
else:
return palindromes[k - 1]
def solution(s, k):
palindromes = []
length = len(s)
for start_idx in range(length):
for cnt in range(1, length - start_idx + 1):
sub_s = s[start_idx : start_idx + cnt]
if func_b(sub_s) == True:
if func_a(palindromes, sub_s) == False:
palindromes.append(sub_s)
answer = func_c(palindromes,k)
return answer
s1 = "abcba"
k1 = 4
ret1 = solution(s1, k1)
print("solution 함수의 반환 값은", ret1, "입니다.")
s2 = "ccddcc"
k2 = 7
ret2 = solution(s2, k2)
print("solution 함수의 반환 값은", ret2, "입니다.")
※ 가끔 코드 중 print(~)가 있습니다. 정리 못한 점 죄송합니다.
728x90
반응형