A shop applies a 2-dollar discount to every item priced above 5 dollars and totals the discounted prices. Two versions of the code, one procedural and one functional, process the same list of prices.
prices = [5, 8, 2, 9, 6] # Procedural version total = 0 for p in prices: if p > 5: total = total + (p - 2) # Functional version total_f = sum(map(lambda p: p - 2, filter(lambda p: p > 5, prices)))
Complete the table with whole numbers. Values of total are in dollars.
Unlock full course notes by signing up
State and side effects in a counter class
Consider this Python code, which defines a class and then makes three method calls on two objects.
Which of the following statements are true? Select all that apply.
Reusing pure results without changing the total
A shop calculates an order quote using a pure function: it returns the same result for the same input and produces no observable side effects. For a positive integer quantity qqq, its quote in dollars is
The application adds the quotes for three separate orders by evaluating
Counting Prolog answers for a recursive rule
A Prolog program records which course is a direct prerequisite of another and defines a recursive rule for indirect prerequisites.
The query ?- leads_to(cs101, C). is entered, and the user keeps asking for more answers until Prolog reports there are no more. Prolog uses its standard search: clauses top to bottom, goals left to right, with backtracking. How many answers for C does Prolog report in total, counting any repeated value each time it appears? Give your answer as a whole number.
Finding unknown inputs with a booking relation
A theatre booking system uses a logical relation to describe all combinations of values satisfying its stated conditions. Its relation booking(a,c)\operatorname{booking}(a,c)booking(a,c) holds when aaa is the number of adult tickets, ccc is the number of child tickets, and all of these conditions hold:
aaa and ccc are integers from 1 through 9, inclusive.
Adult tickets cost $18 each and child tickets cost $12 each.