Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  1. ("heal", ) add this to every characters' health points in the list

[1, 2, 3, 4], ("heal", 1) -> [2, 3, 4, 5]

  1. ("multi_heal", ) multiply every characters' health points in the list by this

[1, 2, 3, 4], ("multi_heal", 2) -> [2, 4, 6, 8]

  1. ("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]

  1. ("remove", ) remove the characters' health points at

[1, 2, 3, 4], ("remove", 1) -> [1, 3, 4]

  1. ("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]

  1. ("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:

  1. Defining a dictionary of lambda functions may be helpful in this question.
  2. 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.
  3. 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

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions