Refined Partitions
Pyth, 19 bytes
&gF_m{.u+NYdYQqFsMQ
Try it online: Demonstration or Test harness
I'm using Pyth's tuple/list format as input. Simply replace the spaces of the test cases with commas.
Explanation:
implicit: Q is the evaluated input
m Q map each input list d to:
.u dY reduce with intermediate states over d, initial value = []
+NY update initial value N with sum of N and Y (current element of d)
{ generate a set
_ invert
gF check, if the first element is >= (superset) than the second
& and
sMQ check, if the joined lists of the input
qF are equal
Since the pseudo-code is still a little bit confusing, I'll demonstrate the algorithm using an example input.
Input: [[1,0,9],[1,3,8]],[[1],[0,9],[1,3],[8]]
The .u+NYdY
part computes all continuous sublists, that contain the first element.
[[1,0,9],[1,3,8]] => [[], [1,0,9], [1,0,9,1,3,8]]
[[1],[0,9],[1,3],[8]] => [[], [1], [1,0,9], [1,0,9,1,3], [1,0,9,1,3,8]]
B
is a refinement of the A
, iff each continuous sublist of A
is also a continuous sublist of B
(there's only one exception).
So I simply check, if the set of continuous sublists of A
is a subset of the set of continuous sublists of B
(gF_m.u+NYdYQ
).
The only exception is, if the first input list contains less elements than the second input list. For instance <Fm.u+YdYQ
would return True
for the input [[1]],[[1],[2]]
.
Therefore I also check if the joined lists are also equal &...qFsMQ
.
JavaScript (ES6), 67 70
Edit 3 bytes saved thx @apsillers
Run the snippet below in Firefox to test
f=(a,b)=>a+''==b // same values in the lists ?
&![...a.join(' ')].some((c,p)=>c<','&b.join(c)[p]>c) // splits in a are present in b?
// TEST
out=x=>O.innerHTML += x+'\n';
OK=[
[[],[]],
[[[0]],[[0]]],
[[[1,0,9,1,3,8]],[[1,0,9],[1,3,8]]],
[[[1,0,9,1,3,8]],[[1,0,9,1,3],[8]]],
[[[1,0,9,1,3,8]],[[1],[0],[9],[1],[3],[8]]],
[[[1,0,9],[1,3,8]],[[1,0,9],[1,3,8]]],
[[[1,0,9],[1,3,8]],[[1],[0,9],[1,3],[8]]],
[[[9,8,8,5,8,2,7],[5],[1,4],[2,0,0,6,0,8,4,2,6,4,2,3,7,8,7,3,9,5,7,9,8,2,9,5],[3,9,8],[7,1,4,9,7,4,5,9],[3,3,3],[9,0,7,8],[3,9,4,7,2,7,8,0,3,0],[8,2,2,7,3,9,3,2],[2,9,0,8,5,4,1,8,5,5,6,2,0,9,2,7,7,9,2,7],[3,6],[1,2,7,7,4,4,2,9]],[[9,8],[8],[5,8,2],[7],[5],[1,4],[2],[0,0,6],[0],[8,4,2],[6,4],[2],[3],[7,8],[7,3],[9],[5,7,9],[8,2],[9,5],[3],[9,8],[7,1,4],[9,7],[4,5,9],[3,3],[3],[9,0],[7,8],[3],[9],[4],[7,2],[7,8],[0],[3,0],[8,2],[2],[7,3],[9,3],[2],[2],[9],[0],[8,5,4],[1,8],[5,5],[6],[2,0],[9],[2],[7,7,9],[2,7],[3,6],[1,2],[7,7],[4,4,2],[9]]]
];
KO=[
[[[0]],[]],
[[[0]],[[1]]],
[[[1,0,9]],[[1,0,9],[1,3,8]]],
[[[1,0,9],[1,3,8]],[[1,0,9,1,3,8]]],
[[[1,0,9],[1,3,8]],[[1,0,9]]],
[[[1,0,9],[1,3,8]],[[1,0],[9]]],
[[[1,0,9],[1,3,8]],[[1,0],[9,1],[3,8]]],
[[[1],[0,9],[1,3],[8]],[[1,0,9],[1,3,8]]],
[[[9,8,8,5,8,2,7],[5],[1,4],[2,0,0,6,0,8,4,2,6,4,2,3,7,8,7,3,9,5,7,9,8,2,9,5],[3,9,8],[7,1,4,9,7,4,5,9],[3,3,3],[9,0,7,8],[3,9,4,7,2,7,8,0,3,0],[8,2,2,7,3,9,3,2],[2,9,0,8,5,4,1,8,5,5,6,2,0,9,2,7,7,9,2,7],[3,6],[1,2,7,7,4,4,2,9]],[[9,8],[8],[5,8,2],[7],[5,1],[4],[2],[0,0,6],[0],[8,4,2],[6,4],[2],[3],[7,8],[7,3],[9],[5,7,9],[8,2],[9,5],[3],[9,8],[7,1,4],[9,7],[4,5,9],[3,3],[3],[9,0],[7,8],[3],[9],[4],[7,2],[7,8],[0],[3,0],[8,2],[2],[7,3],[9,3],[2],[2],[9],[0],[8,5,4],[1,8],[5,5],[6],[2,0],[9],[2],[7,7,9],[2,7],[3,6],[1,2],[7,7],[4,4,2],[9]]]
];
dump=l=>l.map(x=>'['+x+']').join(',');
out('YES');
OK.forEach(l=>out(f(l[0],l[1])+' a['+dump(l[0])+'] b['+dump(l[1])+']'));
out('NO');
KO.forEach(l=>out(f(l[0],l[1])+' a['+dump(l[0])+'] b['+dump(l[1])+']'));
<pre id=O></pre>
C, 69 75
A function with 2 string parameters, returning 0 or 1.
Parameter format: sublist separated with spaces (' '
), list elements separated with commas.
Example: "1,0,9 1,3,8"
"1,0 9,1,3,8"
f(char*a,char*b){for(;*a-44?*a&&*b==*a:*b<48;a++)b++;return!(*b|*a);}
Less golfed
int f(char *a, char *b)
{
// expected in a,b: digit,separator,digit... with separator being ' ' or ','
for(; *a; a++,b++)
// ' ' or digit in a must be the same in b
// comma in a must be comma or space in b
if (*a != ',' ? *b != *a : *b > *a) return 0;
return !*b; // must have a null in *b too
}
Test Ideone (outdated)