algoadvance

Given a list of strings, where each string represents information about a person in the format “Name, Age”. You are required to count the number of senior citizens in the list. A senior citizen is defined as a person whose age is 60 or above.

Clarifying Questions

  1. Input Format:
    • Is the input strictly a list of strings where each string is in the format “Name, Age”?
    • Can there be any invalid entries (e.g., wrong format, missing information)?
  2. Output:
    • Should the function return only the count of senior citizens as an integer?

For the sake of this problem, we’ll make the following assumptions:

Strategy

  1. Parse each string to extract the age.
  2. Convert the age to an integer.
  3. Count the number of entries where the age is 60 or more.
  4. Return the count.

Time Complexity

Code

def count_senior_citizens(info_list):
    senior_citizen_count = 0
    
    for info in info_list:
        # Split the string by ',' to separate the name and age
        name, age_str = info.split(", ")
        age = int(age_str)  # Convert age to integer
        
        # Check if the age is 60 or more
        if age >= 60:
            senior_citizen_count += 1
    
    return senior_citizen_count

# Example usage:
info_list = ["Alice, 45", "Bob, 65", "Charlie, 60", "David, 55"]
print(count_senior_citizens(info_list))  # Output: 2

This function takes a list of strings, processes each to check if the person is a senior citizen, and returns the count of senior citizens.

Try our interview co-pilot at AlgoAdvance.com