The next colour
JavaScript, 68 bytes
s=>'RedOrangeYellowGreenBlueIndigoVioletRed'.match(s+'(.[a-z]*)')[1]
For input "Red"
, this function first construct an RegExp /Red(.[a-z]*)/
to match the string 'RedOrangeYellowGreenBlueIndigoVioletRed'
and then return the first capture result.
f=
s=>'RedOrangeYellowGreenBlueIndigoVioletRed'.match(s+'(.[a-z]*)')[1]
document.write('<table><tr><th>Input<th>Output')
for(i='Red';;){
document.write(`<tr><td>${i}<td>${i=f(i)}`);
if(i=='Red')break;
}
Perl 5 -p
, 58 57 bytes
#!/usr/bin/perl -p
$_={(Red,Orange,Yellow,Green,Blue,Indigo,Violet)x2}->{$_}
Try it online!
Now that the challenge has been changed to be cyclic the regex solution
say RedOrangeYellowGreenBlueIndigoVioletRed=~/$_(.[a-z]+)/
isn't optimal anymore (due to the double Red
)
Also 57 bytes:
#!/usr/bin/perl -p
$_=(Indigo,Blue,Violet,Yellow,Orange,Red,Green)[ord>>2&7]
Try it online!
Python, 79 bytes
z="Red Orange Yellow Green Blue Indigo Violet".split()*2
dict(zip(z,z[1:])).get
Try it online!
Handles Violet -> Red
. The desired function is given anonymously in the second line.
80 bytes
lambda x:"Red Orange Yellow Green Blue Indigo Violet Red".split(x)[1].split()[0]
Try it online!