For each of the calls to the following recursive function be…
For each of the calls to the following recursive function below, indicate what value is returned: string mystery6(string s) { if (s.length() == 1) { return “*” + s + “*”; } else if (s.length() % 2 == 0) { string rest = s.substr(1, s.length() – 1); return s[ s.length() – 1 ] + mystery6(rest) + s[ 0 ]; } else { string rest = s.substr(1); return “-” + mystery6(rest); } } mystery6(“hello”) [r1] mystery6(“worlds”) [r2] mystery6(“koala”) [r3]
Read DetailsFor each of the calls to the following recursive function be…
For each of the calls to the following recursive function below, indicate what value is returned: string mystery1 (string s, char c1, char c2) { if (s.empty()) { return s; } else if (s[ 0 ] == c1) { s.erase(s.begin(), s.begin() + 1); s = mystery1(s, c1, c2); s.insert(s.end() – 1, c2); return s; } else { char c3 = s[ 0 ]; s.erase(s.begin(), s.begin() + 1); s = mystery1(s, c1, c2); s.insert(s.end() – 1, c3); return s[ 0 ] + mystery1(s.substr(1),c1, c2); } } mystery1(“crazy raccoons”, ‘c’, ‘k’) [r1] mystery1(“BANANA”, ‘A’, ‘O’) [r2] mystery1(“sessions”, ‘s’, ‘X’) [r3]
Read DetailsFor each of the calls to the following recursive function be…
For each of the calls to the following recursive function below, indicate what value is returned: string mystery5(string s) { if (s.length() == 0) { return s; } else if (s.length() % 2 == 0) { string rest = s.substr(0, s.length() – 1); string last = s.substr(s.length() – 1, 1); return last + mystery5(rest); } else { string first = s.substr(0, 1); string rest = s.substr(1); return “(” + first + “)” + mystery5(rest); } } mystery5(“foo”) [r1] mystery5(“kakuro”) [r2] mystery5(“computer”) [r4]
Read Details