Created
November 28, 2018 20:05
-
-
Save NevRA/7557adefc215b06fe6a39dd9644d3a0a 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
// Наименьшее и наибольшее | |
// В этом маленьком задании вам предоставляется строка разделенных пробелами чисел и приходится возвращать самое большой и самое маленькое число в виде строки. | |
// Пример | |
// highAndLow("1 2 3 4 5"); // return "5 1" | |
// highAndLow("1 2 -3 4 5"); // return "5 -3" | |
// highAndLow("1 9 3 4 -5"); // return "9 -5" | |
import 'dart:math'; | |
String highAndLow(String input) { | |
int minValue = 42; | |
int maxValue = 43; | |
return '$maxValue $minValue'; | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
void main() { | |
test('should work with single value', () { | |
expect(highAndLow('0'), equals('0 0')); | |
}); | |
test('should work with two equal values', () { | |
expect(highAndLow('42 42'), equals('42 42')); | |
}); | |
test('should work with different values', () { | |
expect(highAndLow('1 2 3 4 5'), equals('5 1')); | |
}); | |
test('should work with negative values', () { | |
expect(highAndLow('1 2 -3 4 5'), equals('5 -3')); | |
expect(highAndLow('1 9 3 4 -5'), equals('9 -5')); | |
}); | |
test('should work when all values negative', () { | |
expect(highAndLow('-1 -2 -3 -4 -5'), equals('-1 -5')); | |
}); | |
_highAndLow(''); | |
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); | |
} | |
String _highAndLow(String input) { | |
if (input == null) return null; | |
final split = input.split(' ').where((s) => !s.isEmpty); | |
final array = split.map(int.parse); | |
int minVal, maxVal; | |
array.forEach((element) { | |
minVal = minVal == null ? element : min(minVal, element); | |
maxVal = maxVal == null ? element : max(maxVal, element); | |
}); | |
return '$maxVal $minVal'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment