mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-11 18:02:40 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1.6 KiB
1.6 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6821eca2237de8297eaee79e | Python Challenge 16: Reverse Parenthesis | 29 | python-challenge-16 |
--description--
Given a string that contains properly nested parentheses, return the decoded version of the string using the following rules:
- All characters inside each pair of parentheses should be reversed.
- Parentheses should be removed from the final result.
- If parentheses are nested, the innermost pair should be reversed first, and then its result should be included in the reversal of the outer pair.
- Assume all parentheses are evenly balanced and correctly nested.
--hints--
decode("(f(b(dc)e)a)") should return "abcdef".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(decode("(f(b(dc)e)a)"), "abcdef")`)
}})
decode("((is?)(a(t d)h)e(n y( uo)r)aC)") should return "Can you read this?".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(decode("((is?)(a(t d)h)e(n y( uo)r)aC)"), "Can you read this?")`)
}})
decode("f(Ce(re))o((e(aC)m)d)p") should return "freeCodeCamp".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(decode("f(Ce(re))o((e(aC)m)d)p"), "freeCodeCamp")`)
}})
--seed--
--seed-contents--
def decode(s):
return s
--solutions--
def decode(s):
while ')' in s:
close_index = s.index(')')
open_index = s.rindex('(', 0, close_index)
inner = s[open_index + 1:close_index][::-1]
s = s[:open_index] + inner + s[close_index + 1:]
return s