From 830b49dd73f650fe02d09e8f428fe10d5a3c2cfc Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Mon, 6 Jan 2025 02:58:09 +0200 Subject: [PATCH 01/12] creating solution and test files --- solutions/largest_perfect_square_less_than_number.py | 0 solutions/tests/README.md | 1 - solutions/tests/test_largest_perfect_square_less_than_number.py | 0 3 files changed, 1 deletion(-) create mode 100644 solutions/largest_perfect_square_less_than_number.py create mode 100644 solutions/tests/test_largest_perfect_square_less_than_number.py diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/tests/README.md b/solutions/tests/README.md index 007eb9551..e69de29bb 100644 --- a/solutions/tests/README.md +++ b/solutions/tests/README.md @@ -1 +0,0 @@ -# Tests diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py new file mode 100644 index 000000000..e69de29bb From d55058366d3642332cb44d8a4139f989a82654e8 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Mon, 6 Jan 2025 03:25:03 +0200 Subject: [PATCH 02/12] adding module docstring --- ...largest_perfect_square_less_than_number.py | 14 +++++++++ ...largest_perfect_square_less_than_number.py | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index e69de29bb..83f922c1b 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +A module for finding the largest perfect square less than a given number. + +Module contents: + - largest_perfect_square_less_than_number: + returns the largest perfect square that is less than the given number. + + +Created on 2025-01-06 +@author: Huda Alamassi +""" diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py index e69de29bb..c1c69376a 100644 --- a/solutions/tests/test_largest_perfect_square_less_than_number.py +++ b/solutions/tests/test_largest_perfect_square_less_than_number.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Test module for largest_perfect_square_less_than function. + +Contains tests for checking the functionality of finding + +the largest perfect square less than a given number. + +Test categories: + + - Functionality tests: checking if the function correctly identifies + the largest perfect square less than a number. + + - Edge tests: + - Small positive numbers + - Perfect squares + - Very large numbers + - Zero + - Non-integer values + + - Defensive tests: ensuring the function handles invalid inputs such as: + - Negative numbers + - Non-numeric inputs + + +Created on 2025-01-06 +@author: Huda Alamassi +""" From c1764b5dd8c1963f90fb6a5f95de154a1faf7d1b Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Mon, 6 Jan 2025 06:11:32 +0200 Subject: [PATCH 03/12] the stratgey of the code may need to be editied --- ...largest_perfect_square_less_than_number.py | 58 +++++++++++++++++++ ...largest_perfect_square_less_than_number.py | 56 +++++++++++++++++- 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index 83f922c1b..7fe69fc0d 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -12,3 +12,61 @@ Created on 2025-01-06 @author: Huda Alamassi """ + +from typing import Union + + +def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: + """ + The function takes in a number (either int or float) and returns the largest perfect square + that is less than the given number. + + Arguments: + number (int, float): The input number to check for the largest perfect square less than it. + + Assumptions: + - The input number must be a non-negative number (either integer or float). + + Returns: + int: + - The largest perfect square that is less than the input number. + - If no perfect square exists (i.e., for inputs <= 1), the function will return 0. + - If the number is 0, it will return 0. + + + Raises: + AssertionError: If the input is a negative number (integer or float). + AssertionError: If the input is not a number (neither integer nor float). + + Examples: + >>> largest_perfect_square_less_than(50) + 49 + + >>> largest_perfect_square_less_than(16.5) + 9 + + >>> largest_perfect_square_less_than(1) + 0 + + >>> largest_perfect_square_less_than(100.99) + 81 + """ + # Validate input type + assert isinstance( + number, (int, float) + ), "Input must be a number (either integer or float)." + + # Validate non-negative input + assert number >= 0, "Input must be a non-negative number." + + # Ensuring The input is integer + number = int(number) + + # If the input is between 0 and 1, no perfect square exists below it + if number <= 1: + return 0 + + # Find the largest perfect square less than the number + for current_integer in range(1, number + 1): + if current_integer * current_integer >= number: + return (current_integer - 1) * (current_integer - 1) diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py index c1c69376a..0dad69f25 100644 --- a/solutions/tests/test_largest_perfect_square_less_than_number.py +++ b/solutions/tests/test_largest_perfect_square_less_than_number.py @@ -14,11 +14,10 @@ the largest perfect square less than a number. - Edge tests: - - Small positive numbers + - Small positive numbers (the input is less than or equal to 1) - Perfect squares - Very large numbers - Zero - - Non-integer values - Defensive tests: ensuring the function handles invalid inputs such as: - Negative numbers @@ -28,3 +27,56 @@ Created on 2025-01-06 @author: Huda Alamassi """ + +import unittest + +from solutions.largest_perfect_square_less_than_number import ( + largest_perfect_square_less_than_number, +) + + +class TestLargestPerfectSquareLessThanNumber(unittest.TestCase): + """Test the largest_perfect_square_less_than_number function.""" + + # Test functionality + def test_integer_input(self): + """Test that the function returns the largest perfect square less than the input integer.""" + actual = largest_perfect_square_less_than_number(50) + self.assertEqual(actual, 49) + + def test_float_input(self): + """Test that the function works with floats and correctly truncates them.""" + actual = largest_perfect_square_less_than_number(2.6) + self.assertEqual(actual, 1) + + # Test edge cases + def test_small_positive_number(self): + """It should return 0 if you pass small positive numbers (less than or equal to 1).""" + actual = largest_perfect_square_less_than_number(0.5) + self.assertEqual(actual, 0) + + def test_perfect_square(self): + """Test when the input is a perfect square.""" + actual = largest_perfect_square_less_than_number(16) + self.assertEqual(actual, 9) + + def test_very_large_number(self): + """Test with a very large number.""" + actual = largest_perfect_square_less_than_number(10**6) + self.assertEqual(actual, 998001) + + def test_zero(self): + """It should return 0 if you pass 0""" + actual = largest_perfect_square_less_than_number(0) + self.assertEqual(actual, 0) + + # Test defensive assertions + def test_defensive_assertion_for_negative_input(self): + """Test that an assertion is raised if the input is a negative number.""" + with self.assertRaises(AssertionError): + largest_perfect_square_less_than_number(-10) + + def test_defensive_assertion_for_non_numeric_input(self): + """Test that an assertion is raised if the input is not a number.""" + with self.assertRaises(AssertionError): + largest_perfect_square_less_than_number("abc") From 92d604db63d47bb7d164f7827b38d55adeb5b2b9 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Mon, 6 Jan 2025 18:05:41 +0200 Subject: [PATCH 04/12] removing iterative from strategy to make the code simpler --- .../largest_perfect_square_less_than_number.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index 7fe69fc0d..8121dd2e2 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -59,14 +59,19 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: # Validate non-negative input assert number >= 0, "Input must be a non-negative number." - # Ensuring The input is integer - number = int(number) - # If the input is between 0 and 1, no perfect square exists below it if number <= 1: return 0 + square_root = number**0.5 + + # Ensuring the square root is integer + square_root = int(square_root) + # Find the largest perfect square less than the number - for current_integer in range(1, number + 1): - if current_integer * current_integer >= number: - return (current_integer - 1) * (current_integer - 1) + perfect_square = square_root * square_root + + if perfect_square == number: + perfect_square = (square_root - 1) * (square_root - 1) + + return perfect_square From b1ddd1553c9a2cb5e1a11a791ddc2aed21da7908 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Mon, 6 Jan 2025 18:59:17 +0200 Subject: [PATCH 05/12] checking the solution behavior --- .../largest_perfect_square_less_than_number.py | 15 ++++++++------- ...est_largest_perfect_square_less_than_number.py | 7 ++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index 8121dd2e2..5fff29854 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -30,8 +30,9 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: Returns: int: - The largest perfect square that is less than the input number. - - If no perfect square exists (i.e., for inputs <= 1), the function will return 0. - - If the number is 0, it will return 0. + - If no perfect square exists (i.e., for inputs less than or equal to 1 or equal to 0), + the function will return 0. + Raises: @@ -39,16 +40,16 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: AssertionError: If the input is not a number (neither integer nor float). Examples: - >>> largest_perfect_square_less_than(50) + >>> largest_perfect_square_less_than_number(50) 49 - >>> largest_perfect_square_less_than(16.5) + >>> largest_perfect_square_less_than_number(16.5) 9 - >>> largest_perfect_square_less_than(1) + >>> largest_perfect_square_less_than_number(1) 0 - >>> largest_perfect_square_less_than(100.99) + >>> largest_perfect_square_less_than_number(100.99) 81 """ # Validate input type @@ -71,7 +72,7 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: # Find the largest perfect square less than the number perfect_square = square_root * square_root - if perfect_square == number: + if perfect_square == int(number): perfect_square = (square_root - 1) * (square_root - 1) return perfect_square diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py index 0dad69f25..35d8eea20 100644 --- a/solutions/tests/test_largest_perfect_square_less_than_number.py +++ b/solutions/tests/test_largest_perfect_square_less_than_number.py @@ -45,10 +45,15 @@ def test_integer_input(self): self.assertEqual(actual, 49) def test_float_input(self): - """Test that the function works with floats and correctly truncates them.""" + """Test that the function returns the largest perfect square less than the input float.""" actual = largest_perfect_square_less_than_number(2.6) self.assertEqual(actual, 1) + def test_perfect_square_condition(self): + """Test the condition when the input number is close to a perfect square.""" + actual = largest_perfect_square_less_than_number(16.5) + self.assertEqual(actual, 9) + # Test edge cases def test_small_positive_number(self): """It should return 0 if you pass small positive numbers (less than or equal to 1).""" From 48953d198ba3f8cc9d48bcc4f5222ee3d09f92d6 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Mon, 6 Jan 2025 21:30:08 +0200 Subject: [PATCH 06/12] checking the code strategy --- solutions/largest_perfect_square_less_than_number.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index 5fff29854..04c17dbc6 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -52,26 +52,27 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: >>> largest_perfect_square_less_than_number(100.99) 81 """ - # Validate input type + # Validate input type to avoid errors in mathematical operations assert isinstance( number, (int, float) ), "Input must be a number (either integer or float)." - # Validate non-negative input + # Negative numbers do not have valid perfect squares assert number >= 0, "Input must be a non-negative number." - # If the input is between 0 and 1, no perfect square exists below it + # Handle edge cases where no perfect square exists below 1 if number <= 1: return 0 square_root = number**0.5 - # Ensuring the square root is integer + # Ensure we work with the integer part of the square root square_root = int(square_root) # Find the largest perfect square less than the number perfect_square = square_root * square_root + # Adjust when the number is exactly or close to a perfect square if perfect_square == int(number): perfect_square = (square_root - 1) * (square_root - 1) From a47855e9f64307cc65089306890b0f0d26c0f528 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Fri, 10 Jan 2025 02:45:17 +0200 Subject: [PATCH 07/12] Some changes after review --- ...largest_perfect_square_less_than_number.py | 25 +++++++++++++------ ...largest_perfect_square_less_than_number.py | 15 +++++++---- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index 04c17dbc6..bbf7febc1 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -64,16 +64,27 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: if number <= 1: return 0 - square_root = number**0.5 + # square_root = number**0.5 - # Ensure we work with the integer part of the square root - square_root = int(square_root) + # Ensure we work with the integer part of the square root + # square_root = int(square_root) - # Find the largest perfect square less than the number - perfect_square = square_root * square_root + # Find the largest perfect square less than the number + # perfect_square = square_root * square_root - # Adjust when the number is exactly or close to a perfect square + # Adjust when the number is exactly or close to a perfect square + # if perfect_square == int(number): + # perfect_square = (square_root - 1) * (square_root - 1) + + # return perfect_square + + square_root = int(number**0.5) + + perfect_square = square_root**2 + + # Adjust if the square root squared is greater than or equal to the number if perfect_square == int(number): - perfect_square = (square_root - 1) * (square_root - 1) + square_root -= 1 + perfect_square = square_root**2 return perfect_square diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py index 35d8eea20..8155b2b22 100644 --- a/solutions/tests/test_largest_perfect_square_less_than_number.py +++ b/solutions/tests/test_largest_perfect_square_less_than_number.py @@ -49,11 +49,6 @@ def test_float_input(self): actual = largest_perfect_square_less_than_number(2.6) self.assertEqual(actual, 1) - def test_perfect_square_condition(self): - """Test the condition when the input number is close to a perfect square.""" - actual = largest_perfect_square_less_than_number(16.5) - self.assertEqual(actual, 9) - # Test edge cases def test_small_positive_number(self): """It should return 0 if you pass small positive numbers (less than or equal to 1).""" @@ -65,6 +60,16 @@ def test_perfect_square(self): actual = largest_perfect_square_less_than_number(16) self.assertEqual(actual, 9) + def test_slightly_above_perfect_square(self): + """Test with inputs slightly above perfect squares.""" + actual = largest_perfect_square_less_than_number(16.5) + self.assertEqual(actual, 9) + + def test_slightly_below_perfect_square(self): + """Test with inputs slightly below perfect squares.""" + actual = largest_perfect_square_less_than_number(15.9) + self.assertEqual(actual, 9) + def test_very_large_number(self): """Test with a very large number.""" actual = largest_perfect_square_less_than_number(10**6) From 84b1fe6e961b565161b5df1f932e056e4283b0bd Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Fri, 10 Jan 2025 02:51:46 +0200 Subject: [PATCH 08/12] fixing some errors --- .../largest_perfect_square_less_than_number.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index bbf7febc1..6ba91d499 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -64,25 +64,13 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: if number <= 1: return 0 - # square_root = number**0.5 - - # Ensure we work with the integer part of the square root - # square_root = int(square_root) - - # Find the largest perfect square less than the number - # perfect_square = square_root * square_root - - # Adjust when the number is exactly or close to a perfect square - # if perfect_square == int(number): - # perfect_square = (square_root - 1) * (square_root - 1) - - # return perfect_square - + # Ensure we work with the integer part of the square root square_root = int(number**0.5) + # Find the largest perfect square less than the number perfect_square = square_root**2 - # Adjust if the square root squared is greater than or equal to the number + # Adjust when the number is exactly or close to a perfect square if perfect_square == int(number): square_root -= 1 perfect_square = square_root**2 From 109a5fbd1910a26fe39048d4453d78b8ba08c923 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Fri, 10 Jan 2025 03:00:31 +0200 Subject: [PATCH 09/12] Fix formatting based on ruff checks --- solutions/tests/test_largest_perfect_square_less_than_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py index 8155b2b22..8ad6c3f61 100644 --- a/solutions/tests/test_largest_perfect_square_less_than_number.py +++ b/solutions/tests/test_largest_perfect_square_less_than_number.py @@ -15,7 +15,7 @@ - Edge tests: - Small positive numbers (the input is less than or equal to 1) - - Perfect squares + - Perfect squares and slightly below or above it - Very large numbers - Zero From 28d9661648a6a802cb40deed3fd4843e46a37980 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Fri, 10 Jan 2025 03:12:06 +0200 Subject: [PATCH 10/12] trying to fix errors --- solutions/largest_perfect_square_less_than_number.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solutions/largest_perfect_square_less_than_number.py b/solutions/largest_perfect_square_less_than_number.py index 6ba91d499..5526e0ce6 100644 --- a/solutions/largest_perfect_square_less_than_number.py +++ b/solutions/largest_perfect_square_less_than_number.py @@ -53,9 +53,9 @@ def largest_perfect_square_less_than_number(number: Union[int, float]) -> int: 81 """ # Validate input type to avoid errors in mathematical operations - assert isinstance( - number, (int, float) - ), "Input must be a number (either integer or float)." + assert isinstance(number, (int, float)), ( + "Input must be a number (either integer or float)." + ) # Negative numbers do not have valid perfect squares assert number >= 0, "Input must be a non-negative number." From 4be54c3f24366b06411081162bd8a6f10596b1fa Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Fri, 10 Jan 2025 03:26:41 +0200 Subject: [PATCH 11/12] fixing format error --- solutions/tests/test_largest_perfect_square_less_than_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py index 8ad6c3f61..61c793688 100644 --- a/solutions/tests/test_largest_perfect_square_less_than_number.py +++ b/solutions/tests/test_largest_perfect_square_less_than_number.py @@ -76,7 +76,7 @@ def test_very_large_number(self): self.assertEqual(actual, 998001) def test_zero(self): - """It should return 0 if you pass 0""" + """It should return 0 if you pass 0.""" actual = largest_perfect_square_less_than_number(0) self.assertEqual(actual, 0) From 1e63f8618917bce285d8ee72caaebc87075c6060 Mon Sep 17 00:00:00 2001 From: HUDA ALAMASSI Date: Fri, 10 Jan 2025 16:05:05 +0200 Subject: [PATCH 12/12] making the tests about behavior --- ...largest_perfect_square_less_than_number.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/solutions/tests/test_largest_perfect_square_less_than_number.py b/solutions/tests/test_largest_perfect_square_less_than_number.py index 61c793688..602bbf65a 100644 --- a/solutions/tests/test_largest_perfect_square_less_than_number.py +++ b/solutions/tests/test_largest_perfect_square_less_than_number.py @@ -40,53 +40,53 @@ class TestLargestPerfectSquareLessThanNumber(unittest.TestCase): # Test functionality def test_integer_input(self): - """Test that the function returns the largest perfect square less than the input integer.""" + """It should return the largest perfect square less than the given integer.""" actual = largest_perfect_square_less_than_number(50) self.assertEqual(actual, 49) def test_float_input(self): - """Test that the function returns the largest perfect square less than the input float.""" + """It should return the largest perfect square less than the given float.""" actual = largest_perfect_square_less_than_number(2.6) self.assertEqual(actual, 1) # Test edge cases def test_small_positive_number(self): - """It should return 0 if you pass small positive numbers (less than or equal to 1).""" + """It should return 0 when the input is less than or equal to 1.""" actual = largest_perfect_square_less_than_number(0.5) self.assertEqual(actual, 0) def test_perfect_square(self): - """Test when the input is a perfect square.""" + """It should return the largest perfect square less than the given perfect square.""" actual = largest_perfect_square_less_than_number(16) self.assertEqual(actual, 9) def test_slightly_above_perfect_square(self): - """Test with inputs slightly above perfect squares.""" + """It should return the largest square less than input slightly above a perfect square.""" actual = largest_perfect_square_less_than_number(16.5) self.assertEqual(actual, 9) def test_slightly_below_perfect_square(self): - """Test with inputs slightly below perfect squares.""" + """It should return the largest square less than input slightly below a perfect square.""" actual = largest_perfect_square_less_than_number(15.9) self.assertEqual(actual, 9) def test_very_large_number(self): - """Test with a very large number.""" + """It should return the largest perfect square less than a very large input.""" actual = largest_perfect_square_less_than_number(10**6) self.assertEqual(actual, 998001) def test_zero(self): - """It should return 0 if you pass 0.""" + """It should return 0 when the input is 0.""" actual = largest_perfect_square_less_than_number(0) self.assertEqual(actual, 0) # Test defensive assertions def test_defensive_assertion_for_negative_input(self): - """Test that an assertion is raised if the input is a negative number.""" + """It should raise an AssertionError when the input is negative.""" with self.assertRaises(AssertionError): largest_perfect_square_less_than_number(-10) def test_defensive_assertion_for_non_numeric_input(self): - """Test that an assertion is raised if the input is not a number.""" + """It should raise an AssertionError when the input is not a number.""" with self.assertRaises(AssertionError): largest_perfect_square_less_than_number("abc")