Fix error in plate calculator

The smallest plate was never being taken into consideration, as the loop never
reached it
This commit is contained in:
Roland Geider
2021-06-15 17:33:39 +02:00
parent 70e0620fe1
commit 039b195e7a
2 changed files with 5 additions and 4 deletions

View File

@@ -26,16 +26,16 @@ List<num> plateCalculator(num totalWeight, num barWeight, List<num> plates) {
return [];
}
// Remove the bar and divide by two to get weight on each side
totalWeight = (totalWeight - barWeight) / 2;
// Weight can't be divided with the smallest plate
if (totalWeight % plates.first > 0) {
return [];
}
// Remove the bar and divide by two to get weight on each side
totalWeight = (totalWeight - barWeight) / 2;
// Find the plates
for (int i = (platesCount - 1); i > 0; i--) {
for (int i = (platesCount - 1); i >= 0; i--) {
var plate = plates[i];
while (totalWeight >= plate) {

View File

@@ -25,6 +25,7 @@ void main() {
test('Regular weights', () async {
expect(plateCalculator(40, BAR_WEIGHT, AVAILABLE_PLATES), [10]);
expect(plateCalculator(100, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 10]);
expect(plateCalculator(102.5, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 10, 1.25]);
expect(plateCalculator(140, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 15, 15]);
expect(plateCalculator(45, BAR_WEIGHT, AVAILABLE_PLATES), [10, 2.5]);
expect(plateCalculator(85, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 2.5]);