Implement a function that calculates the number of penalty score occurrences in a given string. A penalty score is counted whenever the substring "21" appears in the string sequentially.
Input/Output Requirements:
Input: The function penalty_score(S: str) -> int takes a single argument:
S: A string consisting of any characters. The length of the string will be at least 0.
Output: The function returns an integer representing the number of times the substring "21" occurs in the string.
Implementation Details:
You should implement the function penalty_score as specified in the function signature.
The implementation should correctly iterate through the string, checking each character and its subsequent character to detect the substring "21".
Ensure that the solution is able to handle an empty string input by returning 0.
File Structure:
The solution should be written in a file called /app/solution.py. The file should include the function definition with necessary imports if required.
Example:
python
penalty_score("2211221") # This should return 2
In the example above, the function finds the substring "21" twice.