University of Arizona, Department of Computer Science

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:

Solutions that go outside these constructs, e.g., by using for/while loops or list comprehensions, will not get credit.

Examples

  1. fmt("testing",[1,2,3])
    return value: 'testing'

  2. fmt("{{},{},{}}",[1,'two',3])
    return value: '{1,two,3}'

  3. fmt("L = {}.",["just a test".split()])
    return value: "L = ['just', 'a', 'test']."

  4. fmt("x = {}, y = {}", [10, 20])
    return value: 'x = 10, y = 20'