ลบรายการที่ซ้ำออกจากสตริง


17

ได้รับแรงบันดาลใจจากคำถาม StackOverflow ที่ยังไม่เกิดขึ้น

ความคิดนั้นง่าย กำหนด String และอาร์เรย์ของ Strings ให้ลบอินสแตนซ์ของคำใด ๆ ในอาร์เรย์ (ไม่สนใจขนาดตัวพิมพ์) จากอินพุตสตริงอื่นที่ไม่ใช่แถวแรกพร้อมกับช่องว่างเพิ่มเติมที่อาจทำให้เกิด คำต้องตรงกับคำทั้งหมดในสตริงป้อนเข้าไม่ใช่ส่วนของคำ

เช่น"A cat called matt sat on a mat and wore a hat A cat called matt sat on a mat and wore a hat", ["cat", "mat"]ควรส่งออก"A cat called matt sat on a mat and wore a hat A called matt sat on a and wore a hat"

อินพุต

  • อินพุตสามารถใช้เป็นสตริงได้และอาร์เรย์ของ Strings หรืออาร์เรย์ของสตริงที่อินพุตสตริงเป็นองค์ประกอบแรก พารามิเตอร์เหล่านี้สามารถอยู่ในลำดับใดก็ได้
  • สตริงอินพุตอาจไม่ถูกใช้เป็นรายการของสตริงที่คั่นด้วยช่องว่าง
  • สตริงอินพุตจะไม่มีช่องว่างนำหน้าต่อท้ายหรือเว้นวรรคติดต่อกัน
  • อินพุตทั้งหมดจะมีเฉพาะอักขระ [A-Za-z0-9] โดยมีข้อยกเว้นของสตริงอินพุตรวมถึงช่องว่างด้วย
  • อาร์เรย์อินพุตอาจว่างเปล่าหรือมีคำที่ไม่อยู่ในสตริงป้อนเข้า

เอาท์พุต

  • เอาต์พุตสามารถเป็นค่าส่งคืนจากฟังก์ชันหรือพิมพ์ไปที่ STDOUT
  • เอาต์พุตจะต้องอยู่ในกรณีเดียวกันกับ String ดั้งเดิม

กรณีทดสอบ

the blue frog lived in a blue house, [blue] -> the blue frog lived in a house
he liked to read but was filled with dread wherever he would tread while he read, [read] -> he liked to read but was filled with dread wherever he would tread while he
this sentence has no matches, [ten, cheese] -> this sentence has no matches
this one will also stay intact, [] -> this one will also stay intact
All the faith he had had had had no effect on the outcome of his life, [had] -> All the faith he had no effect on the outcome of his life
5 times 5 is 25, [5, 6] -> 5 times is 25
Case for different case, [case] -> Case for different
the letters in the array are in a different case, [In] -> the letters in the array are a different case
This is a test Will this be correct Both will be removed, [this,will] -> This is a test Will be correct Both be removed

เช่นนี้เป็นรหัสกอล์ฟจำนวนไบต์ต่ำสุดชนะ!

คำตอบ:


9

R , 84 ไบต์

function(s,w,S=el(strsplit(s," ")),t=tolower)cat(S[!duplicated(x<-t(S))|!x%in%t(w)])

ลองออนไลน์!

น้อยกว่า 100 ไบต์ในความท้าทายที่ไม่ยัง ?

คำอธิบาย:

หลังจากที่เราแยกสตริงออกเป็นคำแล้วเราต้องแยกคำเหล่านั้นออก

  1. ซ้ำซ้อนและ
  2. ใน w

หรืออีกทางหนึ่งคือหมุนมันบนหัวของมัน

  1. การเกิดขึ้นครั้งแรกของคำหรือ
  2. wไม่ได้อยู่ใน

duplicatedส่งคืนดัชนีโลจิคัลอย่างเป็นระเบียบของสิ่งที่ไม่ได้เกิดขึ้นครั้งแรกดังนั้น!duplicated()ส่งกลับดัชนีของดัชนีที่เกิดขึ้นครั้งแรกและx%in%wส่งกลับดัชนีโลจิคัลสำหรับxสิ่งที่อยู่ในwนั้น เรียบร้อย


6

Java 8, 117 110 ไบต์

a->s->{for(String x:a)for(x="(?i)(.*"+x+".* )"+x+"( |$)(.*)";s.matches(x);s=s.replaceAll(x,"$1$3"));return s;}

คำอธิบาย:

ลองออนไลน์

a->s->{                // Method with String-array and String parameters and String return
  for(String x:a)      //  Loop over the input-array
    for(x="(?i)(.*"+x+".* )"+x+"( |$)(.*)";
                       //   Regex to match
        s.matches(x);  //   Inner loop as long as the input matches this regex
      s=s.replaceAll(x,"$1$3")); 
                       //    Replace the regex-match with the 1st and 3rd capture groups
  return s;}           //  Return the modified input-String

คำอธิบายเพิ่มเติมสำหรับ regex:

(?i)(.*"+x+".* )"+x+"( |$)(.*)   // Main regex to match:
(?i)                             //  Enable case insensitivity
    (                            //  Open capture group 1
     .*                          //   Zero or more characters
       "+x+"                     //   The input-String
            .*                   //   Zero or more characters, followed by a space
               )                 //  End of capture group 1
                "+x+"            //  The input-String again
                     (           //  Open capture group 2
                       |$        //   Either a space or the end of the String
                         )       //  End of capture group 2
                          (      //  Open capture group 3
                           .*    //   Zero or more characters
                             )   //  End of capture group 3

$1$3                             // Replace the entire match with:
$1                               //  The match of capture group 1
  $3                             //  concatted with the match of capture group 3

4

MATL , 19 18 ไบต์

"Ybtk@kmFyfX<(~)Zc

อินพุตคือเซลล์อาร์เรย์ของสตริงจากนั้นสตริง

ลองออนไลน์! หรือตรวจสอบกรณีทดสอบทั้งหมด

มันทำงานอย่างไร

"        % Take 1st input (implicit): cell array of strings. For each
  Yb     %   Take 2nd input (implicit) in the first iteration: string; or
         %   use the string from previous iteration. Split on spaces. Gives
         %   a cell array of strings
  tk     %   Duplicate. Make lowercase
  @k     %   Push current string from the array taken as 1st input. Make
         %   lowercase
  m      %   Membership: gives true-false array containing true for strings
         %   in the first input argument that equal the string in the second
         %   input argument
  F      %   Push false
  y      %   Duplicate from below: pushes the true-false array again
  f      %   Find: integer indices of true entries (may be empty)
  X<     %   Minimum (may be empty)
  (      %   Assignment indexing: write false in the true-false array at that
         %   position. So this replaces the first true (if any) by false
  ~      %   Logical negate: false becomes true, true becomes false
  )      %   Reference indexing: in the array of (sub)strings that was
         %   obtained from the second input, keep only those indicated by the
         %   (negated) true-false array
  Zc     %   Join strings in the resulting array, with a space between them
         % End (implicit). Display (implicit)

3

Perl 5 , 49 ไบต์

@B=<>;$_=join$",grep!(/^$_$/xi~~@B&&$v{+lc}++),@F

ลองออนไลน์!

บันทึกแล้ว 9 (!!) ไบต์ขอบคุณ@TonHospel !


1
นี้ดูเหมือนว่าจะล้มเหลว+This is a test Will this be correct Both will be removed this willคำสองคำที่สองถูกลบออกอย่างถูกต้อง แต่ยังลบคำbeที่สองหลังwillด้วยเหตุผลบางอย่าง
Kevin Cruijssen

1
@KevinCruijssen อืมฉันเห็นได้เลยว่าทำไมมันถึงเกิดขึ้นในตอนนี้ ฉันจะลองดูมื้อเที่ยงอย่างเหมาะสมในวันพรุ่งนี้ แต่ตอนนี้ฉันคงที่ +4 แล้ว ขอบคุณสำหรับการให้ฉันรู้ว่า!
Dom Hastings

สำหรับ 49:@B=<>;$_=join$",grep!(/^$_$/xi~~@B&&$v{+lc}++),@F
Ton Hospel

@ TonHospel Ahh ใช้เวลาในขณะที่พยายามรับlcการเรียกโดยไม่ต้อง parens ! น่ากลัว และการใช้ regex เทียบกับ array นั้นดีกว่ามากขอบคุณ! ฉันพยายามจำเคล็ดลับทั้งหมดของคุณ!
Dom Hastings

2

Pyth, 27 ไบต์

jdeMf!}r0eT@mr0dQmr0dPT._cz

ลองออนไลน์

คำอธิบาย

jdeMf!}r0eT@mr0dQmr0dPT._cz
                          z  Take the string input.
                       ._c   Get all the prefixes...
    f    eT@                 ... which end with something...
     !}         Q    PT      ... which is not in the input and the prefix...
       r0   mr0d mr0d        ... case insensitive.
jdeM                         Join the ends of each valid prefix.

ฉันแน่ใจว่าการตรวจสอบแบบไม่ตอบสนองต่อ case จะลดลง 10 ไบต์ แต่ฉันไม่ทราบวิธี


2

Stax , 21 ไบต์CP437

åìøΓ²¬$M¥øHΘQä~╥ôtΔ♫╟

25 ไบต์เมื่อคลายการแพค

vjcm[]Ii<;e{vm_]IU>*Ciyj@

ผลลัพธ์ที่ได้คืออาร์เรย์ เอาต์พุตที่สะดวกสบายสำหรับ Stax คือหนึ่งองค์ประกอบต่อบรรทัด

เรียกใช้และแก้ไขข้อบกพร่องออนไลน์!

คำอธิบาย

vj                           Convert 1st input to lowercase and split at spaces,
  c                          Duplicate at the main stack
   m                         Map array with the rest of the program 
                                 Implicitly output
    []I                      Get the first index of the current array element in the array
       i<                    Test 1: The first index is smaller than the iteration index
                                 i.e. not the first appearance
         ;                   2nd input
          {vm                Lowercase all elements
             _]I             Index of the current element in the 2nd input (-1 if not found)
                U>           Test 2: The index is non-negative
                                 i.e. current element is a member of the 2nd input
                  *C         If test 1 and test 2, drop the current element
                                 and go on mapping the next
                    iyj@     Fetch the corresponding element in the original input and return it as the mapped result
                                 This preserves the original case

2

Perl 6 , 49 ไบต์

->$_,+w{~.words.grep:{.lcw».lc||!(%){.lc}++}}

ทดสอบมัน

ขยาย:

->              # pointy block lambda
  $_,           # first param 「$_」 (string)
  +w            # slurpy second param 「w」 (words)
{

  ~             # stringify the following (joins with spaces)

  .words        # split into words (implicit method call on 「$_」)

  .grep:        # take only the words we want

   {
     .lc        # lowercase the word being tested
               # is it not an element of
     w».lc      # the list of words, lowercased

     ||         # if it was one of the words we need to do a secondary check

     !          # Boolean invert the following
                # (returns true the first time the word was found)

     (
       %        # anonymous state Hash variable
     ){ .lc }++ # look up with the lowercase of the current word, and increment
   }
}

2

Perl 5 , 50 48 ไบต์

รวม+1สำหรับ-p

ให้สตริงเป้าหมายตามด้วยคำตัวกรองแต่ละคำในบรรทัดแยกบน STDIN:

perl -pe '$"="|";s%\b(@{[<>]})\s%$&x!$v{lc$1}++%iegx;chop';echo
This is a test Will this be correct Both will be removed
this
will
^D
^D

chopเป็นสิ่งจำเป็นในการแก้ไขปัญหาพื้นที่ต่อท้ายในกรณีที่คำพูดสุดท้ายที่ได้รับการถอดออก

เพียงแค่รหัส:

$"="|";s%\b(@{[<>]})\s%$&x!$v{lc$1}++%iegx;chop

ลองออนไลน์!



1

K4 , 41 ไบต์

วิธีการแก้:

{" "/:x_/y@>y:,/1_'&:'(_y)~/:\:_x:" "\:x}

ตัวอย่าง:

q)k){" "/:x_/y@>y:,/1_'&:'(_y)~/:\:_x:" "\:x}["A cat called matt sat on a mat and wore a hat A cat called matt sat on a mat and wore a hat";("cat";"mat")]
"A cat called matt sat on a mat and wore a hat A called matt sat on a and wore a hat"

q)k){" "/:x_/y@>y:,/1_'&:'(_y)~/:\:_x:" "\:x}["Case for different case";enlist "case"]
"Case for different"

q)k){" "/:x_/y@>y:,/1_'&:'(_y)~/:\:_x:" "\:x}["the letters in the array are in a different case";enlist "In"]
"the letters in the array are a different case"

q)k){" "/:x_/y@>y:,/1_'&:'(_y)~/:\:_x:" "\:x}["5 times 5 is 25";(1#"5";1#"6")]
"5 times is 25"

คำอธิบาย:

แยกบนช่องว่างตัวพิมพ์เล็กทั้งสองมองหารายการที่ตรงกันลบทั้งหมดยกเว้นเหตุการณ์ที่เกิดขึ้นครั้งแรกเข้าร่วมสตริงกลับเข้าด้วยกัน

{" "/:x_/y@>y:,/1_'&:'(_y)~/:\:_x:" "\:x} / the solution
{                                       } / lambda with implicit x & y args
                                  " "\:x  / split (\:) on whitespace " "
                                x:        / save result as x
                               _          / lowercase x
                          ~/:\:           / match (~) each right (/:), each left (\:)
                      (_y)                / lowercase y
                   &:'                    / where (&:) each ('), ie indices of matches
                1_'                       / drop first of each result
              ,/                          / flatten
            y:                            / save result as y
         y@>                              / descending indices (>) apply (@) to y
      x_/                                 / drop (_) from x
 " "/:                                    / join (/:) on whitespace " "

1

JavaScript (Node.js) , 75 ไบต์

f=(s,a)=>a.map(x=>s=s.replace(eval(`/\\b${x}\\b */ig`),s=>i++?"":s,i=0))&&s

ลองออนไลน์!


1
เนื่องจากนี่ไม่ใช่ฟังก์ชันแบบเรียกซ้ำคุณไม่จำเป็นต้องรวมf=จำนวนไบต์ของคุณ นอกจากนี้คุณยังสามารถบันทึกไบต์โดย currying พารามิเตอร์แทนที่(s,a)=>ด้วยแล้วโทรฟังก์ชั่นที่มีs=>a=> f(s)(a)
Shaggy

@ Shaggy ใช่ แต่ฉันสนใจเกี่ยวกับการเล่นกอล์ฟนิยามของฟังก์ชั่นเพราะข้อตกลงหลักคือการเล่นกอล์ฟร่างกาย แต่ขอบคุณครับเคล็ดลับที่ดี :)
DanielIndie

1

JavaScript ES6, 78 ไบต์

f=(s,a,t={})=>s.split` `.filter(w=>a.find(e=>w==e)?(t[w]?0:t[w]=1):1).join` `

มันทำงานอย่างไร:

f=(s,a,t={})=> // Function declaration; t is an empty object by default
s.split` ` // Split the string into an array of words
.filter(w=> // Declare a function that, if it returns false, will delete the word
  a.find(e=>w==e) // Returns undeclared (false) if the word isn't in the list
  ?(t[w]?0 // If it is in the list and t[w] exists, return 0 (false)
    :t[w]=1) // Else make t[w] exist and return 1 (true)
  :1) // If the word isn't in the array, return true (keep the word for sure)
.join` ` // Rejoin the string

2
ยินดีต้อนรับสู่ PPCG! เนื่องจากคุณไม่ได้ใช้ชื่อฟังก์ชันสำหรับการโทรซ้ำฟังก์ชั่นชื่อนี้ยังจะเป็นส่งที่ถูกต้องเพื่อให้คุณสามารถบันทึกไบต์ที่สองโดยทิ้งf f=
Martin Ender

ยินดีต้อนรับสู่ PPCG! น่าเศร้าที่สิ่งนี้ล้มเหลวเมื่อมีหลายกรณีที่เกี่ยวข้อง
ขนด

หากไม่ใช่สำหรับสิ่งนั้นคุณสามารถเอามันลงไปได้ 67 ไบต์
Shaggy

@MartinEnder ขอบคุณสำหรับเคล็ดลับ!
เอียน

@Shaggy ใช้อาร์เรย์อินพุตเนื่องจากวัตถุเป็นความคิดที่น่าสนใจที่ฉันไม่เคยคิด ฉันจะลองและแก้ไขปัญหากรณี
เอียน

0

PowerShell v3 หรือใหม่กว่า 104 ไบต์

Param($s,$w)$w|?{$_-and$s-match($r="\b$_(?: |$)")}|%{$h,$t=$s-split$r;$s="$h$($Matches.0)$(-join$t)"};$s

ค่าใช้จ่ายของหนึ่งไบต์ก็สามารถทำงานใน PS 2.0 โดยการแทนที่ด้วย $Matches.0$Matches[0]

รุ่นยาว:

Param($s, $w)
$w | Where-Object {$_ -and $s -match ($r = "\b$_(?: |$)")} |    # Process each word in the word list, but only if it matches the RegEx (which will be saved in $r).
    ForEach-Object {                                            # \b - word boundary, followed by the word $_, and either a space or the end of the string ($)
        $h, $t = $s -split $r                                   # Split the string on all occurrences of the word; the first substring will end up in $h(ead), the rest in $t(ail) (might be an array)
        $s = "$h$($Matches.0)$(-join $t)"                       # Create a string from the head, the first match (can't use the word, because of the case), and the joined tail array
    }
$s                                                              # Return the result

การใช้งาน
บันทึกเป็น Anything.ps1 และโทรด้วยสตริงและคำเป็นอาร์กิวเมนต์ หากต้องมีมากกว่าหนึ่งคำต้องส่งผ่านคำนั้นต้องอยู่ในเครื่องหมาย @ ():

.\Whatever.ps1 -s "A cat called matt sat on a mat and wore a hat A cat called matt sat on a mat and wore a hat" -w @("cat", "mat")

ทางเลือกที่ไม่มีไฟล์ (สามารถวางโดยตรงลงในคอนโซล PS):
บันทึกสคริปต์เป็น ScriptBlock (ในวงเล็บปีกกา) ในตัวแปรจากนั้นเรียกใช้เมธอด Invoke () หรือใช้กับ Invoke-Command:

$f={Param($s,$w)$w|?{$_-and$s-match($r="\b$_(?: |$)")}|%{$h,$t=$s-split$r;$s="$h$($Matches.0)$(-join$t)"};$s}
$f.Invoke("A cat called matt sat on a mat and wore a hat A cat called matt sat on a mat and wore a hat", @("cat", "mat"))
Invoke-Command -ScriptBlock $f -ArgumentList "A cat called matt sat on a mat and wore a hat A cat called matt sat on a mat and wore a hat", @("cat", "mat")

0

Javascript, 150 ไบต์

s=(x, y)=>{let z=new Array(y.length).fill(0);let w=[];for(f of x)(y.includes(f))?(!z[y.indexOf(f)])&&(z[y.indexOf(f)]=1,w.push(f)):w.push(f);return w}

นอกเหนือจากปัญหาเกี่ยวกับการเล่นกอล์ฟ (ดูที่โซลูชัน JS อื่น ๆ สำหรับเคล็ดลับที่นั่น) นี่เป็นการป้อนข้อมูลครั้งแรกในรูปแบบของคำและส่งออกอาร์เรย์ของคำที่ไม่ได้รับอนุญาตจากสเป็คความท้าทาย นอกจากนี้ยังล้มเหลวเมื่อกรณีต่าง ๆ ที่เกี่ยวข้อง
ขนด

@Shaggy "ผลลัพธ์อาจเป็นค่าส่งคืนจากฟังก์ชัน" ดูเหมือนว่าจะส่งคืนค่าจากฟังก์ชันหรือไม่
aimorris

0

สะอาด , 153 142 138 134 ไบต์

import StdEnv,StdLib,Text
@ =toUpperCase
$s w#s=split" "s
=join" "[u\\u<-s&j<-[0..]|and[i<>j\\e<-w,i<-drop 1(elemIndices(@e)(map@s))]]

ลองออนไลน์!

กำหนดฟังก์ชั่น$ :: String [String] -> Stringให้ทำตามความท้าทายที่แท้จริง มันจะค้นหาและลบทุกเหตุการณ์ที่เกิดขึ้นหลังจากคำแรกสำหรับแต่ละคำเป้าหมาย


0

จอประสาทตา 46ไบต์

+i`(^|,)((.+),.*\3.* )\3( |$)
$2
.*,

-14 ไบต์ขอบคุณ @Neilและ +5 ไบต์สำหรับการแก้ไขข้อบกพร่อง

อินพุตในรูปแบบ word1,word2,word3,sentenceเนื่องจากฉันไม่แน่ใจว่าจะมีอินพุตแบบหลายบรรทัดได้อย่างไร (โดยที่อินพุตจะใช้แตกต่างกัน)

คำอธิบาย:

ลองออนไลน์

+i`(^|,)((.+),.*\3.* )\3( |$)   Main regex to match:
+i`                              Enable case insensitivity
   (^|,)                          Either the start of the string, or a comma
        (                         Open capture group 2
         (                         Open capture group 3
          .+                        1 or more characters
            )                      Close capture group 3
             ,                     A comma
              .*                   0 or more characters
                \3                 The match of capture group 3
                  .*               0 or more characters, followed by a space
                     )            Close capture group 2
                      \3          The match of capture group 2 again
                        ( |$)     Followed by either a space, or it's the end of the string
$2                              And replace everything with:
                                 The match of capture group 2

.*,                             Then get everything before the last comma (the list)
                                 and remove it (including the comma itself)

1
ในฐานะที่เป็นลายลักษณ์อักษรที่คุณสามารถลดความซับซ้อนของบรรทัดแรกไป+i`((.+),.*\2.* )\2( |$)และครั้งที่สองไป$1แต่ผมสังเกตเห็นว่ารหัสของคุณล้มเหลวในoften,he intended to keep ten geeseอยู่แล้ว
Neil

@ Neil ขอบคุณสำหรับการเล่นกอล์ฟ -14 และแก้ไขข้อบกพร่องด้วย +1
Kevin Cruijssen

... ยกเว้นว่าตอนนี้ล้มเหลวในหนึ่งในกรณีที่การทดสอบเดิม ...
นีล

@Neil Ah อ๊ะ .. แก้ไขอีกครั้งสำหรับ +4 ไบต์
Kevin Cruijssen

ข่าวดีก็คือฉันคิดว่าคุณสามารถใช้\bแทน(^|,)แต่ข่าวดีก็คือฉันคิดว่าคุณต้องการ\b\3\b(ยังไม่ได้คิดค้นกรณีทดสอบที่เหมาะสม)
Neil

0

สีแดง 98 ไบต์

func[s w][foreach v w[parse s[thru[any" "v ahead" "]any[to remove[" "v ahead[" "| end]]| skip]]]s]

ลองออนไลน์!

f: func [s w][ 
    foreach v w [                   ; for each string in the array
        parse s [                   ; parse the input string as follows:
            thru [                  ; keep everything thru: 
                any " "             ; 0 or more spaces followed by
                v                   ; the current string from the array followed by
                ahead " "           ; look ahead for a space
            ]
            any [ to remove [       ; 0 or more: keep to here; then remove: 
                " "                 ; a space followed by 
                v                   ; the current string from the array
                ahead [" " | end]]  ; look ahead for a space or the end of the string
            | skip                  ; or advance the input by one 
            ]
        ]
    ]
    s                               ; return the processed string 
]

0

Husk , 13 ไบต์

wüöVËm_Ṗ3+⁰ew

รับรายการสตริงและสตริงเดี่ยวเป็นอาร์กิวเมนต์ตามลำดับนี้ ถือว่ารายการนั้นไม่ซ้ำกัน ลองออนไลน์!

คำอธิบาย

wüöVËm_Ṗ3+⁰ew  Inputs: list of strings L (explicit, accessed with ⁰), string S (implicit).
               For example, L = ["CASE","for"], s = "Case for a different case".
            w  Split S on spaces: ["Case","for","a","different","case"]
 ü             Remove duplicates wrt an equality predicate.
               This means that a function is called on each pair of strings,
               and if it returns a truthy value, the second one is removed.
  öVËm_Ṗ3+⁰e    The predicate. Arguments are two strings, say A = "Case", B = "case".
           e    Put A and B into a list: ["Case","case"]
         +⁰     Concatenate with L: ["CASE","for","Case","case"]
       Ṗ3       All 3-element subsets: [["CASE","for","Case"],["CASE","for","case"],
                                        ["CASE","Case","case"],["for","Case","case"]]
  öV            Does any of them satisfy this:
    Ë            All strings are equal
     m_          after converting each character to lowercase.
                In this case, ["CASE","Case","case"] satisfies the condition.
               Result: ["Case","for","a","different"]
w              Join with spaces, print implicitly.

0

ต่ำสุด , 125 ไบต์

=a () =b a 1 get =c a 0 get " " split
(:d (b d in?) ((c d in?) (d b append #b) unless) (d b append #b) if) foreach
b " " join

อินพุตอยู่quotในสแต็กที่มีสตริงอินพุตเป็นองค์ประกอบแรกและหนึ่งquotในสตริงที่ซ้ำกันเป็นองค์ประกอบที่สองคือ

("this sentence has no matches" ("ten" "cheese"))


0

AWK , 120 ไบต์

NR%2{for(;r++<NF;)R[tolower($r)]=1}NR%2==0{for(;i++<NF;$i=$(i+s))while(R[x=tolower($(i+s))])U[x]++?++s:i++;NF-=s}NR%2==0

ลองออนไลน์!

ส่วน "ลบช่องว่าง" ทำให้สิ่งนี้ท้าทายยิ่งกว่าที่ฉันคิดไว้เล็กน้อย การตั้งค่าเขตข้อมูลเป็น""ลบเขตข้อมูล แต่ปล่อยให้มีตัวคั่นพิเศษ

ลิงก์ TIO มี 28 ไบต์พิเศษเพื่ออนุญาตให้มีหลายรายการ

อินพุตถูกให้มากกว่า 2 บรรทัด บรรทัดแรกคือรายการคำศัพท์และบรรทัดที่สองคือ "ประโยค" โปรดทราบว่า "คำว่า" และ "คำว่า" ไม่ถือว่าเหมือนกันกับเครื่องหมายวรรคตอนที่แนบมา การมีข้อกำหนดด้านเครื่องหมายวรรคตอนน่าจะทำให้ปัญหานี้สนุกยิ่งขึ้น


0

Ruby , 63 61 60 59 ไบต์

->s,w{w.any?{|i|s.sub! /\b(#{i}\b.*) #{i}\b/i,'\1'}?redo:s}

ลองออนไลน์!

เวอร์ชันที่สั้นกว่าที่คำนึงถึงตัวพิมพ์เล็กและใหญ่ ~ ทุก ๆ 10 15ครั้งเนื่องจากการสุ่ม (37 ไบต์)

->s,w{s.uniq{|i|w.member?(i)?i:rand}}

0

Python 2 , 140 ไบต์

from re import*
p='\s?%s'
S,A=input()
for a in A:S=sub(p%a,lambda s:s.end()==search(p%a,S,flags=I).end()and s.group()or'',S,flags=I)
print S

ลองออนไลน์!

คำอธิบาย:

re.sub(..)สามารถใช้เป็นอาร์กิวเมนต์ฟังก์ชันแทนสตริงแทนที่ได้ ดังนั้นที่นี่เรามีแลมบ์ดาแฟนซี ฟังก์ชั่นถูกเรียกใช้สำหรับการเกิดขึ้นของแต่ละรูปแบบและวัตถุหนึ่งถูกส่งผ่านไปยังฟังก์ชั่นนี้ - matchobject วัตถุนี้มีข้อมูลเกี่ยวกับการเกิดขึ้นที่ก่อตั้งขึ้น ฉันสนใจดัชนีของเหตุการณ์นี้ที่สามารถเรียกคืนได้โดยstart()หรือend()ฟังก์ชัน ต่อมาสั้นกว่าจึงใช้

ในการยกเว้นการแทนที่การเกิดขึ้นครั้งแรกของคำฉันใช้ฟังก์ชันการค้นหา regex อื่นเพื่อให้ได้ผลลัพธ์ที่แน่นอนและเปรียบเทียบดัชนีโดยใช้แบบเดียวกัน end()

การตั้งค่าสถานะre.Iเป็นรุ่นย่อของre.IGNORECASES

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