Last active
April 19, 2018 12:47
-
-
Save steghio/4e8b3543c7368a37f373d06da30d1f2c to your computer and use it in GitHub Desktop.
Scorecard algorithm
This file contains 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
Scorecard algorithm |
This file contains 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
package com.blogspot.groglogs.scorecard; | |
public class Scorecard { | |
public static int getScore(String[] input){ | |
int score = 0; | |
if(input == null || input.length == 0) return score; | |
//track partial scores here | |
int[] scores = new int[input.length]; | |
//track current position in partial scores array. Due to deletion we might have a smaller array than the input | |
int curr = 0; | |
boolean increaseCurr = false; | |
for(int i = 0; i < input.length; i++){ | |
if(increaseCurr)curr++; | |
switch(input[i]){ | |
//double previous value. If there is no previous, score is 0 | |
case "X": | |
if(curr > 0)scores[curr] = scores[curr-1] * 2; | |
else scores[curr] = 0; | |
increaseCurr = true; | |
break; | |
/* | |
sum two previous values | |
if there is only one previous, score is previous | |
if there are no two previous, score is 0 | |
*/ | |
case "+": | |
if(curr > 1)scores[curr] = scores[curr-1] + scores[curr-2]; | |
else if(curr > 0)scores[curr] = scores[curr-1]; | |
else scores[curr] = 0; | |
increaseCurr = true; | |
break; | |
//delete previous value and never consider it again subsequently. Also Z is not a value. | |
case "Z": | |
if(curr>0){ | |
curr--;//we go back so we will overwrite this score with the next or just count as 0 if we are at the end | |
scores[curr] = 0; | |
increaseCurr = false; | |
} | |
break; | |
//just get the simple score | |
default: | |
scores[curr] = Integer.parseInt(input[i]); | |
increaseCurr = true; | |
} | |
} | |
//sum up all partial scores | |
for(int i = 0; i <= curr; i++){ | |
score += scores[i]; | |
} | |
return score; | |
} | |
} |
This file contains 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
package com.blogspot.groglogs.test.scorecard; | |
import org.junit.Test; | |
import com.blogspot.groglogs.scorecard.Scorecard; | |
import static org.junit.Assert.assertEquals; | |
public class ScorecardJTests { | |
String [] input; | |
@Test | |
//null input, expected 0 | |
public void nullEmpty() { | |
input = new String[] {}; | |
//empty | |
assertEquals("empty = 0", 0, Scorecard.getScore(input)); | |
input = null; | |
//null | |
assertEquals("null = 0", 0, Scorecard.getScore(input)); | |
} | |
@Test | |
//simple numeric input, expected sum | |
public void simple() { | |
input = new String[] {"1", "2", "3"}; | |
//1 +2 +3 | |
assertEquals("simple 1,2,3 = 6", 6, Scorecard.getScore(input)); | |
input = new String[] {"1"}; | |
//1 | |
assertEquals("simple 1 = 1", 1, Scorecard.getScore(input)); | |
} | |
@Test | |
//simple doubling, expecting doubling of the first element N times | |
public void simpleX() { | |
input = new String[] {"2", "X"}; | |
//2 +(2*2) | |
assertEquals("simpleX 2,X = 6", 6, Scorecard.getScore(input)); | |
//2 +(2*2) +((2*2)*2) | |
input = new String[] {"2", "X", "X"}; | |
assertEquals("simpleX 2,X,X = 14", 14, Scorecard.getScore(input)); | |
} | |
@Test | |
//simple summation, expected summation of previous elements N times | |
public void simpleSum() { | |
input = new String[] {"1", "1", "+"}; | |
//1 +1 +(1+1) | |
assertEquals("simpleSum 1,1,+ = 4", 4, Scorecard.getScore(input)); | |
input = new String[] {"1", "1", "+", "+"}; | |
//1 +1 +(1+1) +(1+(1+1)) | |
assertEquals("simpleSum 1,1,+,+ = 7", 7, Scorecard.getScore(input)); | |
} | |
@Test | |
//simple deletion, expected summation ignoring simple numeric elements | |
public void simpleDel() { | |
input = new String[] {"1", "Z"}; | |
//1DEL | |
assertEquals("simpleDel 1,Z = 0", 0, Scorecard.getScore(input)); | |
input = new String[] {"1", "Z", "1", "1", "Z"}; | |
//1DEL +1 +1DEL | |
assertEquals("simpleSum 1,Z,1,1,Z = 1", 1, Scorecard.getScore(input)); | |
} | |
@Test | |
//only special characters, expected 0 no matter in which combination they appear | |
public void simpleSpecial() { | |
input = new String[] {"Z", "Z"}; | |
//(DEL)DEL | |
assertEquals("simpleSpecial Z,Z = 0", 0, Scorecard.getScore(input)); | |
input = new String[] {"X", "X"}; | |
//0*2 +((0*2)*2) | |
assertEquals("simpleSpecial X,X = 0", 0, Scorecard.getScore(input)); | |
input = new String[] {"+", "+"}; | |
//0 +(0+0) | |
assertEquals("simpleSpecial +,+ = 0", 0, Scorecard.getScore(input)); | |
input = new String[] {"Z", "X", "+"}; | |
//DEL +0*2 +(0+(0*2)) | |
assertEquals("simpleSpecial Z,X,+ = 0", 0, Scorecard.getScore(input)); | |
input = new String[] {"X", "Z", "+"}; | |
//(0*2)DEL +0 | |
assertEquals("simpleSpecial X,Z,+ = 0", 0, Scorecard.getScore(input)); | |
input = new String[] {"X", "+", "Z"}; | |
//0*2 +((0*2)+0)DEL | |
assertEquals("simpleSpecial X,+,Z = 0", 0, Scorecard.getScore(input)); | |
} | |
@Test | |
//actual test cases, check expected final summation in comments below | |
public void complex() { | |
input = new String[] {"1", "Z", "2", "3", "X", "1", "+"}; | |
//1DEL +2 +3 +(3*2) +1 +((3*2)+1) | |
assertEquals("complex 1,Z,2,3,X,1,+ = 19", 19, Scorecard.getScore(input)); | |
input = new String[] {"1", "Z", "2", "3", "X", "1", "+", "Z", "Z"}; | |
//1DEL +2 +3 +(3*2) +1 +(((3*2)+1)DEL)DEL | |
assertEquals("complex 1,Z,2,3,X,1,+,Z,Z = 11", 11, Scorecard.getScore(input)); | |
input = new String[] {"Z", "2", "X", "+", "X", "1", "2", "+", "+"}; | |
//DEL +2 +2*2 +(2+(2*2)) +(2+(2*2))*2 +1 +2 +(1+2) +(2+(1+2)) | |
assertEquals("complex Z,2,X,+,X,1,2,+,+ = 35", 35, Scorecard.getScore(input)); | |
input = new String[] {"Z", "2", "X", "+", "X", "Z", "1", "2", "+", "Z", "+"}; | |
//DEL +2 +2*2 +(2+(2*2)) +((2+(2*2))*2)DEL +1 +2 +(1+2)DEL +(1+2) | |
assertEquals("complex Z,2,X,+,X,Z,1,2,+,Z,+ = 18", 18, Scorecard.getScore(input)); | |
input = new String[] {"1", "2", "3", "Z", "Z", "Z"}; | |
//1DEL +2DEL +3DEL | |
assertEquals("complex 1,2,3,Z,Z,Z = 0", 0, Scorecard.getScore(input)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full description at: http://groglogs.blogspot.ch/2017/03/java-scorecard-algorithm.html