import re


def extract_names(filename):
  """
  Given a file name for baby.html, returns a list starting with the year string
  followed by the name-rank strings in alphabetical order.
  ['2006', 'Aaliyah 91', Aaron 57', 'Abagail 895', ' ...]
  """ 
  f = open(filename, 'r')
  dict = {}
  year = None
  
  for line in f:
    match_year = re.search(r'>Popularity\s+in\s+(\d\d\d\d)<', line)
    match_names_rank = re.search(
                      r'>(\d+)</td><td>([a-zA-Z]+)</td><td>([a-zA-Z]+)<', line)
    
    if match_year:
      year = match_year.group(1)
    elif match_names_rank:
      dict[match_names_rank.group(2)] = match_names_rank.group(1)
      dict[match_names_rank.group(3)] = match_names_rank.group(1)
    
  result_list = [str(year)]
  for k, v in sorted(dict.items(), key=lambda x : x[0]):
    result_list.append(f'{k} {v}')
    
  f.close()
  return result_list