Skip to content

Instantly share code, notes, and snippets.

@tantaroth
Last active January 24, 2019 17:24
Show Gist options
  • Save tantaroth/1e05c0f09490ed612946f97d4568eafd to your computer and use it in GitHub Desktop.
Save tantaroth/1e05c0f09490ed612946f97d4568eafd to your computer and use it in GitHub Desktop.
AND Equation
List<int> toCombine(List<int> A) {
A.add(A[0]);
A.removeRange(0, 1);
return A;
}
class ANDEquation {
int _counter = 0;
int restoreY([List<int> A = null]) {
int result;
try {
if (A.isNotEmpty) {
Map<int, int> N = A.sublist(0, (A.length - 1)).asMap();
int Nlen = N.length;
int residue = A[A.length - 1];
if (this._counter < A.length) {
int i = 0;
while (i < Nlen) {
if (result == null) {
result = N[i];
} else {
result &= N[i];
}
i++;
}
if (result != residue) {
++this._counter;
return this.restoreY(toCombine(A));
}
} else {
this._counter = 0;
if (result != residue) {
result = -1;
}
}
}
return result;
} catch (e) {}
return null;
}
}
void main() {
ANDEquation AEq = new ANDEquation();
print(AEq.restoreY([1, 3, 5]));
print(AEq.restoreY([31, 7]));
print(AEq.restoreY([31, 7, 7]));
print(AEq.restoreY([1,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,1]));
print(AEq.restoreY([191411,256951,191411,191411,191411,256951,195507,191411,192435,191411,191411,195511,191419,191411,256947,191415,191475,195579,191415,191411,191483,191411,191419,191475,256947,191411,191411,191411,191419,256947, 191411,191411,191411]));
print(AEq.restoreY([1362,1066,1659,2010,1912,1720,1851,1593,1799,1805,1139,1493,1141,1163,1211]));
print(AEq.restoreY([2, 3, 7, 19]));
print(AEq.restoreY());
print(AEq.restoreY([]));
print(AEq.restoreY([1372]));
print(AEq.restoreY([-31, 7, 7]));
print(AEq.restoreY([0, 0, 0]));
print(AEq.restoreY([1, 2, 3]));
print(AEq.restoreY([7, 31, 7]));
print(AEq.restoreY([100, 31, 7]));
print(AEq.restoreY([9, 1, 21]));
print(AEq.restoreY([-9, -1, -21]));
}
@tantaroth
Copy link
Author

tantaroth commented Jan 23, 2019

TO TEST THE CODE, CLICK TO THE NEXT DartPad LINK

https://dartpad.dartlang.org/1e05c0f09490ed612946f97d4568eafd

  • Running the code executes the main() function which runs some tests.
  • Clicking 'Run' button to test the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment