Skip to content

Instantly share code, notes, and snippets.

@iethree
Last active November 26, 2024 17:55
Show Gist options
  • Save iethree/4a193ca51867b49b6ab4e8290725ad9a to your computer and use it in GitHub Desktop.
Save iethree/4a193ca51867b49b6ab4e8290725ad9a to your computer and use it in GitHub Desktop.
Pokemon Evolutions List

You are given a data structure that represents Pokemon evolutions

bulbasaur

The data structure looks like this:

{
   name: "A", // can evolve to B and E
   evolutions: [{ 
     name: "B", // can evolve to C
     evolutions: [{
       name: "C", // can evolve to D
       evolutions: ["D"] // cannot further evolve
     }]
   }, 
   "E" // cannot further evolve
   ]
}
  • Pokemon that do not evolve further are represented as strings rather than objects.
  • While the provided examples only go to a depth of 3, you should provide a solution that works for any tree depth (ignoring memory constraints)
  • Your task: write a function that produces a string showing the hierarchy of how Pokemon evolve, with each evolution level indented by one space:
A
 B
  C
   D
 E

Full input data:

[
  {
    name: "Bulbasaur",
    evolutions: [
      {
        name: "Ivysaur",
        evolutions: ["Venusaur"]
      }
    ]
  },
  {
    name: "Charmander",
    evolutions: [
      {
        name: "Charmeleon",
        evolutions: ["Charizard"]
      }
    ]
  },
  {
    name: "Squirtle",
    evolutions: [
      {
        name: "Wartortle",
        evolutions: ["Blastoise"]
      }
    ]
  },
  {
    name: "Pichu",
    evolutions: [
      {
        name: "Pikachu",
        evolutions: ["Raichu"]
      }
    ]
  },
  {
    name: "Nidoran",
    evolutions: [
      {
        name: "Nidorino",
        evolutions: ["Nidoking"]
      },
      {
        name: "Nidorina",
        evolutions: ["Nidoqueen"]
      }
    ]
  },
  {
    name: "Oddish",
    evolutions: [
      {
        name: "Gloom",
        evolutions: ["Vileplume", "Bellossom"]
      }
    ]
  },
  {
    name: "Slowpoke",
    evolutions: ["Slowbro", "Slowking"]
  },
  {
    name: "Tyrogue",
    evolutions: ["Hitmonlee", "Hitmonchan", "Hitmontop"]
  },
  {
    name: "Evee",
    evolutions: [
      "Vaporeon",
      "Jolteon",
      "Flareon",
      "Espeon",
      "Umbreon",
      "Leafeon",
      "Glaceon",
      "Sylveon"
    ]
  },
  "Zapdos",
]

full output:

Bulbasaur
 Ivysaur
  Venusaur
Charmander
 Charmeleon
  Charizard
Squirtle
 Wartortle
  Blastoise
Pichu
 Pikachu
  Raichu
Nidoran
 Nidorino
  Nidoking
 Nidorina
  Nidoqueen
Oddish
 Gloom
  Vileplume
  Bellossom
Slowpoke
 Slowbro
 Slowking
Tyrogue
 Hitmonlee
 Hitmonchan
 Hitmontop
Evee
 Vaporeon
 Jolteon
 Flareon
 Espeon
 Umbreon
 Leafeon
 Glaceon
 Sylveon
Zapdos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment