Skip to content

Commit 2178d29

Browse files
committed
feat(day 84): simulate internet worm spread with periodic patch reduction
1 parent fe2fac5 commit 2178d29

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

FreeCodeCamp/CodingQ/Infected.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Infected
3+
On November 2nd, 1988, the first major internet worm was released, infecting about 10% of computers connected to the internet after only a day.
4+
5+
In this challenge, you are given a number of days that have passed since an internet worm was released, and you need to determine how many computers are infected using the following rules:
6+
7+
On day 0, the first computer is infected.
8+
Each subsequent day, the number of infected computers doubles.
9+
Every 3rd day, a patch is applied after the virus spreads and reduces the number of infected computers by 20%. Round the number of patched computers up to the nearest whole number.
10+
For example, on:
11+
12+
Day 0: 1 total computer is infected.
13+
Day 1: 2 total computers are infected.
14+
Day 2: 4 total computers are infected.
15+
Day 3: 8 total computers are infected. Then, apply the patch: 8 infected * 20% = 1.6 patched. Round 1.6 up to 2. 8 computers infected - 2 patched = 6 total computers infected after day 3.
16+
Return the number of total infected computers after the given amount of days have passed.
17+
"""
18+
19+
import unittest,math
20+
21+
class InfectedTest(unittest.TestCase):
22+
23+
def test1(self):
24+
self.assertEqual(infected(1),2)
25+
26+
def test2(self):
27+
self.assertEqual(infected(3),6)
28+
29+
def test3(self):
30+
self.assertEqual(infected(8),152)
31+
32+
def test4(self):
33+
self.assertEqual(infected(17),39808)
34+
35+
def test5(self):
36+
self.assertEqual(infected(25),5217638)
37+
38+
39+
40+
41+
def infected(days):
42+
infected = 1
43+
for day in range(1 , days+1):
44+
infected *= 2
45+
if day%3 == 0:
46+
patched = math.ceil(infected * 0.20)
47+
infected -= patched
48+
49+
50+
51+
return infected
52+
53+
if __name__ == "__main__":
54+
print(infected(8))
55+
unittest.main()

0 commit comments

Comments
 (0)