Recovering a price after pair alignment changes

An invoice test program uses simultaneous recursion, organizing one recursive computation around two list inputs. Each quantity is paired with the unit price at the same position. The helper head(x) returns the first entry, and tail(x) removes that entry.


Python
def invoice_total(quantities, prices):
    if not quantities or not prices:
        return 0
    return (
        head(quantities) * head(prices)
        + invoice_total(tail(quantities), tail(prices))
    )


The quantity list is [4, 2, 5], measured in items, and the original price list is [3, 6, 2, 8], measured in dollars per item. The function excludes entries remaining after either list becomes empty. Assume there are no taxes or other charges.


Find both quantities below:

  • The total returned for the original lists, in dollars, to two decimal places.
  • The unit price that must be inserted at the beginning of the original price list to make the returned total exactly $64.00, in dollars per item, to two decimal places. In this second test, the quantities stay unchanged and all original prices shift one position to the right.
Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Simultaneous Recursion Questions: