Skip to content

Instantly share code, notes, and snippets.

@ohir
Last active April 16, 2018 15:52
Show Gist options
  • Save ohir/a6403fe240d72ddb22987c03dad3136f to your computer and use it in GitHub Desktop.
Save ohir/a6403fe240d72ddb22987c03dad3136f to your computer and use it in GitHub Desktop.
mini-max-sum in Dart
String minMax(String s) {
int sumA, min4, max4;
List<int> l;
try {
l = s.split(' ').map((String s) => int.parse(s)).toList();
if (l.length != 5 || l.any((e) => e < 1 || e > 1000000000))
throw 'Five integers in 1..10^9 range expected!';
} catch (e) {
throw 'Input error: $e';
}
sumA = l.fold(0, (s, e) => (s + e));
max4 = l.fold(0, (s, e) => sumA - e > s ? sumA - e : s);
min4 = l.fold(sumA, (s, e) => sumA - e < s ? sumA - e : s);
return '$min4 $max4';
}
// https://www.hackerrank.com/challenges/mini-max-sum/problem
// do test it. Note Args/args for arguments passing.
void main(List<String> Args) { // unused on DartPad => make args below
List<String> args = ['1', '2', '3', '4', '5',]; // for DartPad sharing
if (args.isEmpty) {
print('Gimme FIVE ints, please!');
return;
}
String s = args.join(' ');
try {
print(minMax(s) + ' from $s.');
} catch (e) {
print('$e [input: $s]');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment