Commit b569a44
Changed files (2)
src/Q8/BadmintonScoring.java
@@ -1,5 +1,7 @@
package Q8;
+import java.util.*;
+
public class BadmintonScoring {
private int[][] scores;
private static final int PLAYER1 = 0;
@@ -10,11 +12,11 @@ public class BadmintonScoring {
}
public int getContinuousPointsPlayer1() {
- return 0;
+ return this.longestStreakFor(PLAYER1);
}
public int getContinuousPointsPlayer2() {
- return 0;
+ return this.longestStreakFor(PLAYER2);
}
public int getPlayer1Points() {
@@ -30,4 +32,31 @@ public class BadmintonScoring {
for (int[] items : scores) finalScore = items[player];
return finalScore;
}
+
+ private int longestStreakFor(int player) {
+ int streak = 0;
+ int longestStreak = 0;
+ boolean lastWinner = false;
+
+ for (int i = 0; i < scores.length; i++) {
+ int score = scores[i][player];
+ int previousScore = i == 0 ? -1 : scores[i - 1][player];
+ boolean winner = score > previousScore;
+
+ if (winner && lastWinner) {
+ streak++;
+ longestStreak = (streak > longestStreak) ? streak : longestStreak;
+ } else streak = 0;
+
+ lastWinner = winner;
+ }
+
+ return longestStreak;
+ }
+
+ private int winnerOf(int round) {
+ int player1Score = scores[round][0];
+ int player1PreviousScore = scores[round - 1][0];
+ return player1Score > player1PreviousScore ? PLAYER1 : PLAYER2;
+ }
}
src/Q8/BadmintonScoringTest.java
@@ -56,4 +56,12 @@ public class BadmintonScoringTest extends TestCase {
public void test_getPlayer2Points() {
assertEquals(21, this.subject.getPlayer2Points());
}
+
+ public void test_getContinuousPointsPlayer1() {
+ assertEquals(2, this.subject.getContinuousPointsPlayer1());
+ }
+
+ public void test_getContinuousPointsPlayer2() {
+ assertEquals(9, this.subject.getContinuousPointsPlayer2());
+ }
}