Created
November 28, 2018 22:24
-
-
Save NevRA/fe0f8807ee729f2953204cf6fc2f13e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Уникальный порядок | |
// Реализуй функцию, которая принимает в качестве аргумента массив элементов и возвращает массив элементов без каких-либо элементов с одинаковым значением рядом друг с другом и сохраняя исходный порядок | |
// uniqueInOrder([1,2,2,3,3,2,2,1]) ==> [1,2,3,2,1] | |
// Сделай так, чтобы прошли тесты | |
Iterable<int> uniqueInOrder(Iterable<int> array) { | |
return [1, 2]; | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
void main() { | |
test('should pass test cases', () { | |
expect(uniqueInOrder([1, 2, 2, 3, 3]), equals([1, 2, 3])); | |
expect(uniqueInOrder([7, 7, 3, 1, 2, 7]), equals([7, 3, 1, 2, 7])); | |
expect(uniqueInOrder([-1, 0, 0, -1]), equals([-1, 0, -1])); | |
expect(uniqueInOrder([5]), equals([5])); | |
expect(uniqueInOrder([5, 5]), equals([5])); | |
expect(uniqueInOrder([1, 2, 3, 3, 4, 3, 3, 4, 0]), equals([1, 2, 3, 4, 3, 4, 0])); | |
expect(uniqueInOrder([1, 2, 3, 3, 1, 3, 6, 5, 4, 2, 3]), equals([1, 2, 3, 1, 3, 6, 5, 4, 2, 3])); | |
}); | |
_uniqueInOrder([]); | |
if(!hasErrors ){ | |
print('💪Tests are passed! \n\n'); | |
} else { | |
print('💩 Tests aren\'t passed! \n\n'); | |
} | |
print(messages.join('\n')); | |
} | |
typedef bool Checker(dynamic input); | |
Checker equals(dynamic input) { | |
return (dynamic internalInput) { | |
input.toString() == internalInput.toString() | |
? true | |
: throw AssertionError('value: $input is not equal: $internalInput'); | |
}; | |
} | |
void test(String name, Function input) { | |
try { | |
input(); | |
messages.add('✓ $name'); | |
} catch (e) { | |
hasErrors = true; | |
if (e is AssertionError) { | |
messages.add('✗ $test failed \n name: $name\n exception: ${e.message}'); | |
} | |
} | |
} | |
void expect(dynamic input, bool validator(dynamic validatorInput)) { | |
validator(input); | |
} | |
Iterable<int> _uniqueInOrder(Iterable<int> array) { | |
final result = <int>[]; | |
array.forEach((item) { | |
if(result.isEmpty || result.last != item) { | |
result.add(item); | |
} | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment