Comments on Coat of Many Colours
Parent
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
Post
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))
0 comment threads