diff --git a/kata/7-kyu/index.md b/kata/7-kyu/index.md index fb3e281f..620db10a 100644 --- a/kata/7-kyu/index.md +++ b/kata/7-kyu/index.md @@ -558,6 +558,7 @@ - [The old switcheroo](the-old-switcheroo "55d410c492e6ed767000004f") - [The Poet And The Pendulum](the-poet-and-the-pendulum "5bd776533a7e2720c40000e5") - [The Pony Express](the-pony-express "5b18e9e06aefb52e1d0001e9") +- [The Solar System - Jumbled Planets](the-solar-system-jumbled-planets "678e32f27625ec1b6a0e5976") - [The Speed of Letters](the-speed-of-letters "5fc7caa854783c002196f2cb") - [The 'spiraling' box](the-spiraling-box "63b84f54693cb10065687ae5") - [The wheat/rice and chessboard problem](the-wheat-slash-rice-and-chessboard-problem "5b0d67c1cb35dfa10b0022c7") diff --git a/kata/7-kyu/the-solar-system-jumbled-planets/README.md b/kata/7-kyu/the-solar-system-jumbled-planets/README.md new file mode 100644 index 00000000..7365325e --- /dev/null +++ b/kata/7-kyu/the-solar-system-jumbled-planets/README.md @@ -0,0 +1,48 @@ +# [The Solar System - Jumbled Planets](https://www.codewars.com/kata/the-solar-system-jumbled-planets "https://www.codewars.com/kata/678e32f27625ec1b6a0e5976") + +Oh, no! The celestial bodies orbiting the sun are all jumbled up! +

TASK

+Given the entire Solar System in the form of a list. Return a new list which has either '<', '>' +or '=' depending on whether the planet is smaller than the planet on its left or not. You have to start comparing from the +second item, because the first has nothing on its left. + +However, there are also asteroids in the Solar System. All asteroids are smaller than all the planets. If two asteroids are found beside +each other, the leftmost one will depend on the celestial being on the left of it. The one on the right will have `'='`. + +The Solar System might be empty. + +The celestial bodies stand in the order (size ascending): + +```Asteroid < Pluto < Mercury < Mars < Venus < Earth < Neptune < Uranus < Saturn < Jupiter``` + +Important: the dwarf planet Pluto is also included in the Solar System + +

EXAMPLES

+ +``` +["Mars", "Asteroid", "Venus", "Jupiter", "Asteroid", "Earth", "Pluto"] -> ['<', '>', '>', '<', '>', '<'] +# Asteriod is '<' Mars +# Venus is '>' any Asteroid +# Jupiter is '>' Venus +# Any Asteroid is '<' Jupiter +# Earth is '>' the Asteroid +# Finally, Pluto, being a dwarf planet, is '<' Earth + +["Asteroid", "Asteroid", "Asteroid", "Earth", "Jupiter"] -> ['=', '=', '>', '>'] +# Here, the Asteroid is '=' to the Asteroid on its left +# Again, it is '=' because there is another Asteroid on its left +# Earth is '>' than the Asteroid +# Finally, Jupiter, being the king of the planets, is '>' than Earth + +["Mercury", "Venus", "Earth", "Mars", "Asteroid", "Jupiter", "Saturn", "Uranus", "Neptune", "Asteroid", "Pluto"] -> ['>', '>', '<', '<', '>', '<', '<', '<', '<', '>'] +# Atleast here the Solar System is how it's supposed to be! +``` + +

NOTES

+· There will never be more than one of anything except asteroids + +· The Solar System will never contain anything outside: + +``` +["Asteroid", "Pluto", "Mercury", "Mars", "Venus", "Earth", "Neptune", "Uranus", "Saturn", "Jupiter"] +``` \ No newline at end of file diff --git a/kata/7-kyu/the-solar-system-jumbled-planets/main/SolarSystem.java b/kata/7-kyu/the-solar-system-jumbled-planets/main/SolarSystem.java new file mode 100644 index 00000000..9ec991a1 --- /dev/null +++ b/kata/7-kyu/the-solar-system-jumbled-planets/main/SolarSystem.java @@ -0,0 +1,12 @@ +import static java.util.stream.Collectors.joining; +import static java.util.stream.IntStream.range; + +class SolarSystem { + char[] annotate(String[] celestialBodies) { + var planets = "AsteroidPlutoMercuryMarsVenusEarthNeptuneUranusSaturnJupiter"; + return range(1, celestialBodies.length) + .map(i -> planets.indexOf(celestialBodies[i - 1]) - planets.indexOf(celestialBodies[i])) + .mapToObj(i -> i > 0 ? "<" : i < 0 ? ">" : "=") + .collect(joining()).toCharArray(); + } +} \ No newline at end of file diff --git a/kata/7-kyu/the-solar-system-jumbled-planets/test/SolarSystemTest.java b/kata/7-kyu/the-solar-system-jumbled-planets/test/SolarSystemTest.java new file mode 100644 index 00000000..ff2da214 --- /dev/null +++ b/kata/7-kyu/the-solar-system-jumbled-planets/test/SolarSystemTest.java @@ -0,0 +1,16 @@ +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +class SolarSystemTest { + @Test + void sample() { + assertArrayEquals(new char[]{'<', '>', '>', '<', '>', '<'}, new SolarSystem().annotate( + new String[]{"Mars", "Asteroid", "Venus", "Jupiter", "Asteroid", "Earth", "Pluto"})); + assertArrayEquals(new char[]{'=', '=', '>', '>'}, new SolarSystem().annotate( + new String[]{"Asteroid", "Asteroid", "Asteroid", "Earth", "Jupiter"})); + assertArrayEquals(new char[]{'>', '>', '<', '<', '>', '<', '<', '<', '<', '>'}, new SolarSystem().annotate( + new String[]{"Mercury", "Venus", "Earth", "Mars", "Asteroid", "Jupiter", "Saturn", "Uranus", "Neptune", "Asteroid", "Pluto"})); + assertArrayEquals(new char[0], new SolarSystem().annotate(new String[0])); + } +} \ No newline at end of file