Skip to content

Commit 88e593b

Browse files
committed
feat(day 42): implement video storage calculator with unit validation and byte conversion
1 parent d221d5c commit 88e593b

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Video Storage
3+
Given a video size, a unit for the video size, a hard drive capacity, and a unit for the hard drive, return the number of videos the hard drive can store using the following constraints:
4+
5+
The unit for the video size can be bytes ("B"), kilobytes ("KB"), megabytes ("MB"), or gigabytes ("GB").
6+
If not given one of the video units above, return "Invalid video unit".
7+
The unit of the hard drive capacity can be gigabytes ("GB") or terabytes ("TB").
8+
If not given one of the hard drive units above, return "Invalid drive unit".
9+
Return the number of whole videos the drive can fit.
10+
Use the following conversions:
11+
Unit Equivalent
12+
1 B 1 B
13+
1 KB 1000 B
14+
1 MB 1000 KB
15+
1 GB 1000 MB
16+
1 TB 1000 GB
17+
For example, given 500, "MB", 100, and "GB as arguments, determine how many 500 MB videos can fit on a 100 GB hard drive.
18+
"""
19+
20+
21+
def number_of_videos(video_size, video_unit, drive_size, drive_unit):
22+
23+
unit_map = {
24+
"KB": 1000,
25+
"MB": 1000 * 1000,
26+
"GB": 1000 * 1000 * 1000
27+
}
28+
29+
drive_map = {
30+
"GB": 1000 * 1000 * 1000 ,
31+
"TB": 1000 * 1000 * 1000 * 1000
32+
}
33+
34+
if video_unit not in unit_map:
35+
return "Invalid video unit"
36+
37+
if drive_unit not in drive_map:
38+
return "Invalid drive unit"
39+
40+
video_bytes = video_size * unit_map[video_unit]
41+
42+
drive_bytes = drive_size * drive_map[drive_unit]
43+
44+
return drive_bytes // video_bytes
45+
46+
if __name__ == "__main__":
47+
print(number_of_videos(500,'MB',100,"GB"))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from VideoStorage import number_of_videos
3+
4+
class VideoStorageTest(unittest.TestCase):
5+
6+
def test1(self):
7+
self.assertEqual(number_of_videos(500,"MB",100,"GB"),200)
8+
9+
def test2(self):
10+
self.assertEqual(number_of_videos(2000,"B",1,"TB"),"Invalid video unit")
11+
12+
def test3(self):
13+
self.assertEqual(number_of_videos(2000,"MB",100000,"MB"),"Invalid drive unit")
14+
15+
def test4(self):
16+
self.assertEqual(number_of_videos(500000,"KB",2,"TB"),4000)
17+
18+
def test5(self):
19+
self.assertEqual(number_of_videos(1.5,"GB",2.2,"TB"),1466)
20+
21+
22+
if __name__ == "__main__":
23+
unittest.main()

0 commit comments

Comments
 (0)