你是一位研究被遗忘已久的语言的历史学家。你刚刚发现了一块粘土石碑,它以字母顺序列出了语言中所有已知的单词。你的任务是找到这个字母的顺序,如果它存在的话。
给定一个有序的单词列表,尝试输出一个有序的字符列表,以便:
如果(而且只有当)没有字符列表能够满足这些要求,则输出您所选择的错误/失败值。您必须在回答中指定所选择的值。
值得注意的是,多个字母排序对于给定的单词列表是有效的;如果我们有单词Cod、Com和Dy,可以假定C在D之前,d在m前面。字母o和y在生成的字母表中有不同的有效位置。为了更清楚起见,以下是该单词列表的一些有效输出(但不是全部):
Input: Cod Com Dy
Output: CDdmoy
Output: CDdmyo
Output: CDdomy
Output: CoDdym
Output: dmCDoy
Output: dCmDoy
Output: CdoymD
Output: dyCoDm
Output: yodmCD如果有多个有效字符列表,则代码必须一致地输出一个有效答案或所有有效答案。
[A-Za-z0-9]都可以作为字符包含在单词列表中。您的正确输出可能不匹配所有这些,因为任何给定的单词列表可能有多个正确的字母表。有些人可能只有一个有效的答案。
Input: bob cat chat dog hat hot guy
Output: tuybcdaohg
Input: A Aa AA Bb Ba
Output: baAB
Input: 147 172 120 04 07
Output: 14720这些都应该是错误的输出,并且可以用作测试用例。
Input: GREG HOUSE ROCK AND ROLL
Output: [falsy]
Input: b5OtM bb5O MO Mtt 5
Output: [falsy]
Input: a aa aaa aaaaa aaaa
Output: [falsy]这是密码-高尔夫,最短的代码获胜。:)
发布于 2021-10-20 03:05:39
∑UṖ'→?µ←:₃[nL|β];?⁼;h输出一个可能的字母表。使用较大字母的输入超时,因为它检查输入字符集的所有排列。退出时若有错误,则为falsey。
14如果a aa aaa aaaaa aaaa不是falsey的话。如果基地转换不是那么简陋的话,时间就会短得多。
∑UṖ'→?µ←ðpβ;?⁼;h
∑U # Get all unique characters of the input
Ṗ # and then all permutations of that.
' # From that, keep only items (n) where
→?µ←ðp; # the input sorted by conversion to bijective base n with a space prepended to make everything non-zero
?⁼ # equals the original input (⁼ is non-vectorising equals)
;h # and get the first from that实际上,我需要使用鬼变量和变量范围限定一次。
发布于 2021-10-20 07:48:23
返回所有有效的字母表和空列表作为falsy值。
FQŒ!iⱮⱮṢƑ¥ƇF -- flatten the word list into a single string
Q -- get the unique characters
Œ! -- get all permutations of those characters
Ƈ -- keep the permutation that return a truthy value for:
¥ -- group the last two links into dyad
-- which is called with the permutation on the left and the word list on the right
iⱮⱮ -- for each character of each word get its index in the permutation
ṢƑ -- is the resulting list invariant under sorting?发布于 2021-10-20 06:36:04
a=>(g=s=>s[0]?g(s.filter(c=>a.some(w=>s.includes((r=/(.*)(.?).*,\1(.)/.exec([p,p=w]))[2])&&r[3]==c,p='')||!(q+=c))):q)([...new Set(a.join(q=''))])https://codegolf.stackexchange.com/questions/236452
复制相似问题