คำสำคัญชื่อเรื่องในบริบท


10

ความท้าทายนี้จะขึ้นอยู่กับปัญหาที่อธิบายไว้ในD. Parnas บนเกณฑ์ที่จะใช้ในการย่อยสลายระบบเป็นโมดูลและเนื้อหาตามที่เจมอร์ริส, การเขียนโปรแกรมจริงในการทำงานภาษา

เขียนโปรแกรมหรือฟังก์ชั่นที่ใช้รายการหนังสือจาก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

ชื่อจะประกอบด้วยชุดของคำหลักคั่นด้วยช่องว่างเดียว คำหลักจะมีเพียงตัวอักษรตามตัวอักษร คำหลักคือเพื่อจะแยกlexicographically ชื่อจะไม่ซ้ำกันและคำหลักจะไม่ซ้ำกันในแต่ละชื่อ แต่คำหลักเดียวกันอาจมีอยู่ในหลายชื่อ หากคำหลักมีอยู่มากกว่าหนึ่งชื่อเรื่องเอาต์พุตควรแสดงแต่ละลักษณะที่ปรากฏตามลำดับที่ต้องการ ตัวอย่างเช่นกำหนดอินพุตนี้:

A Dugong
A Proboscis

ผลลัพธ์ที่ถูกต้องจะเป็น:

<A> Proboscis
<A> Dugong
A <Dugong>
A <Proboscis>

หรือ:

<A> Dugong
<A> Proboscis
A <Dugong>
A <Proboscis>

นี่คือ - ผู้ชนะคือทางออกที่สั้นที่สุดในหน่วยไบต์ ช่องโหว่มาตรฐานไม่ได้รับอนุญาต


จะเกิดอะไรขึ้นหากคำหลักเดียวกันเกิดขึ้นมากกว่าหนึ่งครั้งในชื่อ เหตุการณ์ที่เกิดขึ้นควรอยู่ในลำดับที่ปรากฎหรือได้รับอนุญาตตามลำดับ
ปีเตอร์เทย์เลอร์

1
@PeterTaylor: จากสเป็ค "ชื่อจะไม่ซ้ำกันและคำหลักจะไม่ซ้ำกันในแต่ละชื่อ ... "
JohnE

คำตอบ:


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 Quoting จาก OP keywords will be unique within each titleที่
PurkkaKoodari

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

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.