Question 1
Solution:
def count_occur(s, c):
"""
Input: string s, character c
Output: integer count of the number of times c occurs in s.
"""
if len(s) == 0:
return 0
elif s[0] == c:
return 1 + count_occur(s[1:], c)
else:
return count_occur(s[1:], c)
Rubric:
- 0 mark is given if Incorrect/ Not Attempted/ used loop / used build-in function.
- 1 mark is given if the base case is correct.
- 2 marks are given if the correct recursive step is written when the character matches the character at the start/end of the current string.
- 2 marks are given if the correct recursive step is written when the character does not match the character at the start/end of the current string.
Question 2
Solution:
Set 1
Set 2
Rubrics
- 1 point will be provided if A is correct. A bonus of 0.5 points will be provided if A = 2.
- 1 point for correct B.