上下文中的标题关键字


10

这个挑战是基于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>

这是 -获胜者是最短的字节解决方案。不允许出现标准漏洞。


如果同一关键字在标题中出现多次,该怎么办?应该按照出现的顺序出现,还是允许任意顺序出现?
彼得·泰勒

1
@PeterTaylor:从规范来看,“标题将是唯一的,并且每个标题中的关键字将是唯一的……”
JohnE

Answers:


4

Pyth,25 24 22字节

VSsm,Rdcd\ QAN:HGjG"<>

在线尝试。

将输入作为行的数组,例如["Green Sleeves","Time Was Lost"]

说明

VSsm,Rdcd\ QAN:HGjG"<>   implicit: Q = evaluated input

   m       Q                   map input lines:
       cd\                         split input line to words
    ,Rd                          replace each word by pair [word, entire line]
  s                          concatenate results for all input lines
 S                         sort the array of pairs lexicographically
V                        loop over the array
            AN             assign the word to G and the corresponding line to H
                 jG"<>       put the word between <>
              :HG          replace the word by the above in the line and print

看起来有问题 -检查标题的处理Time Was Time
彼得·泰勒

3
@PeterTaylor从OP引用,keywords will be unique within each title
PurkkaKoodari 2015年

2

Japt,55个字节

也许可以将其缩短一些,但是我不确定如何...

P+UqR £XqS m_+S+X) q', n £Xs1+XbS)rXs0,XbS),@"<{X}>")qR

怎么运行的

P+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


1

Haskell,113个字节

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

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.