
import sys
input = sys.stdin.readline
def recursion(s, l, r):
global cnt
cnt += 1
if l >= r: return 1
elif s[l] != s[r]: return 0
else: return recursion(s, l+1, r-1)
def isPalindrome(s):
return recursion(s, 0, len(s)-1)
for i in range(int(input())):
cnt = 0
word = input().rstrip()
print(isPalindrome(word),cnt)
global을 이용해서 전역변수로 만들어주기!
global을 이용하지 않고 풀었을 때는
import sys
input = sys.stdin.readline
def recursion(s, l, r):
if l >= r: return 1,l
elif s[l] != s[r]: return 0,l
else: return recursion(s, l+1, r-1)
def isPalindrome(s):
return recursion(s, 0, len(s)-1)
for i in range(int(input())):
word = input().rstrip()
print(isPalindrome(word)[0],isPalindrome(word)[1]+1)
이와 같이 풀었다.
| [파이썬/python] 백준 4779번 : 칸토어 집합 (🥈3) (0) | 2023.06.10 |
|---|---|
| [파이썬/python] 백준 1541번 : 잃어버린 괄호 (🥈2) (0) | 2023.06.04 |
| [파이썬/python] 백준 28135번 : Since 1973 (🥉3) (0) | 2023.06.02 |
| [파이썬/python] 백준 1021번 : 회전하는 큐 (🥈3) (0) | 2023.05.31 |
| [파이썬/python] 백준 1966번 : 프린터 큐 (🥈3) (0) | 2023.05.31 |