Problem Statement
We define the "m-factor" to be the number of scores that immediately follow a score by the same team minus the number of scores that immediately follow a score by the opponent. So if "ABBAAAAAB" were the sequence of scores in a game between teams A and B, then the m-factor would be 5 - 3 = 2. (The 5 comes from one time that a score by B was immediately followed by another score by B and four times that a score by A was immediately followed by a score by A. Similarly, the 3 comes from the one BA and the two AB's that occur.)
But we have to be careful. A high m-factor will naturally occur if one team is just a lot better than the other. If only one team scores, no score will follow a score by the opponent. So to judge whether momentum played an important role, we have to compare the m-factor for a game to the random m-factor for the given number of scores by the two teams. For a game with n scores, the random m-factor is defined to be the average of the n! m-factors corresponding to the n! (not necessarily distinct) permutations of the scores. For example, the random m-factor of the game "ABA" is the average of the 6 m-factors corresponding to "ABA", "AAB", "BAA", "BAA", "AAB", "ABA".
Create a class NoMo that contains a method momentum that is given a
Definition
- Class:
- NoMo
- Method:
- momentum
- Parameters:
- String
- Returns:
- double
- Method signature:
- double momentum(String game)
- (be sure your method is public)
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- game will contain between 0 and 25 characters inclusive.
- Each character in game will be 'A' or 'B'.
Examples
"AAAAAAA"
Returns: 0.0
The m-factor is 6, but so is the random m-factor for 7 scores against 0 scores.
"AAB"
Returns: 0.6666666666666666
The m-factor for this game is 0. The six permutations with their m-factors are: AAB = 0, AAB = 0, ABA = -2, ABA = -2, BAA = 0, BAA = 0 so the random m-factor for 2 scores against 1 is -4/6. Thus we return 0 - (-4/6) = .6666666... So momentum played a positive role in this game.
"AAABBBBB"
Returns: 5.5
""
Returns: 0.0
"ABABABABABABABABABABABABA"
Returns: -23.04
"AAAABAAAABAAAABAAAABAAAAA"
Returns: -2.5600000000000005
"BAAAAAAAAAAAAAAAAAAAAAAA"
Returns: 1.8333333333333321
"ABAAAAAAAAAAAAAAAAAAAAAA"
Returns: -0.16666666666666785
"BB"
Returns: 0.0
"AB"
Returns: 0.0
"AAAAAAAAAAAABBBBBBBBBBBBB"
Returns: 22.96
"AABABAAABBAAABAAA"
Returns: -1.8823529411764706
"ABBABABABBBABABBBABAB"
Returns: -10.19047619047619
"ABBABBABBAAAA"
Returns: 0.9230769230769231
"ABABABABABABABABABBBBBBBB"
Returns: -10.96
"BABABABAAABBBABBB"
Returns: -3.5294117647058822
"ABABABAAABBBABBB"
Returns: -2.25
"ABABAAABBBABBB"
Returns: -0.2857142857142857
"ABABAAABBBABBBBAAB"
Returns: -0.2222222222222222
"BBBBABBAABBBABBABBAA"
Returns: 0.19999999999999996
"BBBAABABBABBAAABB"
Returns: 0.47058823529411764
"BBABAAAAAABBABBA"
Returns: 1.75
"BAABABABBBBBAAABBBBAAB"
Returns: 1.2727272727272727
"AABABAABBBBABAABBABA"
Returns: -4.0
"BABBAABBABAB"
Returns: -4.333333333333333
"BAAAAAB"
Returns: 1.7142857142857144
"AAAABBBBBAB"
Returns: 4.909090909090909
"BAAABAABBAB"
Returns: -1.0909090909090908
"AABBA"
Returns: 0.8
"ABBAAAABA"
Returns: 0.0
"ABAABBBBAAAABBBBABBBB"
Returns: 5.809523809523809
"ABBABB"
Returns: -0.6666666666666667
"AAAAABAAABB"
Returns: 2.7272727272727275
"BBABBBB"
Returns: -0.5714285714285716
"BBABBBABABBBAAABAAAAAAAB"
Returns: 3.8333333333333335
"AABAABA"
Returns: -2.2857142857142856
"AABBBBABABAAAA"
Returns: 1.7142857142857144
"AABAAABAABAAAABABBBA"
Returns: -1.8
"ABBAAA"
Returns: 1.3333333333333333
"AABBBBBBAAAABAAAAAABBBAA"
Returns: 11.333333333333334
"AAAAABABBAABAABABABBABBB"
Returns: -2.1666666666666665
"ABAABABABBAA"
Returns: -4.333333333333333
"ABBAABBA"
Returns: 0.0
"BBABAABAABBABBBAABBB"
Returns: -0.8
"ABBAAB"
Returns: 0.0
"BBABAABABABBABAABBAB"
Returns: -8.2
"ABABBAABABBBAAA"
Returns: -1.0666666666666667
"AABAAABBBAAAAABBB"
Returns: 6.470588235294118
"BAABABBB"
Returns: -0.5
"BABBBABBBAAAAABAABBBAABA"
Returns: 2.0
"ABBB"
Returns: 1.0
"BAAB"
Returns: 0.0
"BBBBBAABABBBAAAAB"
Returns: 4.470588235294118
"BABBABBBABABBBABABABA"
Returns: -10.19047619047619
"ABABBBBBBB"
Returns: 0.3999999999999999
"AABBAB"
Returns: 0.0
"AABBBABABBBAA"
Returns: 0.9230769230769231
"BABBBABAB"
Returns: -4.0
"BABBABBBBBBAABAAB"
Returns: -0.47058823529411764
"ABAABAB"
Returns: -3.142857142857143
"ABBBBABBBBABAABBAB"
Returns: -2.0
"AAAABBBABABAABB"
Returns: 0.9333333333333333
"BBAABBB"
Returns: 1.7142857142857144
"BBAB"
Returns: -1.0
"AABBBAAABBBAAAAAABAA"
Returns: 6.2
"AABABAB"
Returns: -3.142857142857143
"ABABAABAAAABBAABBBAAAAB"
Returns: -0.08695652173913043
"AAAAAAAAABABAAABBA"
Returns: 0.44444444444444464
"BABA"
Returns: -2.0