Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68b1f72371a5ac895ac70a04 Challenge 41: File Storage 29 challenge-41

--description--

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:

  • The unit for the file size can be bytes ("B"), kilobytes ("KB"), or megabytes ("MB").
  • Return the number of whole files the drive can fit.
  • Use the following conversions:
Unit Equivalent
1 B 1 B
1 KB 1000 B
1 MB 1000 KB
1 GB 1000 MB

For example, given 500, "KB", and 1 as arguments, determine how many 500 KB files can fit on a 1 GB hard drive.

--hints--

number_of_files(500, "KB", 1) should return 2000.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(number_of_files(500, "KB", 1), 2000)`)
}})

number_of_files(50000, "B", 1) should return 20000.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(number_of_files(50000, "B", 1), 20000)`)
}})

number_of_files(5, "MB", 1) should return 200.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(number_of_files(5, "MB", 1), 200)`)
}})

number_of_files(4096, "B", 1.5) should return 366210.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(number_of_files(4096, "B", 1.5), 366210)`)
}})

number_of_files(220.5, "KB", 100) should return 453514.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(number_of_files(220.5, "KB", 100), 453514)`)
}})

number_of_files(4.5, "MB", 750) should return 166666.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(number_of_files(4.5, "MB", 750), 166666)`)
}})

--seed--

--seed-contents--

def number_of_files(file_size, file_unit, drive_size_gb):

    return file_size

--solutions--

def number_of_files(file_size, file_unit, drive_size_gb):
    drive_size_bytes = drive_size_gb * 1000 * 1000 * 1000

    if file_unit == "B":
        file_size_bytes = file_size
    elif file_unit == "KB":
        file_size_bytes = file_size * 1000
    else:
        file_size_bytes = file_size * 1000 * 1000

    return int(drive_size_bytes // file_size_bytes)