21
\$\begingroup\$

Challenge

Consider the rainbow as seven colours, represented by strings as Red Orange Yellow Green Blue Indigo Violet.
Your task is to create a program that receives one of these colours as input and outputs next in order rainbow colour. This includes overlapping Violet -> Red

Input

A string containing one of rainbow colours.

Output

The next in order colour of the rainbow.

Rules

  • Colour names are case sensitive. They must match the case included in this post.
  • The input will always be valid. Any behavior is allowed for invalid input.
  • This is code golf, so the shortest amount of bytes wins!

Example Input and Output

Input -> Output
Red -> Orange
Orange -> Yellow
Yellow -> Green
Green -> Blue
Blue -> Indigo
Indigo -> Violet
Violet -> Red
\$\endgroup\$
8
  • 1
    \$\begingroup\$ "Provide at least one example input and output. Make sure they match your own description of what the input should look like." Are you describing your own challenge post? Or is this a requirement for solutions? \$\endgroup\$
    – recursive
    Mar 21, 2018 at 6:52
  • 6
    \$\begingroup\$ Are lower case colours okay? \$\endgroup\$
    – Emigna
    Mar 21, 2018 at 8:32
  • 4
    \$\begingroup\$ But what happens when you reach 'gray'? ;) \$\endgroup\$
    – AJFaraday
    Mar 21, 2018 at 11:24
  • 1
    \$\begingroup\$ @Emigna From the way OP phrased the question, I would guess that you can choose whether to do upper case, lower case, or case insensitive. Provide at least one example input and output. Make sure they match your own description of what the input should look like. \$\endgroup\$
    – AvahW
    Mar 21, 2018 at 13:47
  • 1
    \$\begingroup\$ Are trailing spaces okay? \$\endgroup\$
    – ivzem
    Mar 22, 2018 at 17:45

42 Answers 42

1
2
1
\$\begingroup\$

Bash + GNU Utilities, 63

grep -Po $1\\K.[a-z]+<<<RedOrangeYellowGreenBlueIndigoVioletRed

The list of colours is reversed so that a regex lookahead may be used. The lookahead boilerplate is one byte shorter than the lookbehind boilerplate. Of course we can do better the PCRE \K.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Factor, 81 bytes

[ qw{ Red Orange Yellow Green Blue Indigo Violet } [ index 1 + 7 mod ] keep nth ]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Red, 74 bytes

func[c][prin select[Red Orange Yellow Green Blue Indigo Violet Red]load c]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

APL+WIN, 66 bytes

↑(c⍳⊂⎕)⌽c←'Red' 'Orange' 'Yellow' 'Green' 'Blue' 'Indigo' 'Violet'

Prompts for screen input of colour as a string.

\$\endgroup\$
0
\$\begingroup\$

C (gcc) preprocessor macro, 74

Score is 74 bytes passed to the compiler.

Same idea as @nwellnhof's Perl answer.

-Df(c)=printf("%.6s","IndigoBlue  VioletYellowOrangeRed   Green"+*c/4%8*6)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, 89 87 bytes

def f(c):a="red orange yellow green blue indigo violet".split();print a[-~a.index(c)%7]

Test cases:

f("red") -> orange

f("orange") -> yellow

f("yellow") -> green

f("green") -> blue

f("blue") -> indigo

f("indigo") -> violet

f("violet") -> red

f("notAColour") -> Error

\$\endgroup\$
7
  • \$\begingroup\$ No, it's correct! I said to assume the input is a colour, so you don't have to implement other things \$\endgroup\$
    – lolad
    Mar 21, 2018 at 15:40
  • \$\begingroup\$ I think your modulus needs to be 7, not 6. Also, there are a few things you can do to get down to 81. tio.run/… \$\endgroup\$
    – recursive
    Mar 21, 2018 at 17:01
  • \$\begingroup\$ @lolad thanks for the clarification. \$\endgroup\$
    – AvahW
    Mar 22, 2018 at 9:59
  • \$\begingroup\$ @recursive I originally missed out orange from the string, and must have forgotten to change it, thanks :) \$\endgroup\$
    – AvahW
    Mar 22, 2018 at 10:00
  • \$\begingroup\$ OP specified that colors need to be uppercase. \$\endgroup\$
    – Laikoni
    Mar 22, 2018 at 12:51
0
\$\begingroup\$

C (gcc), 88 bytes

f(char*s){printf("%.6s",(strstr("red   violetindigoblue  green yelloworangered",s)+6));}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 84 bytes f(int*s){write(1,strstr("red violetindigoblue green yelloworangered ",s)+6,6);} \$\endgroup\$
    – ceilingcat
    Oct 5, 2018 at 8:02
0
\$\begingroup\$

Python 3, 85 bytes

c='Red Orange Yellow Green Blue Indigo Violet'.split()
f=lambda x:c[(c.index(x)+1)%7]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ you can golf this by reversing the order of the colours, and using index-1 instead of (index+1)%7 \$\endgroup\$ Mar 23, 2018 at 2:33
0
\$\begingroup\$

Wolfram Language (Mathematica), 84 bytes

TextWords version (84 Bytes but slow)

#/.Thread[#->RotateLeft@#&[TextWords@"Red Orange Yellow Green Blue Indigo Violet"]]&

StringSplit (86 Bytes)

#/.Thread[#->RotateLeft@#&[StringSplit@"Red Orange Yellow Green Blue Indigo Violet"]]&

Built-in color names called by number, also 86 Bytes:

#/.Thread[#->RotateLeft@#&[ColorData["HTML","Range"][[1,{114,100,139,52,10,57,135}]]]]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Lua, 89 bytes

c="Red   OrangeYellowGreen Blue  IndigoVioletRed"r=c:find(io.read())+6print(c:sub(r,r+5))

Outputs some trailing spaces. Example:

input: Yellow
output: Green 
space here:  ^

Try it online!

Lua, 102 bytes

c="Red   OrangeYellowGreen Blue  IndigoVioletRed"r=c:find(io.read())+6print(c:sub(r,r+5):match("%a+"))

Doesn't output any trailing spaces.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Tcl, 98 bytes

puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet}] [expr ([lsearch $L $argv]+1)%7]]

Try it online!


Tcl, 98 bytes

puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet Red}] [expr [lsearch $L $argv]+1]]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Swift, 121 113 bytes

let s=readLine()!;let i=["Red","Orange","Yellow","Green","Blue","Indigo","Violet"];print(i[(i.index(of:s)!+1)%7])

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.