Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer in Python (no imports) Directions: - Given lst: a list of integers; and operations, a list of tuples, apply all the commands to

Please answer in Python (no imports)

Directions: - Given lst: a list of integers; and operations, a list of tuples, apply all the commands to the list in the given order and return the transformed list. - All lambda functions should return a list of money after the command is applied, but not change the original list. - Possible commands: 1. ('add', amount): buys in. Adds amount to each session; E.g: amount = 1 [1, 2, 3] -> [1 + 1, 2 + 1, 3 + 1] 2. ('lose', amount): lost a hand. Subtracts amount to each session 3. ('combine', k): combines the k subsequent numbers into one, assume k is always less than the length of the list and k > 1 E.g: k = 2, [1, 2, 3, 4] -> [1 + 2, 2 + 3, 3 + 4] = [3, 5, 7]; E.g: k = 3, [1, 2, 3, 4] -> [1 + 2 + 3, 2 + 3 + 4] = [6, 9]; 4. ('first', k): takes the first k digits of the number, note k might be larger than the length of some number E.g: k = 4, [123, 1234, 12345] -> [123, 1234, 1234]; 5. ('last', k): takes the last k digits of the number, note k might be larger than the length of some number E.g: k = 4, [123, 1234, 12345] -> [123, 1234, 2345]; 6. ('reduce_last', k): replaces the last k numbers with their sum, assume k is always less than the length of the list E.g: k = 3, [1, 2, 3, 4, 5, 6] -> [1, 2, 3, 4 + 5 + 6] = [1, 2, 3, 15].

Doctests: >>> lst = [1, 12, 123, 1234, 12345, 123456] >>> operations_1 = [('add', 5), ('last', 3), ('reduce_last', 2)] >>> session_money(lst, operations_1) [6, 17, 128, 239, 811] >>> operations_2 = [('first', 3), ('combine', 2), ('lose', 10)] >>> session_money(lst, operations_2) [3, 125, 236, 236, 236]

Function starter code (need to add commands to the lambda functions): def session_money(lst, operations): commands = { 'add': lambda lst, amount: ..., 'lose': lambda lst, amount: ..., 'combine': lambda lst, k: , 'first': lambda lst, k: ..., 'last': lambda lst, k: ..., 'reduce_last': lambda lst, k: ... } return #code here

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions