Skip to main content
5 of 7
added 43 characters in body
Tim
  • 2.9k
  • 18
  • 34

Given the names of two planets, give the distance

Using the following table (source) write some code that takes the names of two planets and returns the distance between them:

+-------------------+---------------+
|      Planets      | Distance (km) |
+-------------------+---------------+
| Mercury -> Venus  |      50290000 |
| Venus -> Earth    |      41400000 |
| Earth -> Mars     |      78340000 |
| Mars -> Jupiter   |     550390000 |
| Jupiter -> Saturn |     646270000 |
| Saturn -> Uranus  |    1448950000 |
| Uranus -> Neptune |    1627450000 |
| Neptune -> Pluto  |    1405380000 |
+-------------------+---------------+

Examples, input then output:

Mercury, Mars
170030000

<%%>

Neptune, Jupiter
-3722670000

<%%>

Earth, Earth
0

Note the negative sign there, as Jupiter comes before Neptune. They are also all integers.

Pluto doesn't have to be included (mostly because of a weird orbit which makes it hard to work out the distance - that distance given is my own calculation, but as Pluto is all famous now...).

By the way, but distances between planets I'm meaning orbits - I don't expect a date and working out where they are.

This is code golf, shortest code wins.

Tim
  • 2.9k
  • 18
  • 34