Statistics

Problem Statement for "NoMo"

Problem Statement

Football announcers are always pontificating about the importance of momentum. Of course, whenever the team that doesn't have the momentum does something good, the explanation is that the momentum has shifted. We want to do a study of football games to determine how big a role momentum actually plays.

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 String game giving the sequence of scores by teams A and B and that returns the m-factor for the game minus the random m-factor for that number of scores by A and B.

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

  1. "AAAAAAA"

    Returns: 0.0

    The m-factor is 6, but so is the random m-factor for 7 scores against 0 scores.

  2. "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.

  3. "AAABBBBB"

    Returns: 5.5

  4. ""

    Returns: 0.0

  5. "ABABABABABABABABABABABABA"

    Returns: -23.04

  6. "AAAABAAAABAAAABAAAABAAAAA"

    Returns: -2.5600000000000005

  7. "BAAAAAAAAAAAAAAAAAAAAAAA"

    Returns: 1.8333333333333321

  8. "ABAAAAAAAAAAAAAAAAAAAAAA"

    Returns: -0.16666666666666785

  9. "BB"

    Returns: 0.0

  10. "AB"

    Returns: 0.0

  11. "AAAAAAAAAAAABBBBBBBBBBBBB"

    Returns: 22.96

  12. "AABABAAABBAAABAAA"

    Returns: -1.8823529411764706

  13. "ABBABABABBBABABBBABAB"

    Returns: -10.19047619047619

  14. "ABBABBABBAAAA"

    Returns: 0.9230769230769231

  15. "ABABABABABABABABABBBBBBBB"

    Returns: -10.96

  16. "BABABABAAABBBABBB"

    Returns: -3.5294117647058822

  17. "ABABABAAABBBABBB"

    Returns: -2.25

  18. "ABABAAABBBABBB"

    Returns: -0.2857142857142857

  19. "ABABAAABBBABBBBAAB"

    Returns: -0.2222222222222222

  20. "BBBBABBAABBBABBABBAA"

    Returns: 0.19999999999999996

  21. "BBBAABABBABBAAABB"

    Returns: 0.47058823529411764

  22. "BBABAAAAAABBABBA"

    Returns: 1.75

  23. "BAABABABBBBBAAABBBBAAB"

    Returns: 1.2727272727272727

  24. "AABABAABBBBABAABBABA"

    Returns: -4.0

  25. "BABBAABBABAB"

    Returns: -4.333333333333333

  26. "BAAAAAB"

    Returns: 1.7142857142857144

  27. "AAAABBBBBAB"

    Returns: 4.909090909090909

  28. "BAAABAABBAB"

    Returns: -1.0909090909090908

  29. "AABBA"

    Returns: 0.8

  30. "ABBAAAABA"

    Returns: 0.0

  31. "ABAABBBBAAAABBBBABBBB"

    Returns: 5.809523809523809

  32. "ABBABB"

    Returns: -0.6666666666666667

  33. "AAAAABAAABB"

    Returns: 2.7272727272727275

  34. "BBABBBB"

    Returns: -0.5714285714285716

  35. "BBABBBABABBBAAABAAAAAAAB"

    Returns: 3.8333333333333335

  36. "AABAABA"

    Returns: -2.2857142857142856

  37. "AABBBBABABAAAA"

    Returns: 1.7142857142857144

  38. "AABAAABAABAAAABABBBA"

    Returns: -1.8

  39. "ABBAAA"

    Returns: 1.3333333333333333

  40. "AABBBBBBAAAABAAAAAABBBAA"

    Returns: 11.333333333333334

  41. "AAAAABABBAABAABABABBABBB"

    Returns: -2.1666666666666665

  42. "ABAABABABBAA"

    Returns: -4.333333333333333

  43. "ABBAABBA"

    Returns: 0.0

  44. "BBABAABAABBABBBAABBB"

    Returns: -0.8

  45. "ABBAAB"

    Returns: 0.0

  46. "BBABAABABABBABAABBAB"

    Returns: -8.2

  47. "ABABBAABABBBAAA"

    Returns: -1.0666666666666667

  48. "AABAAABBBAAAAABBB"

    Returns: 6.470588235294118

  49. "BAABABBB"

    Returns: -0.5

  50. "BABBBABBBAAAAABAABBBAABA"

    Returns: 2.0

  51. "ABBB"

    Returns: 1.0

  52. "BAAB"

    Returns: 0.0

  53. "BBBBBAABABBBAAAAB"

    Returns: 4.470588235294118

  54. "BABBABBBABABBBABABABA"

    Returns: -10.19047619047619

  55. "ABABBBBBBB"

    Returns: 0.3999999999999999

  56. "AABBAB"

    Returns: 0.0

  57. "AABBBABABBBAA"

    Returns: 0.9230769230769231

  58. "BABBBABAB"

    Returns: -4.0

  59. "BABBABBBBBBAABAAB"

    Returns: -0.47058823529411764

  60. "ABAABAB"

    Returns: -3.142857142857143

  61. "ABBBBABBBBABAABBAB"

    Returns: -2.0

  62. "AAAABBBABABAABB"

    Returns: 0.9333333333333333

  63. "BBAABBB"

    Returns: 1.7142857142857144

  64. "BBAB"

    Returns: -1.0

  65. "AABBBAAABBBAAAAAABAA"

    Returns: 6.2

  66. "AABABAB"

    Returns: -3.142857142857143

  67. "ABABAABAAAABBAABBBAAAAB"

    Returns: -0.08695652173913043

  68. "AAAAAAAAABABAAABBA"

    Returns: 0.44444444444444464

  69. "BABA"

    Returns: -2.0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: