Coat of Many Colours
Challenge
Given a list of unique colour names as input, sort them in the order that they first appear in Joseph's Amazing Technicolour Dreamcoat.
Example
Input: green, blue, red, brown
Output: red, green, brown, blue
The full list of colours, in order, is:
1. red
2. yellow
3. green
4. brown
5. scarlet
6. black
7. ochre
8. peach
9. ruby
10. olive
11. violet
12. fawn
13. lilac
14. gold
15. chocolate
16. mauve
17. cream
18. crimson
19. silver
20. rose
21. azure
22. lemon
23. russet
24. grey
25. purple
26. white
27. pink
28. orange
29. blue
Or as an array of strings:
["red","yellow","green","brown","scarlet","black","ochre","peach","ruby","olive","violet","fawn","lilac","gold","chocolate","mauve","cream","crimson","silver","rose","azure","lemon","russet","grey","purple","white","pink","orange","blue"]
Rules
- You may take input by any reasonable, convenient means (e.g., an array of strings, a delimited string, individual strings), but please specify your input method in your answer.
- You may do the same for your output.
- The input will only ever contain colours from the above list.
- Your solution should be able to handle empty inputs.
- You may choose whether all words in the input are consistently uppercase, lowercase or title case but your output's casing must match your input's.
- This is code-golf so lowest byte count in each language wins.
- As always, standard loopholes are forbidden.
Test cases
Input: []
Output: []
Input: ["green", "blue", "red", "brown"]
Output: ["red", "green", "brown", "blue"]
Input: ["gold", "grey", "green"]
Output: ["green", "gold", "grey"]
Input: ["ruby","yellow","red","grey"]
Output: ["red", "yellow", "ruby", "grey"]
Input: ["gold", "green", "fawn", "white", "azure", "rose", "black", "purple", "orange", "silver", "ruby", "blue", "lilac", "crimson", "pink", "cream", "lemon", "russet", "grey", "olive", "violet", "mauve", "chocolate", "yellow", "peach", "brown", "ochre", "scarlet", "red"]
Output: ["red", "yellow", "green", "brown", "scarlet", "black", "ochre", "peach", "ruby", "olive", "violet", "fawn", "lilac", "gold", "chocolate", "mauve", "cream", "crimson", "silver", "rose", "azure", "lemon", "russet", "grey", "purple", "white", "pink", "orange", "blue"]
Scala, 119 bytes Saved 26 b …
3y ago
[JavaScript (Node.js)], 153 14 …
3y ago
[C (gcc)], 301 bytes Functi …
3y ago
C, 534 bytes Strictly confo …
3y ago
[Python 3], 349 261 160 bytes …
3y ago
Ruby, 101 79 76 72 bytes Th …
3y ago
6 answers
Scala, 119 bytes
Saved 26 bytes after porting Moshi's solution!
_ sortBy("y gree br sc bla oc pe rub ol v f li go ch m cre c s ro a l ru g pu w p o b"split " "indexWhere _.startsWith)
Original solution, 145 130 bytes
Saved 15 bytes after looking at Moshi's solution!
_ sortBy("d,ll,ee,ow,ar,ack,h,ac,b,iv,ol,w,la,ld,o,uv,ea,im,l,se,ur,m,s,e,r,i,n,a,u"split ","indexWhere _.substring(2).startsWith)
It turns out that after dropping the first 2 letters, the first 2 letters after that (and in some cases 1 or 3) are enough to identify colors.
_ sortBy( //Sort the input by this function:
"..." //Take the comma separated string of colors
.split(",") //Split on commas
indexWhere //And find the index where
_.substring(2) //The current string with the 1st 2 chars removed
.startsWith //Starts with that color prefix
)
0 comment threads
JavaScript (Node.js), 153 147 bytes
-6 bytes thanks to Shaggy!
a=>a.sort((x,y)=>g(x)-g(y),g=x=>"y gree br sc bla oc pe rub ol v f li go ch m cre c s ro a l ru g pu w p o b".split` `.findIndex(t=>!x.indexOf(t)))
Inspired by user's solution, I found the minimum number of initial characters that uniquely identified each color. I then golfed if further by realizing the ones at the end of the list don't actually have to uniquely identify the color, because the ones it couldn't determine would have been caught already. (E.g. green
and grey
need the gree
to determine green, but if it isn't green
then it must be grey
and we can catch that with just g
)
Old answer, 219 bytes
a=>"red yellow green brown scarlet black ochre peach ruby olive violet fawn lilac gold chocolate mauve cream crimson silver rose azure lemon russet grey purple white pink orange blue".split(' ').filter(i=>a.includes(i))
C (gcc), 301 bytes
Function taking in an array of null-terminated strings and the array's length.
#include <string.h>
#include <stdlib.h>
char*s="edellowreenrowncarletlackchreachubyliveioletawnilacoldhocolateauvereamrimsonilverosezuremonussetreyurplehiteinkrangelue";int g(const void*a,const void*b){return strstr(s,*(char**)a+1)-strstr(s,*(char**)b+1);}void f(char**a,int l){qsort(a,l,sizeof*a,g);}
I shamelessly stole the idea of removing the first two characters from user's solution. Note: My original solution had a bug where the wn
from fawn
matched the own
from brown and broke the order. This updated solution only removes the first character, making rown
and awn
distinct.
1 comment thread
C, 534 bytes
Strictly conforming program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char i=1,**c,**d,*t[30]={"red","yellow","green","brown","scarlet","black","ochre","peach","ruby","olive","violet","fawn","lilac","gold","chocolate","mauve","cream","crimson","silver","rose","azure","lemon","russet","grey","purple","white","pink","orange","blue"};int f(const void*a,const void*b){c=d=t;for(;*c;)if(!strcmp(*c++,*(char**)a))break;for(;*d;)if(!strcmp(*d++,*(char**)b))break;return c<d?-1:c!=d;}int main(int c,char**v){qsort(v+1,c-1,8,f);for(;i<c;)puts(v[i++]);}
The char*
casts are a bit questionable in a strictly conforming program since they remove const qualifiers. My argument for why it is valid, is that the effective type of the argv strings ought to be char[]
.
I couldn't figure out how to use massive amounts of command line arguments on tio.run, here's a Godbolt with the worst test case entered: https://godbolt.org/z/vbdYe6cxx
3 comment threads
Python 3, 349 261 160 bytes
a="re y gree br sc bla oc pe rub ol v f li go ch m cre cri si ro a le rus grey pu w pi or blu".split()
def f(b):return[j for i in a for j in b if j[:len(i)]==i]
Golfed 88 bytes thanks to @user's advice. Golfed 81 bytes thanks to @celtschk's advice.
3 comment threads
Ruby, 101 79 76 72 bytes
This challenge was one of the funniest I have ever solved!
72 bytes solution (I show the solution as a Ruby string - because binary data is filtered out)
"->l{l.sort_by{'d\v\x162\x82\r\x1D\nJ\"\x01T\x0E?\x8B.\x11\x05\x06G*(\fM\x00 HyI'.index''<<_1[2,4].sum%145}}"
Unfortunately to make it work on Attempt This Online website, I had to generate code there and use eval (because if I paste code directly then it corrupts my code):
Attention This code suppose to include a binary data, which is filtered out by this website. Copying it from this website wont't work. You can clone the repository, which contains code (and generator) from github gist.
0 comment threads