The next colour

20

2

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

lolad

Posted 2018-03-21T06:43:37.060

Reputation: 754

1"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? – recursive – 2018-03-21T06:52:58.450

Do the test cases give the exact set of possible inputs? Do we have to use the same capitalization? – xnor – 2018-03-21T07:28:27.597

6Are lower case colours okay? – Emigna – 2018-03-21T08:32:31.767

4But what happens when you reach 'gray'? ;) – AJFaraday – 2018-03-21T11:24:46.427

1@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. – sonrad10 – 2018-03-21T13:47:03.607

@Enigma lower case colours are not okay – lolad – 2018-03-21T19:51:23.730

1Are trailing spaces okay? – ivzem – 2018-03-22T17:45:00.290

1The issue I have with wrapping back to the first color is that double rainbows reverse direction rather than starting over. – Broots Waymb – 2018-03-22T18:46:43.797

Answers

2

SOGL V0.12, 23 bytes

k‰³d∆|ΝμHō↑≥░δ÷f‘θ⁽,WIw

Try it Here!

Explanation:

...‘θ⁽,WIw  
...‘        push "red orange yellow green blue indigo violet"
    θ       split on spaces
     ⁽      uppercase the 1st letter of every item (SOGLs dictionary only has lowercase words)
      ,W    get the inputs index in the array
        I   increment
         w  and get that item in the array, wrapping if necessary

dzaima

Posted 2018-03-21T06:43:37.060

Reputation: 19 048

22

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;
}

tsh

Posted 2018-03-21T06:43:37.060

Reputation: 13 072

What's the result for input "Violet"? I guess you should change the text to 'RedOrangeYellowGreenBlueIndigoVioletRed' or something – Olivier Grégoire – 2018-03-21T08:38:37.613

1@OlivierGrégoire Ok, added. This rule is changed by OP after answer posted. – tsh – 2018-03-21T08:42:37.370

I didn't know: I only saw the final version of the question. – Olivier Grégoire – 2018-03-21T08:59:52.480

9

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!

Ton Hospel

Posted 2018-03-21T06:43:37.060

Reputation: 14 114

7

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!

xnor

Posted 2018-03-21T06:43:37.060

Reputation: 115 687

7

Perl 6, 56 bytes

{<Indigo Blue Violet Yellow Orange Red Green>[.ord/4%8]}

Try it online!

Exploits the fact that bits 2-4 of the ASCII codes of each color's first letter happen to map to 0-6.

say map (*.ord +> 2) % 8, <R O Y G B I V>
# (4 3 6 1 0 2 5)

Here's a nice non-competing solution that uses "purple" instead of "indigo" and "violet" (38 chars, 59 bytes):

{''.uninames~~m/$^a.\S+.<(\S+/}

Try it online!

nwellnhof

Posted 2018-03-21T06:43:37.060

Reputation: 10 037

Almighty Unicode names – Weijun Zhou – 2018-03-21T14:54:24.553

As interesting as this solution is, it ultimately doesn't follow the spec. Could you include a compliant version in you answer and post this as an addendum? – Dennis – 2018-03-21T15:01:36.617

There is also a %12 solution for Coconut, but of course this is neater. – Weijun Zhou – 2018-03-21T18:44:15.503

5

Ruby -n, 62 60 bytes

-2 by Asone Tuhid.

p"RedVioletIndigoBlueGreenYellowOrangeRed"[/.[a-z]+(?=#$_)/]

Try it online!

Regex approach looks promising for Ruby too. However, I arrived at a shorter solution using a lookahead and directly printing the match, rather than playing with capturing groups. The list of colors is in reverse direction since lookahead is 1 byte cheaper than lookbehind.

Kirill L.

Posted 2018-03-21T06:43:37.060

Reputation: 6 693

very nice, -2 bytes (/#$_/ interpolates)

– Asone Tuhid – 2018-03-21T12:56:45.300

4

Red, 87 bytes

func[c][print first find/tail[Red Orange Yellow Green Blue Indigo Violet Red]to-word c]

Try it online!

Galen Ivanov

Posted 2018-03-21T06:43:37.060

Reputation: 13 815

4No idea what this language is, whether or how your code works, or whether it's golfed at all, but have a +1 just for using a language called "Red" for this challenge. – msh210 – 2018-03-21T12:50:42.033

@ msh210 It's closely related to Rebol ( Relative Expression-Based Object Language). The language represents code, data and metadata in the same manner - with blocks denoted by []. The current Red distribution is only 1.1 MB and includes a console/interpeter, as well as compiler that can cross-complie to different platforms. My code is golfed in the sence that I tried different solutions and removed all the spaces I can. The bad thing is that the space is the delimiter almost everywhere. All math expressions need to have spaces on both sides of the operators like a: b + c (a=b+c). – Galen Ivanov – 2018-03-21T13:10:54.447

4

05AB1E, 30 bytes

“†¾›ÈŠÛˆ¨‡—ëßigo°Íolet“#™DIk>è

Try it online!

Explanation

“†¾›ÈŠÛˆ¨‡—ëßigo°Íolet“          # push a string of colours
                       #         # split on spaces
                        ™        # title-case each
                         D       # duplicate
                          Ik     # get the index of the input
                            >    # increment
                             è   # get the element at that index

Emigna

Posted 2018-03-21T06:43:37.060

Reputation: 50 798

3

Haskell, 80 71 75 bytes

Thanks to Laikoni for shortening 9 bytes!

g x=snd(span(/=x)$words"Red Orange Yellow Green Blue Indigo Violet Red")!!1

Try it online!


Another solution, slightly more idiomatic, but I could not get it shorter:

data R=Red|Orange|Yellow|Green|Blue|Indigo|Violet deriving(Enum,Read,Eq)
succ.read

It needs to derive Read because of the requirement that the input is a string and at least Eq or Show in order to either test for equality or show the result.

Cristian Lupascu

Posted 2018-03-21T06:43:37.060

Reputation: 8 369

71 bytes with span: Try it online!

– Laikoni – 2018-03-21T08:00:24.340

@Laikoni Wow, that's cool, thanks! I forgot about span... – Cristian Lupascu – 2018-03-21T08:04:13.647

1OP clarified that Violet should wrap around to Red, so you need to add Red again to the end of the string. – Laikoni – 2018-03-21T11:06:56.070

I like the idea of the second one a lot! However succ Violet won't work for Enums don't wrap around :( – ბიმო – 2018-03-21T12:22:15.033

@BMO Exactly. When I made that version I wasn't aware of the wrap-around requirement. – Cristian Lupascu – 2018-03-21T12:35:18.973

3

Java (JDK 10), 77 bytes

s->"Red Orange Yellow Green Blue Indigo Violet Red".split(s)[1].split(" ")[1]

Try it online!

Credits

Olivier Grégoire

Posted 2018-03-21T06:43:37.060

Reputation: 10 647

178 bytes: s->"RedOrangeYellowGreenBlueIndigoVioletRed".split(s)[1].split("(?=[A-Z])")[0] – Kevin Cruijssen – 2018-03-21T10:39:48.053

@KevinCruijssen That some nice regex fu that you've got! – Olivier Grégoire – 2018-03-21T11:00:26.687

1

I remembered when I once was looking for a split which keeps delimiters as separated items that the answer I found also contained options to keep the delimiter concatted as either leading or trailing part, and figured it would be useful here. :) Here is that answer including the look-ahead / look-behind for the other two options mentioned.

– Kevin Cruijssen – 2018-03-21T11:48:15.467

1Save a byte: s->"Red Orange Yellow Green Blue Indigo Violet Red".split(s)[1].split(" ")[1] – Okx – 2018-03-21T20:44:54.180

3

Excel, 85 bytes

=CHOOSE(MOD(CODE(A1),12),"green","indigo","yellow",,,"orange","blue",,"violet","red")

Uses lowercase names.

Same approach, with Uppercase letters 86 bytes:

=CHOOSE(MOD(CODE(A1),12),"Violet","Red",,,"Green","Indigo","Yellow",,,"Orange","Blue")

Wernisch

Posted 2018-03-21T06:43:37.060

Reputation: 2 534

3

Vim, 59 56 53 52 Bytes

-1 byte thanks to tsh

2IYellow Green Blue Indigo Violet Red Orange <Esc>*wywVp

oktupol

Posted 2018-03-21T06:43:37.060

Reputation: 697

12IYellow Green Blue Indigo Violet Red Orange <Esc>*wywVp – tsh – 2018-03-21T12:14:43.553

3

Retina, 65 58 bytes

$
(.[a-z]+)
L$:`RedOrangeYellowGreenBlueIndigoVioletRed
$1

Try it online!

Explanation

$
(.[a-z]+)

We start by appending (.[a-z]+) to the input, thereby turning it into a regex which matches the input colour, immediately followed by exactly one more colour (capturing the latter).

L$:`RedOrangeYellowGreenBlueIndigoVioletRed
$1

Now the : swaps the stage's input with its own regex. So the previous result becomes the regex and it's matched against the list of colours. The (single) match gets replaced with its first capturing group (i.e. the next colour in the cycle) and returned. Output at the end of the program happens automatically.

Martin Ender

Posted 2018-03-21T06:43:37.060

Reputation: 184 808

Here is another 65-byte alternative.

– Kevin Cruijssen – 2018-03-21T12:14:31.080

Yeah, I removed my comment after seeing Kevin's solution. Having to set up \1 and jumping to the target wastes many bytes ::-) – Ton Hospel – 2018-03-21T13:15:35.667

@TonHospel found something. :) – Martin Ender – 2018-03-21T13:17:51.487

1@KevinCruijssen Thanks to your and Ton's pushing for a no-delimiter solution, I ended up finding a 58-byter. ;) – Martin Ender – 2018-03-21T13:21:09.053

2

Stax, 31 30 29 bytes

ÇôF┘≡▓ƒ◄╙>┘☼░⌂╪B<U[ÇQ╒eöΣQ╔÷n

Run and debug it

This uses the ring translation instruction. It replaces each element in an array with the following one from the "decoder ring". Usually, it's used to do character replacement in a string, but it can be used on an entire string too, if it's wrapped in a singleton array.

Here's the unpacked, ungolfed, commented ascii representation of the same program.

]   wrap input in singleton array
`5^bZ_9G*h]h%oM~X9e-0ZQJkb2`    compressed string literal with color names
:.j title case and split on spaces
:t  do ring translation

Run this one

recursive

Posted 2018-03-21T06:43:37.060

Reputation: 8 616

ok... I love these languages =) – lolad – 2018-03-21T08:16:42.313

3Fails for Violet -> Red – Weijun Zhou – 2018-03-21T12:19:47.610

1@WeijunZhou: That test case was added after this submission. – recursive – 2018-03-21T14:26:35.690

1@WeijunZhou: I fixed that test case and shaved a byte. – recursive – 2018-03-22T04:05:23.483

Not sure why, but this appears to fail on tio. – Weijun Zhou – 2018-03-22T05:55:10.950

1@WeijunZhou: It seems to be an inconsistency between the C# and JS implementations of stax. I'll add a test case and normalize the behavior for the next release. – recursive – 2018-03-22T05:58:09.627

2

Coconut, 79 bytes

s->"# Violet Red # # Green Indigo Yellow # # Orange Blue".split()[ord(s[0])%12]

Try it online!

Laikoni

Posted 2018-03-21T06:43:37.060

Reputation: 23 676

Don't know Coconut. But think you should be able to cut 2 bytes by using lowercase letters, as this reduces the max modulo value: s->"# green indigo yellow # # orange blue # violet red".split()[ord(s[0])%12] – Wernisch – 2018-03-21T12:46:28.327

@Wernisch Thanks for the suggestion. Though I'll wait until OP has responded whether lowercase letters are acceptable before updating. – Laikoni – 2018-03-21T13:09:13.723

2

J, 67 64 62 bytes

-2 bytes thank to FrownyFrog

>:&.((cut'Red Orange Yellow Green Blue Indigo Violet Red')i.<)

Try it online!

Galen Ivanov

Posted 2018-03-21T06:43:37.060

Reputation: 13 815

1&. actually works for this. Try it online – FrownyFrog – 2018-03-21T21:40:52.440

2

Husk, 28 bytes

S!o→€⁰w¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉

Try it online!

Maybe there are better options for managing the arguments, but this is the best I could find

Explanation

S!o→€⁰w¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉
       ¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉    Compressed string with all the colors
                                "Red Orange Yellow Green Blue Indigo Violet"
      w                         Split on spaces
S                               Pass the list to both the following functions:
    €⁰                          1) Find the index of the input in the list
  o→                                  and increase it by one
 !                              2) Get the element of the list at the
                                   resulting position (indexing is cyclical)

Leo

Posted 2018-03-21T06:43:37.060

Reputation: 8 482

2

R, 109 93 bytes

function(x){y=c("Red","Orange","Yellow","Green","Blue","Indigo","Violet");y[match(x,y)%%7+1]}

Try it online!

-16 thanks to Giuseppe for the use of match advice

DS_UNI

Posted 2018-03-21T06:43:37.060

Reputation: 181

welcome to PPCG! this is a nice first answer. I think match(y,x)%%7+1 is shorter for indexing than your if statement. Additionally, the builtin colors() contains a lot of color names, if you find the indices :) – Giuseppe – 2018-03-22T18:33:24.613

oops, looks like colors() doesn't contain indigo! Ah well, still, +1! – Giuseppe – 2018-03-22T18:38:49.260

thanks! match is better here – DS_UNI – 2018-03-23T14:02:58.807

91 bytes – Giuseppe – 2018-03-23T14:04:15.307

and yeah I first tried to use colors() :/ – DS_UNI – 2018-03-23T14:04:24.320

how!? I copied that from tio, let me see again – DS_UNI – 2018-03-23T14:05:25.540

aha, cool! I just got it :D – DS_UNI – 2018-03-23T14:07:16.997

@Giuseppe grep is your friend

– JayCe – 2018-06-13T17:24:49.930

1

IBM/Lotus Notes Formula Language, 79 74 bytes

@Left(@Right("Red Orange Yellow Green Blue Indigo Violet Red ";a+" ");" ")

Previous version for 79:

R:=@Explode("Red,Orange,Yellow,Green,Blue,Indigo,Violet,Red");R[@Member(a;R)+1]

Takes input from an editable text field called a.

There is no TIO for formula language so here's a couple of screenshots.

enter image description here

enter image description here

enter image description here

ElPedro

Posted 2018-03-21T06:43:37.060

Reputation: 5 301

1

Gema, 67 characters

*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}

Sample run:

bash-4.4$ echo -n Yellow | gema '*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}'
Green

bash-4.4$ echo -n Violet | gema '*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}'
Red

Gema, 59 characters

R=Orange
O=Yellow
Y=Green
G=Blue
B=Indigo
I=Violet
V=Red
*=

Boring one. Dumbest approach ever, but quite short.

Sample run:

bash-4.4$ gema 'R=Orange;O=Yellow;Y=Green;G=Blue;B=Indigo;I=Violet;V=Red;*=' <<< 'Yellow'
Green

bash-4.4$ gema 'R=Orange;O=Yellow;Y=Green;G=Blue;B=Indigo;I=Violet;V=Red;*=' <<< 'Violet'
Red

manatwork

Posted 2018-03-21T06:43:37.060

Reputation: 17 865

1

Ruby -n, 75 69 bytes

a=%w{Red Orange Yellow Green Blue Indigo Violet};p a[-~(a.index$_)%7]

Try it online!

Asone Tuhid

Posted 2018-03-21T06:43:37.060

Reputation: 1 944

1Why the i&& thing? No need to check for nil, as “Ignore any errors if this is not a colour.” – manatwork – 2018-03-21T09:49:23.267

@manatwork I understood it as "catch and ignore any errors" but alright. – Asone Tuhid – 2018-03-21T10:35:26.623

1

Batch, 97 bytes

@set s=Red Orange Yellow Green Blue Indigo Violet Red
@call set s=%%s:*%1 =%%
@echo %s: =&rem %

Explanation: The call on the second line has the effect of substituting the parameter into the command and evaluating it, turning it into e.g. set s=%s:Red =%, which deletes the prefix of the string that includes the parameter. The substitution on the third line then replaces all the spaces with statement separators and comments. This works because string substitution happens before parsing.

Neil

Posted 2018-03-21T06:43:37.060

Reputation: 95 035

1

Julia 0.6, 76 bytes

f(s)=match(Regex("$s(.[a-z]*)"),"RedOrangeYellowGreenBlueIndigoViolet"^2)[1]

Try it online!

This handles the Violet->Red by recycling the string with the power ^ operator.

Here's a slightly longer solution without regexes:

g(s,l=split("Red Orange Yellow Green Blue Indigo Violet"," "))=l[(findin(l,[s])[1])%7+1]

niczky12

Posted 2018-03-21T06:43:37.060

Reputation: 301

1

PowerShell, 74 bytes

(-split("Red Orange Yellow Green Blue Indigo Violet "*2-split$args)[1])[0]

Try it online!

Takes the string "Red ... Violet " and string-multiplies it out by two to properly handle the Violet -> Red test case. We then -split that string on input $args to give us an array of two strings. We take the second string thereof [1], then -split that on whitespace to give us an array of strings and take the first [0].

For example, for input "Yellow", the first step will result in @("Red Orange ", " Green Blue Indigo ... Indigo Violet "). We take the second one of that, split it on whitespace (which removes the whitespace), resulting in @("Green", "Blue", ... "Violet"), so taking the [0] one thereof results in the proper next string.

AdmBorkBork

Posted 2018-03-21T06:43:37.060

Reputation: 41 581

1

PHP, 92 bytes

$a=" Red   OrangeYellowGreen Blue  IndigoVioletRed";echo substr($a,strpos($a,$argv[1])+6,6)

Try it online!

Dave

Posted 2018-03-21T06:43:37.060

Reputation: 61

1

Kotlin, 73 bytes

x->"RedOrangeYellow Green  BlueIndigoVioletRed".substringAfter(x).take(6)

Try it online!

Taking advantage of the fact that many colors are 6 characters, the ones that are not are prefixed with spaces to make them 6 characters long. Hopefully it's acceptable that some of the colors are outputted with spaces before them.
e.g. Red is " Red", Blue is " Blue"

Makotosan

Posted 2018-03-21T06:43:37.060

Reputation: 503

1

SmileBASIC, 94 84 bytes

C$="Red   OrangeYellowGreen Blue  IndigoViolet
INPUT I$?MID$(C$*2,INSTR(C$,I$)+6,6)

12Me21

Posted 2018-03-21T06:43:37.060

Reputation: 6 110

1

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!

Digital Trauma

Posted 2018-03-21T06:43:37.060

Reputation: 64 644

1

Japt, 45 43 bytes

-2 bytes thanks to Shaggy

`R‚sOÎÁƒYÁMwsGÎ9sBluƒI˜igosVio¤t`qs
g1+UbNg

Try it online!

Oliver

Posted 2018-03-21T06:43:37.060

Reputation: 7 160

43 bytes – Shaggy – 2018-03-22T00:22:44.160

@Shaggy Thanks! I forgot about N – Oliver – 2018-03-22T00:26:08.700

1

q/kdb+, 59 55 bytes

Solution:

.[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]

Examples:

q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Red"
"Violet"
q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Orange"
"Red"
q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Blue"
"Green"

Explanation:

Create a dictionary of colour => next colour, the input is the key to the dictionary:

.[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange] / the solution
.[ ;                                                  ] / apply multiple args to function
           `Red`Violet`Indigo`Blue`Green`Yellow`Orange  / list of colours
         ($)                                            / convert to strings
    2 8#                                                / reshape into 2x8 grid
  !                                                     / create dictionary

Bonus:

It's 53 bytes in K4:

.[!;2 8#$`Red`Violet`Indigo`Blue`Green`Yellow`Orange]

streetster

Posted 2018-03-21T06:43:37.060

Reputation: 3 635

1

sed, 72 bytes

s/$/%RedOrangeYellowGreenBlueIndigoVioletRed/;s/(.+)%.*\1(.[a-z]+).*/\2/

Try it Online

Example 1:

Input:

Red
Orange
Yellow
Green
Blue
Indigo
Violet

Output:

Orange
Yellow
Green
Blue
Indigo
Violet
Red

Example 2:

Input:

Indigo
Yellow
Red
Red
Blue
Green
Orange
Violet
Green
Green
Green
Blue
Blue
Violet

Output:

Violet
Green
Orange
Orange
Indigo
Blue
Yellow
Red
Blue
Blue
Blue
Indigo
Indigo
Red

lucasb

Posted 2018-03-21T06:43:37.060

Reputation: 451

Welcome to the site! Could you provide a link to an online interpreter, such as Try It Online! where we can test this solution?

– caird coinheringaahing – 2018-03-22T16:21:46.867

@cairdcoinheringaahing: Hi, thank you very much for the welcome and the website recommendation! I've updated my answer with a link to the page where you can test the code online. – lucasb – 2018-03-22T19:07:22.723

0

APL+WIN, 66 bytes

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

Prompts for screen input of colour as a string.

Graham

Posted 2018-03-21T06:43:37.060

Reputation: 3 184

0

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

sonrad10

Posted 2018-03-21T06:43:37.060

Reputation: 535

No, it's correct! I said to assume the input is a colour, so you don't have to implement other things – lolad – 2018-03-21T15:40:56.617

@lolad thanks for the clarification. – sonrad10 – 2018-03-22T09:59:48.460

@recursive I originally missed out orange from the string, and must have forgotten to change it, thanks :) – sonrad10 – 2018-03-22T10:00:59.670

OP specified that colors need to be uppercase. – Laikoni – 2018-03-22T12:51:27.563

@Laikoni Provide at least one example input and output. Make sure they match your own description of what the input should look like. I think that means that the colours can be any case, as long at it is specified in the answer – sonrad10 – 2018-03-22T13:28:40.933

They wrote lower case colours are not okay in the comments.

– Laikoni – 2018-03-22T14:34:50.163

0

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!

Digital Trauma

Posted 2018-03-21T06:43:37.060

Reputation: 64 644

0

C (gcc), 88 bytes

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

Try it online!

GPS

Posted 2018-03-21T06:43:37.060

Reputation: 341

184 bytes f(int*s){write(1,strstr("red violetindigoblue green yelloworangered ",s)+6,6);} – ceilingcat – 2018-10-05T08:02:03.803

0

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!

Tamás Sengel

Posted 2018-03-21T06:43:37.060

Reputation: 211

0

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!

Dat

Posted 2018-03-21T06:43:37.060

Reputation: 879

you can golf this by reversing the order of the colours, and using index-1 instead of (index+1)%7 – Destructible Lemon – 2018-03-23T02:33:06.920

0

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!

Kelly Lowder

Posted 2018-03-21T06:43:37.060

Reputation: 3 225

0

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!

ivzem

Posted 2018-03-21T06:43:37.060

Reputation: 1 129

0

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!

sergiol

Posted 2018-03-21T06:43:37.060

Reputation: 3 055