CSc 120: Format (Recursive)
Expected Behavior
Write a recursive function fmt(spec, values) that is a very simple variant of Python's
str.format method. Here's an example of usage:
>>> fmt("x = {}, y = {}", [10, 20])
'x = 10, y = 20'
fmt(spec, values)
returns a copy of the string spec
where the Nth occurrence of "{}"
is replaced by the Nth value in values
.
Assume that there are a sufficient number of values to fill all occurrences of "{}". If there are excess values, they are simply ignored.
Programming Requirements
Solve this problem using recursion.
IMPORTANT: You may not use str.format
.
Programming Requirements
Solve this problem using recursion. You are allowed to use only
the following programming constructs:
-
if statements;
-
return statements;
-
assignment;
-
recursive function calls (if you use a helper function, then it must be recursive);
-
comparison operations (==, !=, etc.);
-
list indexing and slicing;
-
list and string concatenation.
Solutions that go outside these constructs, e.g., by
using for/while loops or list comprehensions,
will not get credit.
Examples
-
fmt("testing",[1,2,3])
return value: 'testing'
-
fmt("{{},{},{}}",[1,'two',3])
return value: '{1,two,3}'
-
fmt("L = {}.",["just a test".split()])
return value: "L = ['just', 'a', 'test']."
-
fmt("x = {}, y = {}", [10, 20])
return value: 'x = 10, y = 20'