← Back to challenges

Deadly Virus

PythonHardalgorithmsarrayslogiclanguage_fundamentals

Instructions

Mubashir needs your help to identify the spread of a deadly virus. He can provide you with the following parameters:

  • A two-dimensional array persons, containing affected persons 'V' and unaffected persons 'P'.
  • Number of hours n, each infected person is spreading the virus to one person up, down, left and right each hour.

Your function should return the updated array containing affected and unaffected persons after n hours.

Examples

persons = [
  ["P", "P", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

deadly_virus(persons, 0) ➞ [
  ["P", "P", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

deadly_virus(persons, 1) ➞ [
  ["V", "P", "P", "P", "P"],
  ["V", "V", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

deadly_virus(persons, 2) ➞ [
  ["V", "V", "P", "P", "P"],
  ["V", "V", "V", "P", "P"],
  ["V", "V", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.