Skip to content

Commit f297cf8

Browse files
* docs: kata description * feat: kata/the-solar-system-jumbled-planets * test: default constructor --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent d87c75f commit f297cf8

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@
558558
- [The old switcheroo](the-old-switcheroo "55d410c492e6ed767000004f")
559559
- [The Poet And The Pendulum](the-poet-and-the-pendulum "5bd776533a7e2720c40000e5")
560560
- [The Pony Express](the-pony-express "5b18e9e06aefb52e1d0001e9")
561+
- [The Solar System - Jumbled Planets](the-solar-system-jumbled-planets "678e32f27625ec1b6a0e5976")
561562
- [The Speed of Letters](the-speed-of-letters "5fc7caa854783c002196f2cb")
562563
- [The 'spiraling' box](the-spiraling-box "63b84f54693cb10065687ae5")
563564
- [The wheat/rice and chessboard problem](the-wheat-slash-rice-and-chessboard-problem "5b0d67c1cb35dfa10b0022c7")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [The Solar System - Jumbled Planets](https://www.codewars.com/kata/the-solar-system-jumbled-planets "https://www.codewars.com/kata/678e32f27625ec1b6a0e5976")
2+
3+
Oh, no! The celestial bodies orbiting the sun are all jumbled up!
4+
<h2>TASK</h2>
5+
Given the entire Solar System in the form of a list. Return a new list which has either <code>'<'</code>, <code>'>'</code>
6+
or <code>'='</code> depending on whether the planet is smaller than the planet on its left or not. You have to start comparing from the
7+
second item, because the first has nothing on its left.
8+
9+
However, there are also asteroids in the Solar System. All asteroids are smaller than all the planets. If two asteroids are found beside
10+
each other, the leftmost one will depend on the celestial being on the left of it. The one on the right will have `'='`.
11+
12+
The Solar System might be empty.
13+
14+
The celestial bodies stand in the order (size ascending):
15+
16+
```Asteroid < Pluto < Mercury < Mars < Venus < Earth < Neptune < Uranus < Saturn < Jupiter```
17+
18+
<b>Important: the dwarf planet Pluto is also included in the Solar System</b>
19+
20+
<h2>EXAMPLES<h2/>
21+
22+
```
23+
["Mars", "Asteroid", "Venus", "Jupiter", "Asteroid", "Earth", "Pluto"] -> ['<', '>', '>', '<', '>', '<']
24+
# Asteriod is '<' Mars
25+
# Venus is '>' any Asteroid
26+
# Jupiter is '>' Venus
27+
# Any Asteroid is '<' Jupiter
28+
# Earth is '>' the Asteroid
29+
# Finally, Pluto, being a dwarf planet, is '<' Earth
30+
31+
["Asteroid", "Asteroid", "Asteroid", "Earth", "Jupiter"] -> ['=', '=', '>', '>']
32+
# Here, the Asteroid is '=' to the Asteroid on its left
33+
# Again, it is '=' because there is another Asteroid on its left
34+
# Earth is '>' than the Asteroid
35+
# Finally, Jupiter, being the king of the planets, is '>' than Earth
36+
37+
["Mercury", "Venus", "Earth", "Mars", "Asteroid", "Jupiter", "Saturn", "Uranus", "Neptune", "Asteroid", "Pluto"] -> ['>', '>', '<', '<', '>', '<', '<', '<', '<', '>']
38+
# Atleast here the Solar System is how it's supposed to be!
39+
```
40+
41+
<h2>NOTES</h2>
42+
· There will never be more than one of anything except asteroids
43+
44+
· The Solar System will never contain anything outside:
45+
46+
```
47+
["Asteroid", "Pluto", "Mercury", "Mars", "Venus", "Earth", "Neptune", "Uranus", "Saturn", "Jupiter"]
48+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import static java.util.stream.Collectors.joining;
2+
import static java.util.stream.IntStream.range;
3+
4+
class SolarSystem {
5+
char[] annotate(String[] celestialBodies) {
6+
var planets = "AsteroidPlutoMercuryMarsVenusEarthNeptuneUranusSaturnJupiter";
7+
return range(1, celestialBodies.length)
8+
.map(i -> planets.indexOf(celestialBodies[i - 1]) - planets.indexOf(celestialBodies[i]))
9+
.mapToObj(i -> i > 0 ? "<" : i < 0 ? ">" : "=")
10+
.collect(joining()).toCharArray();
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class SolarSystemTest {
6+
@Test
7+
void sample() {
8+
assertArrayEquals(new char[]{'<', '>', '>', '<', '>', '<'}, new SolarSystem().annotate(
9+
new String[]{"Mars", "Asteroid", "Venus", "Jupiter", "Asteroid", "Earth", "Pluto"}));
10+
assertArrayEquals(new char[]{'=', '=', '>', '>'}, new SolarSystem().annotate(
11+
new String[]{"Asteroid", "Asteroid", "Asteroid", "Earth", "Jupiter"}));
12+
assertArrayEquals(new char[]{'>', '>', '<', '<', '>', '<', '<', '<', '<', '>'}, new SolarSystem().annotate(
13+
new String[]{"Mercury", "Venus", "Earth", "Mars", "Asteroid", "Jupiter", "Saturn", "Uranus", "Neptune", "Asteroid", "Pluto"}));
14+
assertArrayEquals(new char[0], new SolarSystem().annotate(new String[0]));
15+
}
16+
}

0 commit comments

Comments
 (0)