Skip to content

Instantly share code, notes, and snippets.

@kalachevmax
Last active November 29, 2018 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kalachevmax/0e8007a5c512002c5c3def613b348260 to your computer and use it in GitHub Desktop.
Save kalachevmax/0e8007a5c512002c5c3def613b348260 to your computer and use it in GitHub Desktop.
// Дан массив целых чисел.
// Найти наибольшую возрастающую подпоследовательность.
// В случае пустого массива и массива из одного элемента - результат равен исходному массиву, в случае, если все подпоследовательности состоят из одного элемента, возвращается первая.
List<int> findMaxIncrementalSubList(List<int> list) {
return null;
}
bool hasErrors = false;
List<String> messages = [];
void main() {
test('should works for empty list', () {
expect(findMaxIncrementalSubList([]), equals([]));
});
test('should works for list of one element', () {
expect(findMaxIncrementalSubList([1]), equals([1]));
});
test('should works for list with one element incremental sub lists', () {
expect(findMaxIncrementalSubList([5, 4]), equals([5]));
});
test('should works for regular list', () {
expect(findMaxIncrementalSubList([3, 4, 2, 1, 5 ,8, 9, 4, 7]), equals([1, 5, 8, 9]));
});
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);
}
class Solver {
final List<int> _list;
Solver(this._list);
List<List<int>> _getNotEmptyIncrementalSubLists(List<int> list) {
final List<List<int>> result = [];
final List<int> temp = [];
temp.add(list[0]);
for (int i = 1; i < list.length; i++) {
if (list[i] > list[i-1]) {
temp.add(list[i]);
} else {
result.add(new List.from(temp));
temp.clear();
temp.add(list[i]);
}
}
result.add(new List.from(temp));
return result;
}
List<List<int>> _getIncrementalSubLists(List<int> list) {
if (list.isNotEmpty) {
return _getNotEmptyIncrementalSubLists(list);
}
return [[]];
}
List<int> solve() {
final List<List<int>> lists = _getIncrementalSubLists(_list);
int max = lists[0].length;
int index = 0;
for (int i = 1; i < lists.length; i++) {
if (lists[i].length > max) {
max = lists[i].length;
index = i;
}
}
return lists[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment