Statistics

Problem Statement for "ParameterSubstitution"

Problem Statement

You are working on a compiler of sorts that really just translates the user's code into another programming language which has its own compiler. You need to implement code translation for method calls in your language. The parameters passed into the method in your language will be given to you as a String[], params. You will also be given a translation descriptor, code, which tells you how to use the parameters in the code for the other language. The translation descriptor will have "$X" where X is a number between 1 and the number of parameters, inclusive, which signifies that the Xth parameter should be inserted in at that spot (1-based).

For instance, if params is {"x", "y", "z"} and code is "($2 + $3)/$1", you should return "(y + z)/x".

If there is ambiguity as to the number of the parameter, use the largest possible valid number less than or equal to the number of parameters - $105 would be parameter 10 followed by the character '5' if there are at least 10 parameters but less than 105 and would be parameter 1 followed by "05" if there are less than 10 parameters and at least one. Return code with all the substitutions made.

Definition

Class:
ParameterSubstitution
Method:
processParams
Parameters:
String, String[]
Returns:
String
Method signature:
String processParams(String code, String[] params)
(be sure your method is public)

Notes

  • There may be other '$' symbols in the code that don't refer to parameters.
  • There will be no leading zeros on parameter numbers (meaning that $01 doesn't refer to parameter 1, only $1 does).

Constraints

  • code will have between 0 and 50 characters, inclusive.
  • params will have between 0 and 50 elements, inclusive.
  • Each element in params will have between 0 and 50 characters, inclusive.
  • The characters in code and in each element of params will be between ASCII 32 and ASCII 126, inclusive.

Examples

  1. "if ($1 == $2) $3;"

    {"12", "12", "doWhatIWant()"}

    Returns: "if (12 == 12) doWhatIWant();"

    It looks like this code will do what I want.

  2. "$3+$1*$2-$7=$10"

    {"myvar", "490jri", "e09jd9", "dlkjfoiej"}

    Returns: "e09jd9+myvar*490jri-$7=myvar0"

    Not every variable needs to be used, and not everything that looks like a variable is.

  3. "12342123$13231232$2123211242$a$dlkj$"

    {"$2", "$1"}

    Returns: "12342123$23231232$1123211242$a$dlkj$"

    Be careful of parameter values that look like they could be other parameters.

  4. "{[(+.*-,/\\:;<=>?@)]}_`~|$$1"

    {"1{[(+.*-,/\\:;<=>?@)]}_`~|"}

    Returns: "{[(+.*-,/\\:;<=>?@)]}_`~|$1{[(+.*-,/\\:;<=>?@)]}_`~|"

    Note: The '\\' in the input is really an escaped single '\'.

  5. "$01"

    {"abc"}

    Returns: "$01"

  6. "$$$$$$$$$$1"

    {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11"}

    Returns: "$$$$$$$$$2"

  7. "$1"

    {}

    Returns: "$1"

  8. "$1"

    {""}

    Returns: ""

  9. "while($5){$1$2@($_=~$3/g).' '}$4"

    {"print ", "2**", "/[DFLT]", ";", "<>"}

    Returns: "while(<>){print 2**@($_=~/[DFLT]/g).' '};"

  10. "$1$2$3$1$2$3$$1$2$3$5$4"

    {"adc", "3098(", "***)", "!!@@##$$%%^^", "~!@)($*)(*%"}

    Returns: "adc3098(***)adc3098(***)$adc3098(***)~!@)($*)(*%!!@@##$$%%^^"

  11. "$1$2"

    {"123", "456"}

    Returns: "123456"

  12. "$3$6$12$1$5$4$1$11$7$14$8$9$17$8$10$11$13$15$2$16"

    {"Main", "Length", "using ", "void ", "{static ", "System;", "{for", "Console.", "WriteLine", ".ReadLine", "()", "class ", "Split", "(int i=0; i<10;i++)", "('D','F','L','T').", ");}}", "(1<<"}

    Returns: "using System;class Main{static void Main(){for(int i=0; i<10;i++)Console.WriteLine(1<

  13. "$2$1$11$6$9$12$8$13$1$15$6$16$4=!!$7($10$14$3p$4$5"

    {"char ", "main(){", "cout<<", "<<", "endl", "while(", "strchr", "int", "cin>>", "\"TDLF\"", "i[21];", "i){", " p=1;", ",*(n++));", "*n=i;", "*n)p"}

    Returns: "main(){char i[21];while(cin>>i){int p=1;char *n=i;while(*n)p<<=!!strchr(\"TDLF\",*(n++));cout<

  14. "{[(+.*-,/\\:;<=>?@)]}_`~|$$1"

    { "1{[(+.*-,/\\:;<=>?@)]}_`~|" }

    Returns: "{[(+.*-,/\\:;<=>?@)]}_`~|$1{[(+.*-,/\\:;<=>?@)]}_`~|"

  15. "12342123$13231232$2123211242$a$dlkj$"

    { "$2", "$1" }

    Returns: "12342123$23231232$1123211242$a$dlkj$"

  16. "$3+$1*$2-$7=$10"

    { "myvar", "490jri", "e09jd9", "dlkjfoiej", "rt", "rt", "rt", "rt", "rt", "rt", "rt", "rt", "rt" }

    Returns: "e09jd9+myvar*490jri-rt=rt"

  17. "$10$1"

    { "x", " ", " ", " ", " ", " ", " ", " ", " ", "$1" }

    Returns: "$1x"

  18. "$11$12"

    { "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "101", "102" }

    Returns: "101102"

  19. "12342123$13231232$00$21232$$11242$a$dlkj$"

    { "$2", "$1" }

    Returns: "12342123$23231232$00$11232$$21242$a$dlkj$"

  20. "if ($1 == $2) $3;"

    { " $1", "12", "doWhatIWant()" }

    Returns: "if ( $1 == 12) doWhatIWant();"

  21. "$$2"

    { "1Eryx was here $1", "1Eryx was here $1" }

    Returns: "$1Eryx was here $1"

  22. "a$2a"

    { "aap", "$1" }

    Returns: "a$1a"

  23. "$01ww"

    { "xxxx" }

    Returns: "$01ww"

  24. "$01"

    { "abc" }

    Returns: "$01"

  25. "{$12$01[(+.*-,/\\\\:;<=>?@)]}_`~|$$2"

    { "XXX", "1{[(+.*-,/\\\\:;<=>?@)]}_`~|" }

    Returns: "{XXX2$01[(+.*-,/\\\\:;<=>?@)]}_`~|$1{[(+.*-,/\\\\:;<=>?@)]}_`~|"

  26. "$10"

    { "a", "b", "c", "f", "d", "e", "r", "f", "t", "g", "h", "y", "d", "d", "d", "f", "g", "h" }

    Returns: "g"

  27. "$2"

    { "abc", "def" }

    Returns: "def"

  28. "$"

    { }

    Returns: "$"

  29. "$0$2$22$11$01$2$2$10"

    { "a", "b", "c", "", "d", "e", "r", "h", "k", "i" }

    Returns: "$0bb2a1$01bbi"

  30. "$01"

    { "a" }

    Returns: "$01"

  31. "$1053$2$02$2bitch$40fuckyo code fyocode $20$1$2$3$"

    { "death", "will", "come", "to", "your", "code", "ha ", " aha", " hah", "yueah bitch", "die bitch", "wha?" }

    Returns: "yueah bitch53will$02willbitchto0fuckyo code fyocode will0deathwillcome$"

  32. "$1234567890123456"

    { "A" }

    Returns: "A234567890123456"

  33. "$12345678991234"

    { "a" }

    Returns: "a2345678991234"

  34. "12342123$13231232$$$2123211242$a$dlkj$"

    { "$2", "$1" }

    Returns: "12342123$23231232$$$1123211242$a$dlkj$"

  35. "$01+12"

    { "x", "r", "z" }

    Returns: "$01+12"

  36. "__DOLLAR__"

    { "a" }

    Returns: "__DOLLAR__"

  37. "$1$2$3"

    { "$3", "$2", "$1" }

    Returns: "$3$2$1"

  38. " $11 $1$2 $3$4 $5 + $6 + $7 + $8 + $9 + $10 "

    { "A", "B", "C", "D", "E", "F", "G", "H", "I", "oops", "oops2" }

    Returns: " oops2 AB CD E + F + G + H + I + oops "

  39. "|$a1|"

    { "a", "b", "c" }

    Returns: "|$a1|"

  40. "$11"

    { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m" }

    Returns: "k"

  41. "$11$1$"

    { "$" }

    Returns: "$1$$"

  42. "$111111111111111111111111"

    { "a", "b", "a", "a", "a", "aa", "a", "a", "aaa", "aaa", "aa", "a", "a", "a", "" }

    Returns: "aa1111111111111111111111"

  43. "$10"

    { "a", "b", "c", "d", "e", "f", "g", "h", "ab", "cd" }

    Returns: "cd"

  44. "$dlk$102$56$p"

    { "\"one\"", "\"two\"", "\"three\"", "\"four\"", "\"five\"", "\"six\"", "\"seven\"", "\"eight\"", "\"nine\"", "\"ten\"", "\"eleven\"", "\"twelve\"" }

    Returns: "$dlk\"ten\"2\"five\"6$p"

  45. "($2)"

    { "nothing()", "$1" }

    Returns: "($1)"

  46. "blablabla$1blablabla"

    { "aaaaaaaaa$1", "kombajnn" }

    Returns: "blablablaaaaaaaaaa$1blablabla"

  47. "$2"

    { "1", "$1" }

    Returns: "$1"

  48. "$3+$1*$2-$7=$1"

    { "myvar", "490jri", "e09jd9", "dlkjfoiej", "rt", "rt", "rt", "rt", "rt", "rt", "rt", "rt", "rt" }

    Returns: "e09jd9+myvar*490jri-rt=myvar"

  49. "$$01$02$1$2$ $$$"

    { "$02", "33", "10078", "22342" }

    Returns: "$$01$02$0233$ $$$"

  50. "$"

    { "1" }

    Returns: "$"

  51. "$2347893492783642396854$638947239847238497"

    { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p" }

    Returns: "b347893492783642396854f38947239847238497"

  52. "$1$11$1$9$0$12"

    { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$A", "$B", "$C" }

    Returns: "$1$B$1$9$0$C"

  53. "$9763456789098765434567865434567"

    { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }

    Returns: "9763456789098765434567865434567"

  54. "$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1$1"

    { "01234567890123456789012345678901234567890123456789" }

    Returns: "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"

  55. "\"$25\""

    { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1" }

    Returns: "\"15\""

  56. "__d__ $1"

    { "$2", "$1" }

    Returns: "__d__ $2"

  57. "$10"

    { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n" }

    Returns: "j"

  58. "$1"

    { "$1$1$1$1" }

    Returns: "$1$1$1$1"

  59. "$3$2$1"

    { "Hello", "Hi", "$1" }

    Returns: "$1HiHello"

  60. ""

    { }

    Returns: ""

  61. "$49$50$51"

    { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y" }

    Returns: "xyE1"

  62. "$10$02$3$$$35$03$010$101"

    {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}

    Returns: "10$023$$35$03$010101"

  63. "\\\\\\{$$21[(+.*-,$02/\\\\:;<=>?@)$001]}_`~|$$1\\"

    {"\\1{[(+.*-,/\\:;<=>?@)]}_`~|\\\\", "2"}

    Returns: "\\\\\\{$21[(+.*-,$02/\\\\:;<=>?@)$001]}_`~|$\\1{[(+.*-,/\\:;<=>?@)]}_`~|\\\\\\"

  64. "$$21"

    {"z", "1"}

    Returns: "$11"


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: