Problem 412220 · easy · Phase 04 Non-Linear Data Structures

Ticket Punch Count

recursion · strings · counting

A tram inspector reads a ticket as a string of symbols and wants to know how many times the punch mark ch appears. Write punch_count(s, ch) that returns the number of occurrences of the single character ch in s, using recursion rather than a loop or s.count.

Examples

Input:  s = "banana", ch = "a"
Output: 3

Input:  s = "", ch = "z"
Output: 0

Constraints

  • 0 <= len(s) <= 2000; ch is exactly one character.
  • Recursion depth is at most len(s), well under the limit.

Goals

  • Express a count as 'this character plus the count of the rest'
  • Return 0 from the empty-string base case
  • Avoid loops and built-in count helpers in favour of recursion
Starting Python…