Question
Given a list of health points (containing ints/floats) from various characters in Overwatch and a sequence of instructions (list of tuples), apply all instructions to
Given a list of health points (containing ints/floats) from various characters in Overwatch and a sequence of instructions (list of tuples), apply all instructions to the list of health points in the given order and return the transformed list representing the final health points of characters.
All possible instructions are as follows:
- ("heal",
) add this to every characters' health points in the list
[1, 2, 3, 4], ("heal", 1) -> [2, 3, 4, 5]
- ("multi_heal",
) multiply every characters' health points in the list by this
[1, 2, 3, 4], ("multi_heal", 2) -> [2, 4, 6, 8]
- ("insert",
, ) insert to the list, so that after insertion, this new characters' health points is at
[1, 2, 3, 4], ("insert", 1, 100) -> [1, 100, 2, 3, 4]
- ("remove",
) remove the characters' health points at
[1, 2, 3, 4], ("remove", 1) -> [1, 3, 4]
- ("mean",)replace every characters' health points with the mean of all characters' health points
[1, 2, 3, 4], ("mean") -> [2.5, 2.5, 2.5, 2.5]
- ("range",) replace every characters' health points with the range of all characters' health points (the difference between max value and min value)
[1, 2, 3, 4], ("range") -> [3, 3, 3, 3]
You may assume that the provided operations would never cause any error, and the list will never be empty at any point during the execution. For insert and remove, you can assume that any indices passed in will be non-negative.
Notes:
- Defining a dictionary of lambda functions may be helpful in this question.
- After calculating the mean, the data type of all numbers will always become float, even if the actual value can be an int! Make sure your doctests account for this issue. For example: 2.0 instead of 2.
- Notice that ("mean",) and ("range",) have a comma before the right parenthesis. This is the syntax of declaring a tuple with one element. Without this comma, Python will not interpret the parentheses as a tuple declaration.
defchars_health(chars_health_points, instructions):
"""
>>> chars_health([1, 2, 3, 4], [('heal', 1)])
[2, 3, 4, 5]
>>> chars_health([3.3, 6.6, 7.7],
... [('insert', 1, 5.5), ('insert', 1, 4.4)])
[3.3, 4.4, 5.5, 6.6, 7.7]
>>> chars_health([9.9, 1.3, 8.2, 4, 10],
... [('remove', 0), ('mean',), ('range',), ('heal', 10)])
[10.0, 10.0, 10.0, 10.0]
"""
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started