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', ' ...]
  """
  names = []
  with open(filename) as f:
    doc = f.read()
  names += re.findall(r'Popularity in (\d+)', doc)
  #names.append(year.group(1))
  names_ranks = re.findall(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)', doc)
  d = {}
  for ranktup in names_ranks:
    d[ranktup[0]] = d.get(ranktup[0], []) + list(ranktup[1:])
  tl = []
  for k in d:
    tl.append(d[k][0] + ' ' + k)
    tl.append(d[k][1] + ' ' + k)
  return sorted(names + tl)