Python 기본 사항중간

재귀함수를 이용하여 문자열의 길이를 계산하는 프로그램

'재귀를 사용하여 문자열 길이를 계산하는 프로그램' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

중간

재귀를 사용하여 문자열 s의 길이를 계산하고 반환하는 함수 string_length(s)을 작성하세요. 내장된 len() 함수를 사용하면 안 됩니다. 기본 사례는 길이가 0인 빈 문자열입니다. 각 재귀 호출에 대해 한 문자를 계산하고 나머지 문자열에 대해 재귀합니다.

제약
  • 0 <= len(s) <= 1000
  • s contains only printable ASCII characters

Example 1
Input
s = 'hello'
Output
5
Explanation

The string 'hello' has 5 characters: h, e, l, l, o.

Example 2
Input
s = 'python'
Output
6
Explanation

The string 'python' has 6 characters.

Example 3
Input
s = ''
Output
0
Explanation

An empty string has length 0.

Need a Hint?
세트나 힙과 같은 재귀 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.