Created with <3 with dartpad.dev.
Last active
October 28, 2022 20:03
-
-
Save EnduringBeta/c85169d3058f229a27edc250a063fecc to your computer and use it in GitHub Desktop.
dashing-hyacinth-2585
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
import 'dart:math'; | |
/// Block Party Technical Exercise using Dart | |
/// By Ross Llewallyn | |
/// Completed in about 45 minutes | |
/// | |
/// Run (assuming you have Dart on your machine) using: `dart maxPairTest.dart` | |
/// Run on DartPad here: https://dartpad.dev/?id=c85169d3058f229a27edc250a063fecc | |
/// GitHub Gist here: https://gist.github.com/EnduringBeta/c85169d3058f229a27edc250a063fecc | |
/// Dart doesn't current have a Tuple type, so custom class used. | |
class Pair { | |
Pair(this.first, this.second); | |
final int first; | |
final int second; | |
@override | |
String toString() { | |
return "($first, $second)"; | |
} | |
} | |
/// Thoughts: | |
/// * it seems like the second index isn't actually needed since it will always | |
/// be the first + 1. | |
/// * can't just look for largest value; see examples | |
/// * don't store list the size of input | |
void main() { | |
/// Output the indices of the two consecutive elements that have the highest | |
/// max sum in an input array of integers (e.g., maxPair([0, 5, 5, 2]) will | |
/// return (1, 2)). | |
/// | |
/// In the case of a tie, the method should return the leftmost pair | |
/// (e.g., maxPair([0, 4, 3, 1, 2, 3, 4, 0]) will return (1, 2)). | |
Pair? maxPair(List<int> list) { | |
// No directions given on how to handle small lists, so returning null, | |
// which seems better than a false (0, 0) or something | |
if (list.length < 2) { | |
return null; | |
} | |
int highestPair = 0; | |
int highestPairIndex = 0; | |
list.asMap().forEach((int index, int value) { | |
// Skip last item | |
if (index != list.length - 1) { | |
// We can trust index + 1 exists, so get sum | |
final int sum = list[index + 1] + value; | |
// If found new highest value, keep it and the index where found | |
if (sum > highestPair) { | |
highestPair = sum; | |
highestPairIndex = index; | |
} | |
} | |
}); | |
// Return highest value's index and the next index | |
return Pair(highestPairIndex, highestPairIndex + 1); | |
} | |
// Basic example | |
print(maxPair([1, 2, 3])); | |
// Instruction examples | |
print(maxPair([0, 5, 5, 2])); | |
print(maxPair([0, 4, 3, 1, 2, 3, 4, 0])); | |
// Bad examples | |
print(maxPair([])); | |
print(maxPair([99])); | |
// Example where highest # isn't part of answer | |
print(maxPair([9, 1, 8, 3, 5])); | |
// Repetitive, long list | |
print(maxPair([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2])); | |
// Super long list | |
print(maxPair(List.generate(1000, (_) => Random().nextInt(100)))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment