Skip to content

Commit

Permalink
# Significant Figures Challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
ikostan committed Dec 20, 2024
1 parent 1ba22dc commit 4dd10c6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
1 change: 1 addition & 0 deletions kyu_7/significant_figures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Significant Figures Challenge."""
18 changes: 10 additions & 8 deletions kyu_7/significant_figures/number_of_sigfigs.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""
Solution for -> Significant Figures
Solution for -> Significant Figures.
Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""


def number_of_sigfigs(number: str) -> int:
"""
return the number of sigfigs in the
passed in string "number"
Return the number of significant figures in the given number.
:param number:
:return:
"""
Expand All @@ -28,8 +29,8 @@ def number_of_sigfigs(number: str) -> int:

def normalize_string(number: str) -> str:
"""
Normalize string by converting it into a
number and back to string once again
Normalize string by converting it into a number and back to string again.
:param number:
:return:
"""
Expand All @@ -44,12 +45,12 @@ def normalize_string(number: str) -> str:

def remove_extra_zeroes(number: str) -> str:
"""
Remove all zeroes from the end of the string
Remove all zeroes from the end of the string.
:param number:
:return:
"""
index = None

for i in range(-1, len(number) * -1, -1):
if number[i] == '0':
index = i
Expand All @@ -64,7 +65,8 @@ def remove_extra_zeroes(number: str) -> str:

def remove_extra_leading_zeroes(number: str) -> str:
"""
Remove all extra leading zeroes from the head of the string
Remove all extra leading zeroes from the head of the string.
:param number:
:return:
"""
Expand Down
55 changes: 27 additions & 28 deletions kyu_7/significant_figures/test_number_of_sigfigs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Test for -> Significant Figures
Test for -> Significant Figures.
Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""
Expand All @@ -8,6 +9,7 @@

import unittest
import allure
from parameterized import parameterized
from utils.log_func import print_log
from kyu_7.significant_figures.number_of_sigfigs import number_of_sigfigs

Expand All @@ -26,14 +28,28 @@
url='https://www.codewars.com/kata/5d9fe0ace0aad7001290acb7',
name='Source/Kata')
class NumberOfSigFigsTestCase(unittest.TestCase):
"""
Testing number_of_sigfigs function
"""
"""Testing number_of_sigfigs function."""

def test_number_of_sigfigs(self):
@parameterized.expand([
(1, "1"),
(0, "0"),
(1, "0003"),
(1, "3000"),
(3, "404"),
(7, "050030210"),
(1, "0.1"),
(2, '1.0'),
(3, '4.40'),
(4, '90.00'),
(1, "0.0"),
(9, '03.27310000'),
(10, '23625700.00'),
(10, '09.971730000'),
(10, '0000.0673560000')])
def test_number_of_sigfigs(self, exp, inp):
"""
Testing number_of_sigfigs function
with various test inputs
Testing 'number_of_sigfigs' function with various test inputs.
:return:
"""
# pylint: disable-msg=R0801
Expand All @@ -47,24 +63,7 @@ def test_number_of_sigfigs(self):
'<h3>Test Description:</h3>'
"<p></p>")
# pylint: enable-msg=R0801
with allure.step("Pass string and verify the output"):
test_data: tuple = (
(1, "1"),
(0, "0"),
(1, "0003"),
(1, "3000"),
(3, "404"),
(7, "050030210"),
(1, "0.1"),
(2, '1.0'),
(3, '4.40'),
(4, '90.00'),
(1, "0.0"),
(9, '03.27310000'),
(10, '23625700.00'),
(10, '09.971730000'),
(10, '0000.0673560000'))

for exp, inp in test_data:
print_log(inp=inp, expected=exp)
self.assertEqual(exp, number_of_sigfigs(inp))
with allure.step(f"Pass a number: {inp} "
f"and verify the expected output: {exp}."):
print_log(inp=inp, expected=exp)
self.assertEqual(exp, number_of_sigfigs(inp))

0 comments on commit 4dd10c6

Please sign in to comment.