input: Write a function to generate sentences based on a grammar dictionary.
  assert generate_sentence() in ["do not play soccer.", "do not play math.", "do not play the guitar.", "do not study soccer.", "do not study math.", "do not study the guitar.", "do not work soccer.", "do not work math.", "do not work the guitar.", "doesn't play soccer.", "doesn't play math.", "doesn't play the guitar.", "doesn't study soccer.", "doesn't study math.", "doesn't study the guitar.", "doesn't work soccer.", "doesn't work math.", "doesn't work the guitar.", "no play soccer.", "no play math.", "no play the guitar.", "no study soccer.", "no study math.", "no study the guitar.", "no work soccer.", "no work math.", "no work the guitar."]
output: import random
  
  def generate_sentence():
      grammar = {
          "S": [["NP", "VP"]],
          "NP": [["do not"], ["doesn't"], ["no"]],
          "VP": [["V", "NP2"]],
          "V": [["play"], ["study"], ["work"]],
          "NP2": [["soccer."], ["math."], ["the guitar."]]
      }
      
      def generate(rule):
          if isinstance(rule, str):
              return rule
          elif isinstance(rule, list):
              result = []
              for part in rule:
                  if part in grammar:
                      result.append(generate(random.choice(grammar[part])))
                  else:
                      result.append(part)
              return ' '.join(result)
      
      sentence_structure = random.choice(grammar["S"])
      return generate(sentence_structure)