विशिष्ट हटाने योग्य बाद में


25

परिचय

पूर्णांक और उसके बाद के अनुक्रम में से एक पर विचार करें, A = [4 2 2 4 4 6 5] और B = [2 2 5] कहें । हम क्रम में B के तत्वों को A से निकालना चाहते हैं , और ऐसा करने के कई तरीके हैं:

A = 4 2 2 4 4 6 5
B =   2   4     5
 -> 4   2   4 6

A = 4 2 2 4 4 6 5
B =     2 4     5
 -> 4 2     4 6

A = 4 2 2 4 4 6 5
B =   2     4   5
 -> 4   2 4   6

A = 4 2 2 4 4 6 5
B =     2   4   5
 -> 4 2   4   6

सभी मामलों में, शेष अनुक्रम समान है, [४ २ ४ ६] । यदि ऐसा होता है, हम कहते हैं कि बी है से विशिष्ट रूप से हटाने योग्य एक

काम

आपके इनपुट नॉनजेटिव पूर्णांक, और बी के दो अनुक्रम हैं , जहां बी को ए के बाद होने की गारंटी है । इनपुट समान हो सकते हैं, और वे खाली हो सकते हैं। आप उन्हें किसी भी क्रम में ले सकते हैं जो आप चाहते हैं, किसी भी उचित प्रारूप में।

यदि B , A से विशिष्ट रूप से हटाने योग्य है और नहीं तो मिथ्या मान के लिए आपका आउटपुट एक सत्य मूल्य होगा।

नियम और स्कोरिंग

आप एक पूर्ण कार्यक्रम या एक फ़ंक्शन लिख सकते हैं। सबसे कम बाइट गिनती जीतती है।

परीक्षण के मामलों

[] [] -> True
[0,3] [] -> True
[1,0,1] [1] -> False
[0,2] [0,2] -> True
[2,2,1,1,2,2,2] [2,1] -> True
[4,2,2,4,4,6,5] [4,5] -> False
[10,5,10,10,5,10] [10,5,10] -> False
[4,2,2,4,4,6,5] [2,4,5] -> True
[1,1,1,0,0,0,1,1,1,0] [1,0,1,1] -> True
[0,1,0,0,0,0,1,1,0,1] [1,0,1,1] -> False
[0,4,0,0,4,1,4,2,2] [0,0,0,1,4] -> True
[0,2,2,25,0,2,2,26,0,0,2] [2,0,0,0,2] -> True
[1,1,1,3,2,1,3,2,2,3,3,2] [1,1,2,3,2] -> False
[0,3,2,0,1,3,2,0,0,0,3,2] [0,1,2,0,3] -> False
[5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5] [2,7,5,7,7,2] -> False
[5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1] [4,0,1,1,2,1] -> False
[0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4] [0,1,0,0,2,0,1,4] -> True

जवाबों:


6

हास्केल, 90 84 बाइट्स

a%[]=[a]
(t:z)%x@(s:y)=[a|a<-z%y,t==s]++[t:s|s<-z%x]
_%_=[]
((all=<<(==).head).).(%)

सभी संभव घटाए गए अनुक्रमों की सूची का निर्माण nondeterministically करें और जांचें कि क्या वे सभी समान हैं।

उपयोग:

*Main> mapM_(\(a,b)->let r=(((all=<<(==).head).).(%)) a b in putStrLn$concat[show a," ",show b," -> ",show r]) [([],[]), ([0,3],[]), ([1,0,1],[1]), ([0,2],[0,2]), ([2,2,1,1,2,2,2],[2,1]), ([4,2,2,4,4,6,5],[4,5]), ([10,5,10,10,5,10],[10,5,10]), ([4,2,2,4,4,6,5],[2,4,5]), ([1,1,1,0,0,0,1,1,1,0],[1,0,1,1]), ([0,1,0,0,0,0,1,1,0,1],[1,0,1,1]), ([0,4,0,0,4,1,4,2,2],[0,0,0,1,4]), ([0,2,2,25,0,2,2,26,0,0,2],[2,0,0,0,2]), ([1,1,1,3,2,1,3,2,2,3,3,2],[1,1,2,3,2]), ([0,3,2,0,1,3,2,0,0,0,3,2],[0,1,2,0,3]), ([5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5],[2,7,5,7,7,2]), ([5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1],[4,0,1,1,2,1]), ([0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4],[0,1,0,0,2,0,1,4])]
[] [] -> True
[0,3] [] -> True
[1,0,1] [1] -> False
[0,2] [0,2] -> True
[2,2,1,1,2,2,2] [2,1] -> True
[4,2,2,4,4,6,5] [4,5] -> False
[10,5,10,10,5,10] [10,5,10] -> False
[4,2,2,4,4,6,5] [2,4,5] -> True
[1,1,1,0,0,0,1,1,1,0] [1,0,1,1] -> True
[0,1,0,0,0,0,1,1,0,1] [1,0,1,1] -> False
[0,4,0,0,4,1,4,2,2] [0,0,0,1,4] -> True
[0,2,2,25,0,2,2,26,0,0,2] [2,0,0,0,2] -> True
[1,1,1,3,2,1,3,2,2,3,3,2] [1,1,2,3,2] -> False
[0,3,2,0,1,3,2,0,0,0,3,2] [0,1,2,0,3] -> False
[5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5] [2,7,5,7,7,2] -> False
[5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1] [4,0,1,1,2,1] -> False
[0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4] [0,1,0,0,2,0,1,4] -> True

6 बाइट बचाने के लिए @Zbb को धन्यवाद!


आप चीजों को पुनर्व्यवस्थित कर सकते हैं और x%_=xदूसरे मामले के लिए कर सकते हैं %। इसके अलावा, मुझे लगता है कि मुख्य कार्य बिंदु रूप में छोटा होगा।
जर्बद

@Zgarb x%_=xकाम नहीं करेगा क्योंकि प्रकार मेल नहीं खाएंगे लेकिन _%_=[]बाइट बचाता है।
आंग।

4

जावास्क्रिप्ट (ईएस 6), 141 152 156 159

पुनरावर्ती कार्य - काफी लंबा

f=(a,b,i=0,j=0,r=a,S=new Set)=>(1/b[j]?1/a[i]&&f(a,b,i+1,j,r,S,a[i]-b[j]||f(a,b,i+1,j+1,r=[...r],r[i]=S)):S.add(0+r.filter(x=>1/x)),S.size<2)

कम गोल्फ वाला

f=(a, b, 
   i = 0, // current position to match in a
   j = 0, // current position to match in b
   r = a, // current result so far, A with elements of B removed - start == A
   S = new Set // set of possible "A removed B"
) => (
    1 / b[j] // check if j is still inside B
    ? 1 / a[i] // if i is inside A
      && (
        // in any case try to find current element of B in the remaining part of A
        f(a, b, i+1, j, r, S),
        a[i] == b[j] // if match was found between A and B
        && 
          // mark current element in a copy of r and 
          // look for the next element of B in the remaining part of A
          f(a, b, i+1, j+1, r=[...r], r[i]=S),
      )
    // else - j is not inside B, we have a solution in r
    // use filter to get a copy without the marked elements
    //  (note: 1/any_number == number_not_0, 1/Object == NaN)
    // then convert to string, to use as a key in S
    : S.add(0+a.filter(x=>1/x)),
    S.size<2 // return true if S has only 1 element
)  

परीक्षा

f=(a,b,i=0,j=0,r=a,S=new Set)=>(1/b[j]?1/a[i]&&f(a,b,i+1,j,r,S,a[i]-b[j]||f(a,b,i+1,j+1,r=[...r],r[i]=S)):S.add(0+r.filter(x=>1/x)),S.size<2)


out=(...x)=>O.textContent+=x.join` `+'\n'
;`[] [] -> True
[0,3] [] -> True
[1,0,1] [1] -> False
[0,2] [0,2] -> True
[2,2,1,1,2,2,2] [2,1] -> True
[4,2,2,4,4,6,5] [4,5] -> False
[10,5,10,10,5,10] [10,5,10] -> False
[4,2,2,4,4,6,5] [2,4,5] -> True
[1,1,1,0,0,0,1,1,1,0] [1,0,1,1] -> True
[0,1,0,0,0,0,1,1,0,1] [1,0,1,1] -> False
[0,4,0,0,4,1,4,2,2] [0,0,0,1,4] -> True
[0,2,2,25,0,2,2,26,0,0,2] [2,0,0,0,2] -> True
[1,1,1,3,2,1,3,2,2,3,3,2] [1,1,2,3,2] -> False
[0,3,2,0,1,3,2,0,0,0,3,2] [0,1,2,0,3] -> False
[5,7,2,7,7,1,7,7,5,2,7,7,5,2,2,7,5] [2,7,5,7,7,2] -> False
[5,4,0,5,4,5,4,1,0,4,2,1,1,2,4,4,0,2,2,1] [4,0,1,1,2,1] -> False
[0,1,4,0,1,4,0,1,5,1,4,4,2,0,0,1,1,1,2,4] [0,1,0,0,2,0,1,4] -> True`
.split('\n').forEach(t=>{
  var [a,b,_,k]=t.match(/\S+/g)
  var r=f(eval(a),eval(b))
  out(r==(k[0]=='T')?'OK':'KO',a,b,r,k)
})
<pre id=O></pre>



3

जावास्क्रिप्ट (ईएस 6), 116 114 113 बाइट्स

लौटाता है falseया true

(a,b,p)=>((F=(a,i,m)=>1/b[i]?a.map((n,j)=>m>j|n-b[i]||F(a.filter((_,k)=>j-k),i+1,j)):p?r|=p!=a:p=a+'')(a,r=0),!r)

प्रारूपित और टिप्पणी की गई

(                                     // given:
  a, b,                               // - a, b = input arrays
  p                                   // - p = reference pattern, initially undefined
) => (                                //
  (F = (                              // F is our recursive search function, with:
    a,                                // - a = current working copy of the main array
    i,                                // - i = index in 'b'
    m                                 // - m = minimum index of matching values in 'a'
  ) =>                                //
    1 / b[i] ?                        // if we haven't reached the end of 'b':
      a.map((n, j) =>                 //   for each element 'n' at index 'j' in 'a':
        m > j | n - b[i] ||           //     if 'n' is a valid matching value,
        F(                            //     do a recursive call to F(), using:
          a.filter((_, k) => j - k),  //     - a copy of 'a' without the current element
          i + 1,                      //     - the next index in 'b'
          j                           //     - 'j' as the new minimum index in 'a'
        )                             //
      )                               //
    :                                 // else:
      p ?                             //   if the reference pattern is already set:
        r |= p != a                   //     check if it's matching the current 'a'
      :                               //   else:
        p = a + ''                    //     set the current 'a' as the reference pattern
  )(a, r = 0),                        //  initial call to F() + initialization of 'r'
  !r                                  //  yields the final result
)                                     //

परीक्षण के मामलों


वाह! मैंने A की कम प्रतिलिपि के साथ
पुनरावृत्ति


1

जावास्क्रिप्ट (फ़ायरफ़ॉक्स 30+), 159 147 बाइट्स

f=(a,b,i=f(a,b,0))=>i?i.every(x=>x+""==i[0]):b+b?a+a&&[for(n of a)if(a[i++]==b[0])for(x of f(a.slice(i),b.slice(1),0))[...a.slice(0,i-1),...x]]:[a]

यहाँ वैकल्पिक दृष्टिकोणों के एक जोड़े, दोनों गुमनाम कार्य हैं:

(a,b,f=(a,b,i=0)=>b+b?a+a&&[for(n of a)if(a[i++]==b[0])for(x of f(a.slice(i),b.slice(1)))[...a.slice(0,i-1),...x]]:[a],c=f(a,b))=>c.every(x=>x+""==c[0])
(a,b,f=(a,b,i=0)=>b+b?a+a&&[for(n of a)if(a[i++]==b[0])for(x of f(a.slice(i),b.slice(1)))[...a.slice(0,i-1),...x]]:[a])=>new Set(f(a,b).map(btoa)).size<2

टेस्ट स्निपेट


मुझे स्निपेट भी पसंद है
edc65

1

गणितज्ञ, 128 बाइट्स

h=Length;n=ToExpression;g=ToString;y=Array;h@Union@ReplaceList[#2,n@Riffle[y["a"<>g@#<>"___"&,t=h@#+1],#]->n@y["a"<>g@#&,t]]==1&

दो सूची तर्कों को लेने का कार्य करें, जहां पहला क्रमिक है और दूसरा पूर्ण अनुक्रम है; आउटपुट Trueया False

मुख्य भाग निम्नलिखित अनुक्रम है, पठनीयता के लिए ungolfed:

ReplaceList[#2, ToExpression @
  Riffle[
    Array["a" <> ToString@# <> "___" &, Length@# + 1]
    , #
  ] -> ToExpression @ 
    Array["a" <> ToString@#& , Length@# + 1 ]
]

यहाँ #, परिणाम को-उदाहरण के लिए प्रतिनिधित्व करता {2,4,5}। पहला Arrayकमांड स्ट्रिंग्स की एक सूची बनाता है {"a1___","a2___","a3___","a4___"}, जैसे कि फिर Riffleएक साथ #एक अजीब सूची प्राप्त करने के लिए {"a1___",2,"a2___",4,"a3___",5,"a4___"}; तब यह सूची एक वास्तविक गणितज्ञ अभिव्यक्ति में डाली जाती है। उदाहरण के लिए {2,4,5}, इस कोर कोड का आंशिक मूल्यांकन है

ReplaceList[#2, {a1___,2,a2___,4,a3___,5,a4___} -> {a1,a2,a3,a4}]

जो वास्तव में परिणाम को दूर करने के लिए हर संभव तरीके की एक सूची देता {2,4,5}से #2और अकेले सूची के बाकी छोड़ दें।

इस सूची के उत्पन्न होने के बाद, हम केवल डुप्लिकेट को निकालते हैं Unionऔर परीक्षण करते हैं कि परिणामी आउटपुट की लंबाई 1 है या नहीं।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.