Last active
November 29, 2018 21:54
-
-
Save NevRA/952411dd933c68ba21dc3731ffd57adb 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
// Сортировка по образцу | |
// Напиши функцию, которая сортирует массив по каталогу. В каталоге есть все элементы из входного массива, но во входном массиве необязательно есть все элементы из каталога. | |
// Пример: | |
// Входной массив: [1,3,4,4,4,4,5] | |
// Каталог: [4,1,2,3,5] | |
// Результат: [4,4,4,4,1,3,5] | |
Iterable<int> exampleSort(Iterable<int> array, Iterable<int> catalog) { | |
return [1, 2, 3]; | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
void main() { | |
test('should work with empty array and catalog', () { | |
expect(exampleSort([], []), equals([])); | |
}); | |
test('should work with empty array', () { | |
expect(exampleSort([], [1, 2, 3]), equals(<int>[])); | |
}); | |
test('should work with example', () { | |
expect(exampleSort([1, 3, 4, 4, 4, 4, 5], [4, 1, 2, 3, 5]), equals([4, 4, 4, 4, 1, 3, 5])); | |
}); | |
test('should work with different array size', () { | |
expect(exampleSort([1, 2, 0, 3], [0, 1]), equals([0, 1])); | |
expect(exampleSort([1, 2, 0, 3], [3]), equals([3])); | |
}); | |
test('should work with equal array size', () { | |
expect(exampleSort([1, 2, 3, 4, 5], [2, 3, 4, 1, 5]), equals([2, 3, 4, 1, 5])); | |
}); | |
test('should work with repeated values', () { | |
expect(exampleSort([1, 2, 3, 3, 3, 4, 5], [2, 3, 4, 1, 5]), equals([2, 3, 3, 3, 4, 1, 5])); | |
expect(exampleSort([1, 2, 3, 3, 3, 5], [2, 3, 4, 1, 5]), equals([2, 3, 3, 3, 1, 5])); | |
expect(exampleSort([1, 2, 3, 3, 3, 5], [3, 4, 5, 6, 9, 11, 12, 13, 1, 7, 8, 2, 10]), equals([3, 3, 3, 5, 1, 2])); | |
expect(exampleSort([1, 1, 1, 5, 1, 7], [1, 7, 4, 5]), equals([1, 1, 1, 1, 7, 5])); | |
}); | |
_exampleSort([], []); | |
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> _exampleSort(Iterable<int> array, Iterable<int> catalog) { | |
if (array == null) return null; | |
if (catalog == null) return null; | |
final tmpMap = <int, int>{}; | |
array.forEach((value) { | |
tmpMap.containsKey(value) ? tmpMap[value] ++ : tmpMap[value] = 1; | |
}); | |
final result = <int>[]; | |
catalog.forEach((value) { | |
if (tmpMap.containsKey(value)) { | |
result.addAll(List.filled(tmpMap[value], value)); | |
} | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment