[해결]
기본 틀은 다 나와있어서 껌이라고 생각했지만.. recursion 호출 횟수를 어떻게 구함?!!
>> 전역변수를 쓰면 된다
주의할 점은 global 선언과 할당은 동시에 할 수 없다. global count=0 이렇게 하니 에러남
def recursion(string,l,r):
global count #함수 내에서 선언한 변수는 원래 기본적으로 local이지만, global로 쓰겠다.
count+=1 #global 변수 count
if l>=r:
return 1
elif string[l]!=string[r]:
return 0
else:
return recursion(string,l+1,r-1)
def isPalindrome(string):
return recursion(string,0,len(string)-1)
import sys
input = sys.stdin.readline
n=int(input().rstrip())
for i in range(n):
count=0 #global변수, 매번 0으로 바꿔주고 호출횟수 카운트
string=input().rstrip()
print(isPalindrome(string), count)
참고
https://lets-hci-la-ai-withme.tistory.com/68
[파이썬] global, unlocal / 전역변수, 지역변수
Global variable: 실행하는 파이썬 파일 전체 영역에서 사용 가능한 변수 Local variable: 특정 지역 범위에서만 영향을 주고 받을 수 있는 변수 (ex. 특정 함수 내에서 선언한 변수는 return되지 않는 한 함
lets-hci-la-ai-withme.tistory.com
'문제 풀이 > 백준' 카테고리의 다른 글
[백준] 11724번 연결 요소의 개수 (0) | 2024.07.10 |
---|---|
[백준] 10997번 별찍기 22 (0) | 2024.07.09 |
[백준] 17478번 재귀함수가 뭔가요 (1) | 2024.07.05 |
[백준] 17413번 단어 뒤집기2 (0) | 2024.07.04 |
[백준] 10799번 쇠막대기 (0) | 2024.07.03 |
댓글