Number of Senior Citizens
Approach
- Iterate through the
details
array: You will go through each string in the array. - Extract the age from each string: The age of a person is represented by the characters at indexes 11 and 12 (0-based) in each string.
- Count the passengers older than 60: If the extracted age is greater than 60, you increment a counter.
- Return the counter: The final counter will be the number of passengers who are strictly more than 60 years old.
Code
Python solution:
|
|
Explanation
- We use string slicing to extract the age part from each string (i.e.,
detail[11:13]
), which represents the 11th and 12th characters in the 0-indexed string. - We convert this substring to an integer and compare it with 60 to decide whether the passenger is older than 60.
- We use a counter to keep track of the number of passengers who fulfill this condition, and in the end, return this count.