Skip to content

Commit df3ffe6

Browse files
committed
feat(day 41): implement file-storage capacity calculator with unit conversion
1 parent 14fd9ec commit df3ffe6

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
File Storage
3+
Given a file size, a unit for the file size, and hard drive capacity in gigabytes (GB), return the number of files the hard drive can store using the following constraints:
4+
5+
The unit for the file size can be bytes ("B"), kilobytes ("KB"), or megabytes ("MB").
6+
Return the number of whole files the drive can fit.
7+
Use the following conversions:
8+
Unit Equivalent
9+
1 B 1 B
10+
1 KB 1000 B
11+
1 MB 1000 KB
12+
1 GB 1000 MB
13+
For example, given 500, "KB", and 1 as arguments, determine how many 500 KB files can fit on a 1 GB hard drive.
14+
15+
16+
17+
"""
18+
19+
def number_of_files(file_size, file_unit, drive_size_gb):
20+
21+
if file_unit == "B":
22+
size_in_bytes = file_size
23+
elif file_unit == "KB":
24+
size_in_bytes = file_size * 1000
25+
elif file_unit == "MB":
26+
size_in_bytes = file_size * 1000 * 1000
27+
else:
28+
raise ValueError("Invalid unit. Use 'B', 'KB', or 'MB'.")
29+
30+
31+
drive_size_bytes = drive_size_gb * 1000 * 1000 * 1000
32+
return drive_size_bytes // size_in_bytes
33+
34+
35+
36+
37+
if __name__ == "__main__":
38+
print(number_of_files(500,"KB",1))
39+
print(number_of_files(50000,"B",1))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
from FileStorage2 import number_of_files
3+
4+
class FileStorge2Test(unittest.TestCase):
5+
6+
def test1(self):
7+
self.assertEqual(number_of_files(500,"KB",1),2000)
8+
9+
def test2(self):
10+
self.assertEqual(number_of_files(50000,"B",1),20000)
11+
12+
def test3(self):
13+
self.assertEqual(number_of_files(5,"MB",1),200)
14+
15+
def test4(self):
16+
self.assertEqual(number_of_files(4096,"B",1.5),366210)
17+
18+
def test5(self):
19+
self.assertEqual(number_of_files(220.5,"KB",100),453514)
20+
21+
def test6(self):
22+
self.assertEqual(number_of_files(4.5,"MB",750),166666)
23+
24+
25+
if __name__ == "__main__":
26+
unittest.main()

0 commit comments

Comments
 (0)