Skip to content

Commit

Permalink
Merge pull request #606 from iKostanOrg/kyu6
Browse files Browse the repository at this point in the history
Merge pull request #605 from iKostanOrg/master
  • Loading branch information
ikostan authored Jan 2, 2025
2 parents 095f05f + ffb0bdd commit 092a71a
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
kyu\_6.conversion\_between\_kilobyte\_and\_kibibyte.module package
==================================================================

Subpackages
-----------

.. toctree::
:maxdepth: 4

kyu_6.conversion_between_kilobyte_and_kibibyte.readme
kyu_6.conversion_between_kilobyte_and_kibibyte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
README
======

.. include:: ../../kyu_6/conversion_between_kilobyte_and_kibibyte/README.md
:parser: myst_parser.sphinx_
32 changes: 32 additions & 0 deletions docs/kyu_6/kyu_6.conversion_between_kilobyte_and_kibibyte.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
kyu\_6.conversion\_between\_kilobyte\_and\_kibibyte package
===========================================================

Submodules
----------

kyu\_6.conversion\_between\_kilobyte\_and\_kibibyte.solution module
-------------------------------------------------------------------

.. automodule:: kyu_6.conversion_between_kilobyte_and_kibibyte.solution
:members:
:undoc-members:
:show-inheritance:
:private-members:

kyu\_6.conversion\_between\_kilobyte\_and\_kibibyte.test\_memory\_size\_conversion module
-----------------------------------------------------------------------------------------

.. automodule:: kyu_6.conversion_between_kilobyte_and_kibibyte.test_memory_size_conversion
:members:
:undoc-members:
:show-inheritance:
:private-members:

Module contents
---------------

.. automodule:: kyu_6.conversion_between_kilobyte_and_kibibyte
:members:
:undoc-members:
:show-inheritance:
:private-members:
1 change: 1 addition & 0 deletions docs/kyu_6/kyu_6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Subpackages
kyu_6.casino_chips.module
kyu_6.character_frequency.module
kyu_6.color_choice.module
kyu_6.conversion_between_kilobyte_and_kibibyte.module
kyu_6.count_letters_in_string.module
kyu_6.decipher_this.module
kyu_6.default_list.module
Expand Down
88 changes: 46 additions & 42 deletions kyu_6/README.md

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions kyu_6/conversion_between_kilobyte_and_kibibyte/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Conversion between Kilobyte and KibiByte

## Background

You most probably know, that the kilo used by IT-People differs
from the kilo used by the rest of the world. Whereas kilo in kB
is (mostly) interpreted as 1024 Bytes (especially by operating
systems) the non-IT kilo denotes the factor 1000 (as in "1 kg is
1000g"). The same goes for the prefixes mega, giga, tera, peta
and so on. To avoid misunderstandings (like your hardware shop
selling you a 1E+12 Byte hard disk as 1 TB, whereas your Windows
states that it has only 932 GB, because the shop uses factor 1000
whereas operating systems use factor 1024 by default) the
International Electro-technical Commission has proposed to use
KibiByte for 1024 Byte.The according unit symbol would be KiB.

Other Prefixes would be respectively:

```bash
1 MiB = 1024 KiB
1 GiB = 1024 MiB
1 TiB = 1024 GiB
```

## Task

Your task is to write a conversion function between the kB and the
KiB-Units. The function receives as parameter a memory size including
a unit and converts into the corresponding unit of the other system:

```bash
memorysizeConversion ( "10 KiB") -> "10.24 kB"
memorysizeConversion ( "1 kB") -> "0.977 KiB"
memorysizeConversion ( "10 TB") -> "9.095 TiB"
memorysizeConversion ( "4.1 GiB") -> "4.402 GB"
```

## Hints

- the parameter always contains a (fraction) number, a whitespace and
a valid unit
- units are case sensitive, valid units are kB MB GB TB KiB MiB GiB TiB
- result must be rounded to 3 decimals (round half up,no trailing zeros)
see examples above

Resources If you want to read more on [...ibi-Units](https://en.wikipedia.org/wiki/Kibibyte)

[Source](https://www.codewars.com/kata/5a115ff080171f9651000046)
1 change: 1 addition & 0 deletions kyu_6/conversion_between_kilobyte_and_kibibyte/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Conversion between Kilobyte and KibiByte."""
52 changes: 52 additions & 0 deletions kyu_6/conversion_between_kilobyte_and_kibibyte/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Solution for -> Conversion between Kilobyte and KibiByte.
Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""

UNITS: dict = {
'KiB': ("kB", 1.024),
"TiB": ("TB", 1.09951163),
"GiB": ("GB", 1.07374182),
'MiB': ('MB', 1.048576),
'kB': ("KiB", 0.9765625),
"TB": ("TiB", 0.9094947),
"GB": ("GiB", 0.93132257461548),
'MB': ('MiB', 0.95367432)}


def memorysize_conversion(memory_size: str) -> str:
"""
Convert between the kB and the KiB-Units.
The function receives as parameter a memory size
including a unit and converts into the corresponding
unit of the other system.
:param memory_size: str
:return: str
"""
# Extract units and ratio
units, convertor = unit_extractor(memory_size)
# Convert memory size into float
memory: float = float(memory_size.split()[0]) * convertor
# No trailing zeros allowed
memory_str: str = f'{memory:.3f}'
return f'{memory_str.rstrip("0")} {units}'


def unit_extractor(memory_size: str) -> tuple:
"""
Convert between multiple-byte units.
:param memory_size: str
:return: str
"""
units: str = ''
convertor: float = 0.0
for key, val in UNITS.items():
if key in memory_size:
units = val[0]
convertor = val[1]
break
return units, convertor
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""
Test for -> # Conversion between Kilobyte and KibiByte.
Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""

# MATHEMATICS STRINGS FUNDAMENTALS

import unittest
import allure
from parameterized import parameterized
from utils.log_func import print_log
from kyu_6.conversion_between_kilobyte_and_kibibyte.solution import (
memorysize_conversion,
unit_extractor)


# pylint: disable-msg=R0801
@allure.epic('6 kyu')
@allure.parent_suite('Novice')
@allure.suite("Fundamentals")
@allure.sub_suite("Unit Tests")
@allure.feature("Math")
@allure.story('# Conversion between Kilobyte and KibiByte.')
@allure.tag('MATHEMATICS',
'MATH',
'STRINGS',
'FUNDAMENTALS')
@allure.link(
url='https://www.codewars.com/kata/5a115ff080171f9651000046',
name='Source/Kata')
# pylint: enable-msg=R0801
class MemorySizeConversionTestCase(unittest.TestCase):
"""Test 'Conversion between Kilobyte and KibiByte' solution."""

@parameterized.expand([
('1 KiB', '1.024 kB', '1 KiB is 1.024 kB'),
("1 MiB", "1.049 MB", "1 MiB is 1.049 MB"),
("1 GB", "0.931 GiB", "1 GB is 0.931 GiB"),
("163.287 GiB", "175.328 GB", "163.287 GiB GiB should equal 175.328 GB"),
('974.834 KiB', '998.23 kB', 'No trailing zeros allowed.')
])
def test_memorysize_conversion(self, memory_size, expected, err):
"""
Test 'memorysize_conversion' function with various test data.
:param memory_size: str
:param expected: str
:param err: str
:return:
"""
# pylint: disable-msg=R0801
allure.dynamic.title("Testing 'memorysize_conversion' function:")
allure.dynamic.severity(allure.severity_level.NORMAL)
allure.dynamic.description_html(
'<h3>Codewars badge:</h3>'
'<img src="https://www.codewars.com/users/myFirstCode'
'/badges/large">'
'<h3>Test Description:</h3>'
"<p>"
"Your task is to write a conversion function between the kB and "
"the KiB-Units. The function receives as parameter a memory size "
"including a unit and converts into the corresponding unit of the "
"other system:"
"<ul>"
"<li>10 KiB -> 10.24 kB</li>"
"<li>1 kB -> 0.977 KiB</li>"
"<li>10 TB -> 9.095 TiB</li>"
"<li>4.1 GiB -> 4.402 GB</li>"
"</ul>"
"</p>")
# pylint: enable-msg=R0801
result: str = memorysize_conversion(memory_size)
with allure.step(f"Pass memory size: {memorysize_conversion} "
f"and assert the result: {result} vs "
f"expected: {expected}"):

print_log(memory_size=memory_size,
result=result,
expected=expected,
err=err)

self.assertEqual(result, expected, msg=err)

@parameterized.expand([
('1 KiB', 'kB', 1.024),
("1 MiB", "MB", 1.048576),
("1 GB", "GiB", 0.93132257461548)])
def test_unit_extractor(self, memory_size, units_exp, ratio_exp):
"""
Test 'unit_extractor' function with various test data.
:param memory_size: str
:param units_exp: str
:param ratio_exp: float
:return:
"""
# pylint: disable-msg=R0801
allure.dynamic.title("Testing 'unit_extractor' function:")
allure.dynamic.severity(allure.severity_level.NORMAL)
allure.dynamic.description_html(
'<h3>Codewars badge:</h3>'
'<img src="https://www.codewars.com/users/myFirstCode'
'/badges/large">'
'<h3>Test Description:</h3>'
"<p>"
"Test a conversion function between the kB and "
"the KiB-Units:"
"<ul>"
"<li>KiB -> kB, 1.024</li>"
"<li>kB -> KiB, 0.9765625</li>"
"</ul>"
"etc..."
"</p>")
# pylint: enable-msg=R0801
units, ratio = unit_extractor(memory_size)
with allure.step(f"Pass memory size: {memorysize_conversion} "
f"and assert the units: {units} "
f" and ratio: {ratio}"):

print_log(memory_size=memory_size,
units=units,
units_exp=units_exp,
ratio=ratio,
ratio_exp=ratio_exp)

# assert units conversion
self.assertEqual(units, units_exp)
# assert ratio
self.assertEqual(ratio, ratio_exp)

0 comments on commit 092a71a

Please sign in to comment.