Statistics

Problem Statement for "AddPeriodic"

Problem Statement

You are given two Strings A and B, each containing the representation of a nonnegative periodic decimal. More precisely, A and B are each given in the form "X.Y(Z)", where:

  • X, Y, Z are sequences of digits
  • X and Z are non-empty
  • X is either "0" or it starts with a nonzero digit.

Y is called the pre-period and Z is called the period. For example, "0.(3)" and "0.33(333)" are two valid representations of the number 1/3, and "12.(0)" and "11.(9)" are two valid representations of the number twelve.

Let C = A + B. Return a String containing a representation of C in the same format. Minimize the length of the pre-period and also the length of the period in the representation of C. If the period can be either 0 or 9, use 0.

Definition

Class:
AddPeriodic
Method:
add
Parameters:
String, String
Returns:
String
Method signature:
String add(String A, String B)
(be sure your method is public)

Constraints

  • A and B will each have between 5 and 12 characters, inclusive.
  • A and B will each have the form from the problem statement.

Examples

  1. "0.33(333)"

    "0.(66)"

    Returns: "1.(0)"

    1/3 + 2/3 = 1

  2. "2.41(5)"

    "5.36(22)"

    Returns: "7.(7)"

    2.41555555... + 5.362222222... = 7.77777777...

  3. "685.4(757)"

    "45.356(43)"

    Returns: "730.832(210119)"

  4. "0.(101)"

    "0.(23)"

    Returns: "0.(333424)"

  5. "0.0(999999)"

    "1.5(00000)"

    Returns: "1.6(0)"

    The input may contain a number whose period consists of all nines, but in order to have a unique correct output the output is not allowed to contain such a period. This test case is the addition 0.1 + 1.5 = 1.6

  6. "5.860(1750)"

    "153.8(06)"

    Returns: "159.66(6235)"

  7. "88.35(859)"

    "3.(3)"

    Returns: "91.69(193)"

  8. "2.9(5)"

    "637.(851)"

    Returns: "640.8(074)"

  9. "687.5(949)"

    "493.879(92)"

    Returns: "1181.474(924287)"

  10. "74.(01)"

    "8993.59(975)"

    Returns: "9067.60(986076)"

  11. "572.(6)"

    "14.01(261)"

    Returns: "586.6(792)"

  12. "528.38(0)"

    "7.(9)"

    Returns: "536.38(0)"

  13. "1.(250)"

    "9.(3)"

    Returns: "10.(583)"

  14. "6.(1)"

    "1.(4)"

    Returns: "7.(5)"

  15. "88.(484)"

    "9.(97)"

    Returns: "98.(464282)"

  16. "5.(8)"

    "187.4(6727)"

    Returns: "193.3(5616)"

  17. "0.7(6)"

    "879.5(1)"

    Returns: "880.2(7)"

  18. "14.81(70347)"

    "18.368(8541)"

    Returns: "33.185(88895576624533122018)"

  19. "5.(16)"

    "5.4334(00)"

    Returns: "10.5950(16)"

  20. "796.08(70)"

    "76.117(9)"

    Returns: "872.205(07)"

  21. "69.8(369)"

    "9623.6(9821)"

    Returns: "9693.5(351567589191)"

  22. "7916.7(265)"

    "11.67(8127)"

    Returns: "7928.4(046543393078)"

  23. "92.66(1837)"

    "59.109(1)"

    Returns: "151.770(9482)"

  24. "3.(8449)"

    "1.(8)"

    Returns: "5.(7338)"

  25. "4.901(23638)"

    "52.(11)"

    Returns: "57.012(34749)"

  26. "11.(22)"

    "250.14(935)"

    Returns: "261.37(158)"

  27. "3.16(8)"

    "83.76(887)"

    Returns: "86.93(776)"

  28. "575.02(016)"

    "730.21(8872)"

    Returns: "1305.23(903304744888)"

  29. "0.5(9903)"

    "57.(50)"

    Returns: "58.1(0409)"

  30. "4.1670(9)"

    "90.63(7)"

    Returns: "94.8048(7)"

  31. "6.9(5)"

    "57.(7)"

    Returns: "64.7(3)"

  32. "43.7(1)"

    "3.(4)"

    Returns: "47.1(5)"

  33. "1.(9)"

    "4.36(85)"

    Returns: "6.36(85)"

  34. "0.800(9399)"

    "0.696(65475)"

    Returns: "1.497(59475054696541564875)"

  35. "19.120(469)"

    "30.66(2)"

    Returns: "49.782(691)"

  36. "9.(267)"

    "49.(2)"

    Returns: "58.(489)"

  37. "301.(9)"

    "8.8(2979)"

    Returns: "310.8(2979)"

  38. "0.97(2)"

    "76.77(92)"

    Returns: "77.7(51)"

  39. "6.602(548)"

    "7.(61)"

    Returns: "14.218(710164)"

  40. "909.5(059)"

    "0.463(65123)"

    Returns: "909.96(955714241827102)"

  41. "782.1(839)"

    "85.2(53)"

    Returns: "867.4(375193)"

  42. "76.(90)"

    "7.1(5)"

    Returns: "84.0(64)"

  43. "2.918(8)"

    "54.89(1503)"

    Returns: "57.81(0392)"

  44. "42.513(72)"

    "0.10(47)"

    Returns: "42.618(47)"

  45. "9.4(2)"

    "33.(631)"

    Returns: "43.0(538)"

  46. "81.53(65)"

    "10.49(176)"

    Returns: "92.02(832741)"

  47. "0.846(18)"

    "830.(3329)"

    Returns: "831.179(1151)"

  48. "8.39(998)"

    "5.2(85)"

    Returns: "13.68(584857)"

  49. "26.(7)"

    "7.4(7174)"

    Returns: "34.(2495)"

  50. "3.9(6)"

    "629.23000(5)"

    Returns: "633.19667(2)"

  51. "41.72(4326)"

    "388.89(078)"

    Returns: "430.61(510721342404)"

  52. "28.5(27)"

    "8403.5(4171)"

    Returns: "8432.0(6898)"

  53. "54.18(8)"

    "528.(2)"

    Returns: "582.4(1)"

  54. "2.0(9)"

    "5.4(2)"

    Returns: "7.5(2)"

  55. "13.(472)"

    "840.72(3)"

    Returns: "854.19(580)"

  56. "4.0(7536822)"

    "2.(9596441)"

    Returns: "7.0(3501242)"

  57. "4.4(3347228)"

    "4.(74904019)"

    Returns: "9.1(82512480837630309626853221323544718736697737492032387425)"

  58. "9.(62668388)"

    "6.6(7551662)"

    Returns: "16.3(02200513818501617834664143311437929594028943905254235549)"

  59. "1.(6048056)"

    "8.1(4032955)"

    Returns: "9.7(4513521)"

  60. "1.(74953047)"

    "3.(12649384)"

    Returns: "4.(87602431)"

  61. "2.(22471442)"

    "6.(3148274)"

    Returns: "8.(53954185372988737074575705214590499029249678626965619716)"

  62. "0.(1137644)"

    "0.(56115483)"

    Returns: "0.(67491924698798949375959732556621205268600022924797253127)"

  63. "9.8(2302095)"

    "3.(5753627)"

    Returns: "13.3(9838370)"

  64. "9.(9673606)"

    "9.3(8630378)"

    Returns: "19.3(5366448)"

  65. "0.(87549886)"

    "9.(3379776)"

    Returns: "10.(21347650255275206732622485313266651836846388786450929663)"

  66. "8.5(6085819)"

    "5.(84203094)"

    Returns: "14.4(02889144506129092785055700227034239918066163953038116768)"

  67. "4.(29539982)"

    "3.0(1226481)"

    Returns: "7.3(07664634180479352188094560211049435120877652247106626304)"

  68. "9.(8578415)"

    "4.(08478972)"

    Returns: "13.(94263130663205578689482992637550500647562433681367057387)"

  69. "5.(2106738)"

    "9.(20828377)"

    Returns: "14.(41895759315021982756658788210483946494445903905102935115)"

  70. "6.(03895836)"

    "7.(2350983)"

    Returns: "13.(27405668389941595487907113728187022193458713093436246819)"

  71. "8.8(7678602)"

    "9.(60800913)"

    Returns: "18.4(84795163758694128661190394036814682859221077699635687738)"

  72. "7.(3054741)"

    "1.(50497721)"

    Returns: "8.(81045134559718520523902697910776246282689180319563552462)"

  73. "4.(50706135)"

    "3.(3781498)"

    Returns: "7.(88521119288559733220451365689917005439504908428534487633)"

  74. "1.(7284245)"

    "2.(52266952)"

    Returns: "4.(25109409806915253651268094724236768397949799537709551198)"

  75. "9.(6621121)"

    "1.(3611870)"

    Returns: "11.(0232992)"

  76. "1.(22243206)"

    "2.(6386891)"

    Returns: "3.(86112122609323700913484491159593114070751388189538630097)"

  77. "5.(34304020)"

    "8.(46505942)"

    Returns: "13.(80809962)"

  78. "8.(2873467)"

    "0.(09611953)"

    Returns: "8.(38346625969586818307924044284826563406877689929982485420)"

  79. "4.(5976953)"

    "4.(43729663)"

    Returns: "9.(03499199414250232068326113265640390894329732735879706617)"

  80. "0.(5114377)"

    "2.(9954904)"

    Returns: "3.(5069282)"

  81. "5.(52141788)"

    "7.(52417694)"

    Returns: "13.(04559483)"

  82. "3.(3046733)"

    "0.(4542611)"

    Returns: "3.(7589344)"

  83. "3.(8916540)"

    "8.(82808949)"

    Returns: "12.(71974358744630389934984148217866368981152370060391725490)"

  84. "4.(65343565)"

    "3.(28072856)"

    Returns: "7.(93416421)"

  85. "1.8(1222713)"

    "5.1(6873005)"

    Returns: "6.9(8095718)"

  86. "0.(0)"

    "0.00(000)"

    Returns: "0.(0)"

  87. "0.00(000)"

    "2.99(99)"

    Returns: "3.(0)"

  88. "1.99(99)"

    "1.99(99)"

    Returns: "4.(0)"

  89. "2137.33(987)"

    "7858.9(8723)"

    Returns: "9996.32(711860375226)"

  90. "0.(12345678)"

    "0.(2468135)"

    Returns: "0.(37027030591592028048092493698146258703594759249164813813)"

  91. "17.2(123123)"

    "82.7(876)"

    Returns: "100.(0)"

  92. "0.0(999)"

    "99.99(001)"

    Returns: "100.09(001)"

  93. "9.9(1234568)"

    "0.(12345678)"

    Returns: "10.0(35802462469135935802490469138015802691269158023804691349)"

  94. "1.12(0202)"

    "1.2(1)"

    Returns: "2.3(31)"

  95. "1.999(99)"

    "8.00(01)"

    Returns: "10.00(01)"

  96. "6.6833(5498)"

    "9.5334(584)"

    Returns: "16.2168(134439570083)"

  97. "0.(74)"

    "0.(75)"

    Returns: "1.(50)"

  98. "0.(9)"

    "0.(9)"

    Returns: "2.(0)"

  99. "9.00(97)"

    "9.99(897)"

    Returns: "19.00(877695)"

  100. "9.5618(158)"

    "3.1550(1656)"

    Returns: "12.7168(323774719814)"

  101. "685.00(333)"

    "45.00(0)"

    Returns: "730.00(3)"

  102. "1.5(00001)"

    "1.5(00001)"

    Returns: "3.0(00002)"

  103. "0.1(22)"

    "0.11(22)"

    Returns: "0.23(4)"

  104. "1.123(9)"

    "0.234(1)"

    Returns: "1.358(1)"

  105. "0.(12345612)"

    "0.(1234561)"

    Returns: "0.(24691223358017244690673557956846684579577346906823580173)"

  106. "1.(6)"

    "2.(6)"

    Returns: "4.(3)"


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: