It was a rainy day, Jake was at his home watching out of the window. As the wind blew the clouds away, the rain stopped. The golden sunshine was back and to his surprise, for the first time in his life, he saw a very beautiful rainbow across the sky. Jake quickly grabbed his notebook and recorded the colors of the rainbow in the order they appeared from top to bottom.
He recorded seven colors in the rainbow in the following order (from top to bottom):
Violet, Indigo, Blue, Green, Yellow, Orange, Red
Program
Write a program to help Jake remember the colors and their order he saw the rainbow. Print the name of the color based on the position of the color in the rainbow. The top color is at position 1 and the last color is at position 7. Along with that display the color that is above and below to the color at the chosen position.
If the input position points the first color in the list of colors, it will only have the below color and not the above color. If the input position points to the last color, it will only have the above color and not the below color. If a position is not valid (not between 1 and 7), there will be no matching color found in the rainbow colors that Jake recorded.
Requirements
- Build a console based program or a GUI or simply use variables to store the input data.
Assumptions
- There are only 7 colors in the rainbow.
- Each color band is unique in the rainbow.
Potential Algorithm
Map the index (position) to the color what Jake recorded. Along with that map the top and below color also with that index as well.
Test cases
No. | Position (Input) | Above, Color, Below Color Names (Output) | Explanation |
---|---|---|---|
1 | 1 | None, Violet, Indigo | Since the input position is the first, there is no color above that one, hence "None". |
2 | 3 | Indigo, Blue, Green | "Blue" is the color at that position, "Indigo" is above it and "Green" is below it. |
3 | 7 | Orange, Red, None | Since the input position is the last, there is no color below that one, hence "None". |
4 | 0 | Invalid Index | There are only 7 colors each of these occupies one position from 1 to 7 |
5 | 8 | Invalid Index | Same as above. |
Instructions
- Prompt the user to enter the color position they like to know about via the command prompt/shell or initialize it in a variable.
- Write the logic to determine the color(s) above, at, and below the input position.
- Don't use arrays or maps unless you are working on it as suggested in the improvements.
- Display the three color names with the color corresponding to the position at the middle.
Suggested Improvements
- Implement the program using arrays or dictionary to store the color of the rainbow at a given position and find the color at the position above or below it.