多种颜色的外套


22

挑战

给定一个唯一的颜色名称列表作为输入,按它们在约瑟夫的《惊人的彩色彩色梦coat》中首次出现的顺序对其进行排序。


Input:  green, blue, red, brown
Output: red, green, brown, blue

颜色的完整列表按顺序是:

 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

或作为字符串数组:

["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"]

规则

  • 只要我们的标准I / O规则允许,您可以通过任何合理,方便的方式(例如,字符串数组,定界字符串,单个字符串)进行输入,但是请在答案中指定输入方法。
  • 您可以对输出执行相同的操作。
  • 输入将仅包含以上列表中的颜色。
  • 您的解决方案应该能够处理空的输入。
  • 您可以选择输入中的所有单词都是大写,小写还是标题大写,但输出的大小写必须与输入的大小写匹配。
  • 这是因此每种语言中的最低字节数为准。
  • 与往常一样,标准漏洞是被禁止的。

测试用例

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"]

1
沙盒(难以置信的话,它在那里呆了18个月!)
Shaggy

Answers:


11

PowerShell中262个 155 151 127 125 95字节

$args|sort{"rlyegwbrscbrocpyrvo lvnfaldgccvmacmcvseraolsrygpptwkpnoeb".indexof((-join$_[3,0]))}

在线尝试!

天真的方法。PowerShell sort-object可以基于为每个对象执行的脚本块进行排序。在这里,我们只是.IndexOf()从字符串中获取颜色,该颜色将为每种颜色分配一个数值,然后根据这些数字进行排序。该字符串由每种颜色的第四个字母和第一个字母构成,以确保唯一性。输出是隐式的。

-4个字节,感谢Shaggy。
-2个字节,感谢mazzy。
多亏了KGlasier,它的存储空间高达-30个字节。


我不知道您是否可以有效地做到这一点(按字节),但是如果您按长度3个子字符串排序,然后使用原始字符串的辅助键排序,则唯一的冲突是green grey按正确的字母顺序。
HyperNeutrino

2
@Shaggy是的,之所以有用,是因为如果找不到该字符串,.IndexOf()它将返回,并按正确的顺序排序。谢谢!-1red
AdmBorkBork

我认为您可以删除字符串周围的括号。
mazzy

@mazzy确实,谢谢!
AdmBorkBork

1
@KGlasier哇,感谢您找到该字符串!这样可以节省很多字节。
AdmBorkBork

8

的JavaScript(SpiderMonkey的) 106个105  104字节

“有疑问时,只需对血腥的输入进行哈希处理即可。”

a=>a.sort((a,b)=>(g=s=>'i0008bar5dj60007f3001p09mseqg0hk40cnl2o'[parseInt(s,36)*50%257%170%40])(a)>g(b))

在线尝试!


之前我已经看过(并给基于哈希的解决方案留下深刻的印象)。关于魔术字符串/乘数/ mod值的生成方式,到处都有解释吗?在找到一组为每种可能的颜色输入提供唯一输出的值之前,还是只是蛮力,还是有一种更聪明的方法?
杰克·布劳恩斯坦

1
@JackBrounstein这只是一种快速而肮脏的蛮力搜索,尝试使用随机值并仅在不考虑完整链的长度的情况下(仅在最后一次模运算之后)将最大输出最小化(例如%99%55,并不比%123%55使用这种方法更好)。因此,它肯定不是最佳的。不过,稍后我可能会尝试一些更复杂的方法。
Arnauld

6

果冻,28个字节

“½Ṗ©cƘʂẒẹMMỤẓHP’Œ?“ðÑþQ’,ḥµÞ

在线尝试!

怎么运行的

µ将其左侧的所有内容转换为单子链,该单子链Þ映射到输入数组并根据生成的值对输入进行排序。

“½Ṗ©cƘʂẒẹMMỤẓHP’ 将返回值设置为176073885534954276276199526358143331。

Œ?[20,28,15,3,5,26,18,16,8,30,4,25,2,21,22,11,24,1,23,10,29,12,17,27,14,9,6,13,7,19]

“ðÑþQ’产量为391695582;产率为109。,将其添加到排列中。然后,Çompute果冻的391695582 散列函数,映射所得到的桶的排列的整数。

魔术常数391695582由Jelly的utils发现。

dennis-home:utils$ time ./findhash 30 29 <<< '["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"]'
391695582

real    0m2.058s
user    0m15.077s
sys     0m0.023s

1
353690280752在29个存储桶中对29种颜色进行哈希处理,但又需要一个字节进行编码。使用大写(332849952364)或标题大写(862442225888)时也会出现28个字节。
丹尼斯

5

Python 3,93字节

lambda r:sorted(r,key=lambda s:'iV^ZzwnFM@pYuOobXGAKyf[tUR]E'.find(chr(int(s,36)%127%60+64)))

在线尝试!

读取每种颜色为base-36 int。蛮力模数,并在不需要转义的19个中选择任意偏移。


4

的powershell,124个 120 124 119 118 102字节

$args|sort{$c=$_
'bluOrPiWPuG*yRusLeARoSiCriCrMCGoLFVOlRuPOBlSBGYR'-csplit'(?=[A-Z])'|%{$c-like"$_*"}}

在线尝试!

说明:

  1. 数据字符串按降序包含颜色标签的前一个有效字母。除了Grey标签- G*y较短。

  2. -csplit'(?=[A-Z])' 将数据字符串拆分为数组 (blu,Or,Pi,W,Pu,G*y,Rus,Le,A,Ro,Si,Cri,Cr,M,C,Go,L,F,V,Ol,Ru,P,O,Bl,S,B,G,Y,R)

  3. |%{$c-like"$_*"}将字符串数组映射到布尔数组。其中的True意思是“颜色标签从该字符串开始”(例如,不区分大小写的运算符,csplit-区分大小写。请参阅doc)。

  4. sort{}按布尔数组升序对颜色标签进行分类。

按数组排序是Powershell中非常有趣的功能。在此脚本中,所有数组都具有相同的长度,并且仅包含布尔值。此排序按布尔数组的字典顺序执行。

因此,字符串可以包含最后一个标签的一个字母的缩写。如果数组的开头有匹配项,则结尾处的匹配项无效。

       blu Or Pi W Pu G*y Rus Le A Ro Si Cri Cr M C Go L F V Ol Ru P O Bl S B G Y R
green: -   -  -  - -  -   -   -  - -  -  -   -  - - -  - - - -  -  - - -  - - T - -
gold : -   -  -  - -  -   -   -  - -  -  -   -  - - T  - - - -  -  - - -  - - T - -
grey : -   -  -  - -  T   -   -  - -  -  -   -  - - -  - - - -  -  - - -  - - T - -

     : green < gold < grey

T现在true-现在在哪里false


测试脚本:

$f = {

$args|sort{$c=$_
'bluOrPiWPuG*yRusLeARoSiCriCrMCGoLFVOlRuPOBlSBGYR'-csplit'(?=[A-Z])'|%{$c-like"$_*"}}

}

@(
    ,( @(), @() )
    ,( ('green', 'blue', 'red', 'brown'), ('red', 'green', 'brown', 'blue') )
    ,( ("gold", "grey", "green"), ("green", "gold", "grey") )
    ,( ("ruby","yellow","red","grey"), ("red", "yellow", "ruby", "grey") )
    ,( ("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"),
       ("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") )
) | % {
    $inp,$expected = $_
    $result = &$f @inp  # splatting
    "$("$result"-eq"$expected"): $result"
}

输出:

True:
True: red green brown blue
True: green gold grey
True: red yellow ruby grey
True: 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

我有This site can’t be reached错 抱歉。
mazzy

1
为您添加了修订的TIO。
毛茸茸的

1
我今天已经设法为TIO获得了新的IP。它仍然为您阻止吗?
丹尼斯

它还活着!!!凉!谢谢!
凌晨20:51


3

Wolfram语言255 213 199字节

丹尼斯(Dennis)使用符号代替字符串,从而避免了“”标记,从而节省了14个字节。

SortBy[#,{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}~Position~#&]&

在线尝试!


2

Python 2 186字节

lambda a:[y for x,y in sorted((max(" 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 ".find(" %s "%c[:i+1])for i,j in enumerate(c)),c)for c in a)]

在线尝试!

查找标识符字符串中渐进字符子字符串的所有匹配项(例如:“绿色”将检查“ g”,“ gr”,“ gre”,“ gree”和“绿色”),并保留最大索引。“ red”始终是第一个,anf find()对于缺少的匹配项返回-1,因此没有专门用于red的标识符。

一旦颜色变成了(索引,颜色)对,就在该对的第一项上对数组进行排序,然后丢弃每对的第一项。


2

Python 3,130个字节

lambda*a:sorted(a,key=lambda c:("r,ylgebwsrtbcorpcryovvlfnlagdccamvca cmnsvrearlorsgyppwtpkonbe".find(c[::3]+" "*(c=="cream")),c))

在线尝试!


@毛茸茸的我不这么认为吗?你可以给我一个输入和预期输出
HyperNeutrino

@AdmBorkBork谢谢,才意识到Shaggy的意思是什么。我只是互相比较绿色和灰色,哈哈
HyperNeutrino 18/12/19

为了增加Shaggy和AdmBorkBork的评论,“绿色”和“灰色”都以“ gre”开头。
DavidC

我认为@Shaggy已修正
HyperNeutrino

1
@Shaggy修复它,谢谢。奶油需要一点技巧,因为它cacca巧克力燕麦的子串。
HyperNeutrino

2

C#(Visual C#中交互式编译器)321个 219 210 161 159 138字节

n=>n.OrderBy(a=>a!="grey"?"yelgrebroscablaochperuboliviofawlilgolchomaucrecrisilrosazulemruspurwhipinorablu".IndexOf(a.Substring(0,3)):65)

在线尝试!

-3个字节要感谢Shaggy,-18个要感谢TheLethalCoder

将输入作为List<string>,返回一个IOrderedEnumerable<string>

它的工作方式是按原始字符串中每个字符串的索引对列表进行排序。原始字符串的所有颜色(灰色除外)都变为前三个字母。灰色不存在,因为绿色和灰色会引起歧义。红色也不存在,因为IndexOf如果不出现字符串,则返回-1。

较短的版本,以IOrderedEnumerable作为输入,137个字节

n=>n.ThenBy(a=>a!="grey"?"yelgrebroscablaochperuboliviofawlilgolchomaucrecrisilrosazulemruspurwhipinorablu".IndexOf(a.Substring(0,3)):65)

利用了ThenBy比短1个字节的事实OrderBy,但ThenBy仅对进行操作IOrderedEnumerable

在线尝试!


认为您可以red从查询字符串中删除并替换6865以节省3个字节。在我的手机上,因此尚未对其进行全面测试。
毛茸茸的

您可以将内联字符串与142个字节的隐式返回一起使用。n=>n.OrderBy(a=>a!="grey"?"redyelgrebroscablaochperuboliviofawlilgolchomaucrecrisilrosazulemruspurwhipinorablu".IndexOf(a.Substring(0,3)):68);但是,您需要using在字节数中包含s,因为它们需要运行。您可以使用名称空间技巧来缩短所需的计数。
TheLethalCoder

如果使用命名空间技巧,是否必须将命名空间添加到字节数中?
无知的体现,

我可以将编译器更改为Visual C#交互式编译器,然后不必添加使用方法
Ignorance的实施例

转换为Interactive的好主意,但是是的,如果您做了名称空间技巧,则必须将其包括在字节数中。本质上是添加namespace System.Linq{}或您选择的任何内容。
TheLethalCoder

1

木炭69 68 63 56字节

F⪪”E$↥l∧_∧K⁰RY⸿h⊕ψVG⌕gbW⟧⁼″5/²H✳<◨A³?ω↗⊘‴⭆1”²Φθ№κ⎇Σιlilι

在线尝试!链接是详细版本的代码。说明:

F⪪”E$↥l∧_∧K⁰RY⸿h⊕ψVG⌕gbW⟧⁼″5/²H✳<◨A³?ω↗⊘‴⭆1”²

取压缩的字符串edyeeebrscckhrpeubivvifa99gohomaamrisiosazemuseypuwhpiorlu并遍历长度为2的每个子字符串。

Φθ№κ⎇Σιlilι

对于每个子字符串,请打印包含该子字符串的输入字符串,除非该子字符串为99,在这种情况下,请查找lil。(lilac是唯一没有唯一的两个字母的子字符串的颜色;oliveincludes li; silverincludes ilblackincludes lacfawn并且仅azure可以使用单个字母进行检测,但这在这里无济于事。)


1

Pyth,66 个字节

oxc."ayÇæ£ðÐ¥~@iF[2BÍÐ:Yë)^ksTTã"2s@LN,03

在此处在线尝试,或在此处一次验证所有测试用例。

列表中的颜色可以通过采用index 03(假设采用模块化索引)中的字符来唯一标识。这将导致以下映射:

rr -> red
yl -> yellow
ge -> green
bw -> brown
sr -> scarlet
bc -> black
or -> ochre
pc -> peach
ry -> ruby
ov -> olive
vl -> violet
fn -> fawn
la -> lilac
gd -> gold
cc -> chocolate
mv -> mauve
ca -> cream
cm -> crimson
sv -> silver
re -> rose
ar -> azure
lo -> lemon
rs -> russet
gy -> grey
pp -> purple
wt -> white
pk -> pink
on -> orange
be -> blue

完整说明:

oxc."..."2s@LN,03Q   Implicit: Q=eval(input())
                     Trailing Q inferred, dictionary string replaced with ... for brevity
o                Q   Order the elements of Q, as N, using:
              ,03      [0,3]
           @LN         Get the characters at the above indices in N
          s            Concatenate into a string
                         The above is result {1}
   ."..."              The compressed dictionary string
  c      2             Split into chunks of length 2
 x                     Get the index of {1} in the above
                       Implicit print of sorted list

1

05AB1E,48 个字节

Σ.•Aå₂мÕh∊þèmvƶ\kΛ1YŠíJ>J#θ₁2©€,Ù{η¦ù-•2ôy¬s3è«k

与大多数其他答案相同的解决方案。稍后将尝试从这里打高尔夫球。

在线尝试验证所有测试用例

说明:

Σ          # Sort the (implicit) input-list by:
 .•Aå₂мÕh∊þèmvƶ\kΛ1YŠíJ>J#θ₁2©€,Ù{η¦ù-•
           #  Push compressed string "rrylgebwsrbcorpcryovvlfnlagdccmvcacmsvrearlorsgyppwtpkonbe"
  2ô       #  Split into parts of size 2
 y         #  Push the current string of the list we're sorting
  ¬        #  Push its head (without popping)
   s       #  Swap so the string is at the top of the stack again
    3è     #  Get the character at index 3 (with automatic wraparound)
      «    #  Merge both characters together
       k   #  And get the index in the compressed string to sort on

看到这个05AB1E尖端(部分如何压缩字符串不是字典的一部分吗?,以了解如何.•Aå₂мÕh∊þèmvƶ\kΛ1YŠíJ>J#θ₁2©€,Ù{η¦ù-•"rrylgebwsrbcorpcryovvlfnlagdccmvcacmsvrearlorsgyppwtpkonbe"

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.