জাভাস্ক্রিপ্ট (ES6), 329
এমন কোনও ভাষার পক্ষে সহজ কাজ নয় যার কোনও সংমিশ্রণমূলক বিল্টিন নেই।
সম্ভবত চিকিত্সা আরও গল্ফযোগ্য।
দ্রষ্টব্য: সমস্ত বিভাজন কমপক্ষে 2 আকারের হয় কারণ একটি একক উপাদান সহ একটি পার্টিশন সর্বদা কম কার্যকর।
Example: [1] [2 3 4] // can take 1 or 2 or 4
Better: [1 2] [3 4] // can take 3 too
a=>{G=(v,i,u=v)=>{if(i--){for(;r[i]=--u;)if(G(u,i))return 1;}else for(w=[...r,n=l].map((x,i)=>a.slice(z,z=x-~i),z=0),y=w.join`;`;w.map(b=>[0,1].map(q=>(x=b[q*=~-b.length])&&(t[x]?([c,p]=t[x],n-=2,p?c.pop():c.shift(),q?b.pop():b.shift()):t[x]=[b,q])),c=0,t=[]),c;)if(!n)return 1};for(l=a.length,r=[],k=0;!G(l-k-1,k);k++);return y}
অংশে ব্যাখ্যা
(এটি অত্যধিক ভার্জোজ, তবে ব্যাখ্যা করা আমার কাছে শক্ত মনে হয়েছিল - অবশেষে "এগুলি সব একসাথে রেখে" এড়িয়ে যান)
একটি অ্যারের সমস্ত সম্ভাব্য বিভাজন গণনার জন্য পুনরাবৃত্ত ফাংশন
// v: array length
// i number of splits
// fill the global array r that must exists
G=(v,i,u=v)=>
{
if(i--)
{
for(;r[i]=--u;)
G(u,i)
}
else
{
// the current split position are in r, ready to use
// for instance...
parts = [...r,a.length].map(x=>a.slice(z,z=x),z=0)
console.log(r, parts)
}
};
r=[]
a=['A','B','C','D']
G(4, 2)
// output in console (firebug)
[2, 3] [["A", "B"], ["C"], ["D"]]
[1, 3] [["A"], ["B", "C"], ["D"]]
[1, 2] [["A"], ["B"], ["C", "D"]]
এখন, আমার 2 বা ততোধিক আকারের পার্টিশনগুলির প্রয়োজন, তাই আমার অবশ্যই এই ফাংশনটি সরলভাবে বিভিন্ন পরামিতিগুলি সহ ব্যবহার করতে হবে। প্যারামিটার ভি হ'ল "অ্যারের আকার - পছন্দসই পার্টিশনের সংখ্যা - 1"। তারপরে অবশ্যই পার্টিশনগুলি কিছুটা ভিন্ন উপায়ে তৈরি করতে হবে।
// Same call (4,2), same r, but the array b is of size 7
part = [...r,b.length].map((x,i)=>
b.slice(z,z=x+i+1) // add 1 more element to each partition
,z=0))
// output in console (firebug)
[2, 3] [["A", "B", "C"], ["D", "E"], ["F", "G"]]
[1, 3] [["A", "B"], ["C", "D", "E"], ["F", "G"]]
[1, 2] [["A", "B"], ["C", "D"], ["E", "F", "G"]]
সুতরাং, আমি বিভাজন, 1 বিভাজন, 2 বিভাজন এবং এর জন্য পার্টিশনের তালিকাটি গণনা করতে পারি। যখন আমি একটি কার্যকারী পার্টিশন পাই তখন আমি থামাব এবং ফলাফলটি আউটপুট করব।
পার্টিশন তালিকাটি পরীক্ষা করতে, স্ক্যান করতে, প্রতিটিটির শুরুতে এবং শেষে মানগুলি নোট করুন, যদি কোনও নামযুক্ত মান পাওয়া যায় তবে এটি মুছে ফেলুন। শেষ পর্যন্ত কিছুই অপসারণ না করা পর্যন্ত পুনরাবৃত্তি করুন: সমস্ত জোড় অপসারণ করা থাকলে এই বিভাজনটি ভাল।
t = []; // array to note the repeated values
// t[x] == [
// subarray holding value x,
// position of value x (I care zero or nonzero)
// ]
n = a.length // counter start, must reach 0
// remember part just in case, because this check will destroy it
result = part.join(';') // a string representation for return value
do
{
// in the golfed code there is a forr loop
// all this body is inside the for condition
c = 0; // init c to a falsy, if a pair is found c becomes truthy
part.forEach(b=> // b: array, current partition
[0,1].forEach(q=> ( // exec for 0 (start), 1 (end)
q *= b.length-1, // now q is the correct index
x = b[q]) // x is the value at start or end
x && ( // b could be empty, check that x is not 'undefined'
t[x] ? // is there a value in t at position x?
( // yes, remove the pair
n-=2, // pair found, decrement counter
[c, p] = t[x], // get stored array and position
p ? c.pop() : c.shift(), // remove from c at start or end
q ? b.pop() : b.shift() // remove twin value from b
)
: // no, remember the value in t
t[x] = [b, q]
)) // end [0,1].forEach
) // end part.forEach
}
while (c) // repeat until nothing can be removed
if(!n) return 1 // wow, result found (in 'result' variable)
তারপরে, অনুপস্থিত অংশটি পার্টিশন গণনা বৃদ্ধি করে জি ফাংশনটি কল করে কেবল একটি লুপ। ফলাফল পাওয়া গেলে লুপটি প্রস্থান করুন।
সব একসাথে রাখুন
F=a=>{
G=(v,i,u=v)=>{
if (i--)
{
for(; r[i]=--u; )
if (G(u,i))
return 1;
}
else
{
w = [...r,n=l].map((x,i)=>a.slice(z, z = x-~i), z = 0);
y = w.join`;`;
for(; // almost all the for body is inside the condition
w.map(b=>
[0,1].map(q=>
(x=b[q*=~-b.length])
&&(t[x]
?([c,p]=t[x],n-=2,
p?c.pop():c.shift(),
q?b.pop():b.shift())
:t[x]=[b,q])) // end [0,1].map
,c=0,t=[] // init variables for w.map
),c; // the loop condition is on c
)
if(!n)return 1 // this is the for body
}
};
for(l = a.length, r = [], k = 0; !G(l-k-1, k); k++);
return y
}
পরীক্ষা
F=a=>{G=(v,i,u=v)=>{if(i--){for(;r[i]=--u;)if(G(u,i))return 1;}else for(w=[...r,n=l].map((x,i)=>a.slice(z,z=x-~i),z=0),y=w.join`;`;w.map(b=>[0,1].map(q=>(x=b[q*=~-b.length])&&(t[x]?([c,p]=t[x],n-=2,p?c.pop():c.shift(),q?b.pop():b.shift()):t[x]=[b,q])),c=0,t=[]),c;)if(!n)return 1};for(l=a.length,r=[],k=0;!G(l-k-1,k);k++);return y}
console.log=x=>O.textContent+=x+'\n'
TestData=[[1,1],[1,2,1,2],[1,3,2,4,3,2,1,4],[1,2,3,4,3,4,1,2],[1,1,2,2,3,3],[4,3,4,2,2,1,1,3]]
TestData.forEach(t=>console.log(t+' -> '+F(t)))
function RandomTest() {
var l=I.value*2
var a=[...Array(l)].map((_,i)=>1+i/2|0)
a.map((v,i)=>a[a[i]=a[j=0|i+Math.random()*(a.length-i)],j]=v) // shuffle
Q.textContent=a+''+'\n\n'+F(a).replace(/;/g, ';\n') // better readability
}
Base test
<pre id=O></pre>
Random test. Number of pairs: <input id=I value=15><button onclick="RandomTest()">-></button>
<pre id=Q></pre>