此挑战基于D. Parnas,关于将系统分解为模块的标准中描述并在J. Morris,函数式语言的实程序设计中阐述的一个问题。
编写一个程序或函数,该程序或函数以合理、方便的格式为您的语言提供一个从stdin或作为参数的书名列表。例如,
Green Sleeves
Time Was Lost或
("Green Sleeves";"Time Was Lost")向stdout返回或打印按字母顺序排列的关键字列表,通过在角大括号(<和>)中封装每个关键字,在原始标题中显示它们的上下文。与输入一样,输出可以采用合理的格式,方便您的语言--换行符分隔行、字符串列表等:
<Green> Sleeves
Time Was <Lost>
Green <Sleeves>
<Time> Was Lost
Time <Was> Lost标题将由一系列由单个空格分隔的关键字组成。关键字将只包含字母字符。关键字将被排序为词典学。标题将是唯一的,关键字将是唯一的在每个标题,但相同的关键字可能存在于几个标题。如果一个关键字存在于多个标题中,则输出应该以任意顺序列出每个外观。例如,考虑到这种输入:
A Dugong
A Proboscis一个有效的输出是:
<A> Proboscis
<A> Dugong
A <Dugong>
A <Proboscis>或者:
<A> Dugong
<A> Proboscis
A <Dugong>
A <Proboscis>这是密码-高尔夫- -胜利者是以字节为单位的最短解。标准的漏洞是不允许的。
发布于 2015-11-27 18:19:16
也许可以缩短一些,但我不知道怎么.
P+UqR £XqS m_+S+X) q', n £Xs1+XbS)rXs0,XbS),@"<{X}>")qRP+UqR m@XqS m_+S+X) q', n m@Xs1+XbS)rXs0,XbS),@"<{X}>")qR
// Implicit: U = input string, S = a space, P = empty string
UqR m@ // Split input at newlines, then map each item X to:
XqS m_ // X split at spaces, with each item Z mapped to:
+S+X) // Z + a space + X.
P+ q', // Parse the result as a string, and split at commas. Due to JS's default
// array-to-string conversion (joining with commas), this flattens the array.
n // Sort the result lexicographically.
m@Xs1+XbS // Map each item X to everything after the first space,
rXs0,XbS // replacing the original keyword with
@"<{X}>" // "<" + keyword + ">".
qR // Join the result with newlines.
// Implicit: output last expression发布于 2015-11-27 18:37:50
import Data.List
g l=[(m,h++('<':m++['>']):t)|(h,m:t)<-zip(inits l)$tails l]
f=map(unwords.snd).sort.(>>=g.words)用法示例:f ["Green Sleeves","Time Was Lost"] -> ["<Green> Sleeves","Time Was <Lost>","Green <Sleeves>","<Time> Was Lost","Time <Was> Lost"]。
https://codegolf.stackexchange.com/questions/64995
复制相似问题