Skip to content

Instantly share code, notes, and snippets.

@alvynfash
Last active October 17, 2019 10:58
Show Gist options
  • Save alvynfash/0ebdb9d27a8fc3e80f0dfb28528543fe to your computer and use it in GitHub Desktop.
Save alvynfash/0ebdb9d27a8fc3e80f0dfb28528543fe to your computer and use it in GitHub Desktop.
P'tit Library for KDS Code Challenge (October)
// Copyright 2019 Alvyn Fasuyi. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.import 'dart:math';
import 'dart:math';
main() async {
final startFileReadTime = DateTime.now();
// final input = [-2, -1, 0];
// final input = [1, 2, 3, -2, 5];
// final input = [-1, -2, -3, -4];
// final input = [3, 5, -8, 2, 1];
// final input = [3, 4, -3, 1, -8, 2, 1];
const min = -10000, max = 10000;
final input =
await List.generate(1000000, (i) => min + Random().nextInt(max - min));
final endFileReadTime = DateTime.now().difference(startFileReadTime);
print(
'Reading the ${input.length}x values input took @: ${endFileReadTime.inMilliseconds} milliSeconds',
);
await SentimentCalculator(
sentiments: input,
minValue: min,
).getBestSentiment();
}
// Copyright 2019 Alvyn Fasuyi. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
class SentimentCalculator {
List<int> _sentiments;
int _minValue;
SentimentCalculator._internal(this._sentiments, this._minValue);
factory SentimentCalculator({List<int> sentiments, int minValue}) =>
SentimentCalculator._internal(
sentiments,
minValue,
);
Future getBestSentiment() async {
final startTime = DateTime.now();
final result = await _getSentiment(_sentiments);
final elapsedTime = DateTime.now().difference(startTime).inMilliseconds;
print('\nActual Calculation took @: $elapsedTime milliSeconds');
print('Output: $result');
}
/* Future<double> _getBestSentiment(List<double> sentiments) async {
if (sentiments == null) return null;
if (sentiments.isEmpty) return Future.value(0);
if (sentiments.length == 1) return Future.value(sentiments[0]);
return await _getSentiment(sentiments);
} */
Future _getSentiment(List<int> sentiments) {
final size = sentiments.length;
var bestMax = _minValue;
var currMax = 0;
for (int i = 0; i < size; i++) {
currMax = currMax + sentiments[i.toInt()];
if (bestMax < currMax) bestMax = currMax;
if (currMax < 0) currMax = 0;
}
return Future.value(bestMax);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment