From 64747451438b42ce91702609a6346c28953c05c3 Mon Sep 17 00:00:00 2001 From: Richa Jaishwal Date: Wed, 9 Oct 2024 09:59:50 +0530 Subject: [PATCH 1/2] added numberCheckingfiles --- pysnippets/SpecialNumbers/Armstrong.py | 16 ++++++++++++++++ pysnippets/SpecialNumbers/Automorphic.py | 13 +++++++++++++ pysnippets/SpecialNumbers/Neon.py | 14 ++++++++++++++ pysnippets/SpecialNumbers/Palindrome.py | 15 +++++++++++++++ pysnippets/SpecialNumbers/PerfectNumber.py | 17 +++++++++++++++++ pysnippets/SpecialNumbers/Strong.py | 21 +++++++++++++++++++++ pysnippets/SpecialNumbers/_init_.py | 4 ++++ 7 files changed, 100 insertions(+) create mode 100644 pysnippets/SpecialNumbers/Armstrong.py create mode 100644 pysnippets/SpecialNumbers/Automorphic.py create mode 100644 pysnippets/SpecialNumbers/Neon.py create mode 100644 pysnippets/SpecialNumbers/Palindrome.py create mode 100644 pysnippets/SpecialNumbers/PerfectNumber.py create mode 100644 pysnippets/SpecialNumbers/Strong.py create mode 100644 pysnippets/SpecialNumbers/_init_.py diff --git a/pysnippets/SpecialNumbers/Armstrong.py b/pysnippets/SpecialNumbers/Armstrong.py new file mode 100644 index 00000000..97baf498 --- /dev/null +++ b/pysnippets/SpecialNumbers/Armstrong.py @@ -0,0 +1,16 @@ +def is_armstrong(number): + num_str = str(number) + num_digits = len(num_str) + + armstrong_sum = sum(int(digit) ** num_digits for digit in num_str) + if (armstrong_sum == number): + print(f"{number} is an Armstrong number.") + else: + print(f"{number} is not an Armstrong number.") + + +if __name__ == "__main__": + # Input from user + is_armstrong(153) + is_armstrong(13) + diff --git a/pysnippets/SpecialNumbers/Automorphic.py b/pysnippets/SpecialNumbers/Automorphic.py new file mode 100644 index 00000000..fa305ead --- /dev/null +++ b/pysnippets/SpecialNumbers/Automorphic.py @@ -0,0 +1,13 @@ +def is_automorphic(number): + square = number ** 2 + if(str(square).endswith(str(number))): + print(f"{number} is an Automorphic number.") + else: + print(f"{number} is not an Automorphic number.") + + + + +if __name__ == "__main__": + is_automorphic(25) + is_automorphic(16) \ No newline at end of file diff --git a/pysnippets/SpecialNumbers/Neon.py b/pysnippets/SpecialNumbers/Neon.py new file mode 100644 index 00000000..5a106425 --- /dev/null +++ b/pysnippets/SpecialNumbers/Neon.py @@ -0,0 +1,14 @@ +def is_neon(number): + square = number ** 2 + digit_sum = sum(int(digit) for digit in str(square)) + if(digit_sum == number): + print(f"{number} is a Neon number.") + else: + print(f"{number} is not a Neon number.") + + + +if __name__ == "__main__": + is_neon(9) + is_neon(20) + \ No newline at end of file diff --git a/pysnippets/SpecialNumbers/Palindrome.py b/pysnippets/SpecialNumbers/Palindrome.py new file mode 100644 index 00000000..409b9089 --- /dev/null +++ b/pysnippets/SpecialNumbers/Palindrome.py @@ -0,0 +1,15 @@ +def is_palindrome(number): + # Convert the number to a string to easily reverse it + str_num = str(number) + + # Check if the original number is the same as its reverse + if str_num == str_num[::-1]: + print(f"{number} is a palindrome.") + else: + print(f"{number} is not a palindrome.") + + +# Example usage +if __name__ == "__main__": + is_palindrome(101) + is_palindrome(10) \ No newline at end of file diff --git a/pysnippets/SpecialNumbers/PerfectNumber.py b/pysnippets/SpecialNumbers/PerfectNumber.py new file mode 100644 index 00000000..681b7819 --- /dev/null +++ b/pysnippets/SpecialNumbers/PerfectNumber.py @@ -0,0 +1,17 @@ +def is_perfect(number): + # Find all divisors of the number (excluding the number itself) + divisors_sum = sum(i for i in range(1, number) if number % i == 0) + + # Check if the sum of divisors equals the original number + if (divisors_sum == number): + print(f"{number} is a Perfect number.") + else: + print(f"{number} is not a Perfect number.") + + + +if __name__ == "__main__": + # Input from user + is_perfect(6) + is_perfect(20) + \ No newline at end of file diff --git a/pysnippets/SpecialNumbers/Strong.py b/pysnippets/SpecialNumbers/Strong.py new file mode 100644 index 00000000..4236fdcc --- /dev/null +++ b/pysnippets/SpecialNumbers/Strong.py @@ -0,0 +1,21 @@ +import math + +def is_strong(number): + num_str = str(number) + + # Calculate the sum of factorials of each digit + strong_sum = sum(math.factorial(int(digit)) for digit in num_str) + + # Check if the sum equals the original number + if (strong_sum == number): + print(f"{number} is a Strong number.") + else: + print(f"{number} is not a Strong number.") + + +if __name__ == "__main__": + # Input from user + is_strong(145) + is_strong(134) + + diff --git a/pysnippets/SpecialNumbers/_init_.py b/pysnippets/SpecialNumbers/_init_.py new file mode 100644 index 00000000..c1125c68 --- /dev/null +++ b/pysnippets/SpecialNumbers/_init_.py @@ -0,0 +1,4 @@ +# __init__.py + +# This file allows the snippets folder to be treated as a package. +# It can be empty or used to expose certain functions for easy imports. From d480f865cec6225029ba3e91e6bf445143c4bb49 Mon Sep 17 00:00:00 2001 From: Richa Jaishwal Date: Sat, 12 Oct 2024 11:39:22 +0530 Subject: [PATCH 2/2] Added the test_cases files --- Tests/SpecialNumbers/Test_Armstrong.py | 27 ++++++++++++++++ Tests/SpecialNumbers/Test_Automorphic.py | 24 ++++++++++++++ Tests/SpecialNumbers/Test_Neon.py | 40 ++++++++++++++++++++++++ Tests/SpecialNumbers/Test_Palindrome.py | 35 +++++++++++++++++++++ Tests/SpecialNumbers/Test_Perfect.py | 35 +++++++++++++++++++++ Tests/SpecialNumbers/Test_strong.py | 35 +++++++++++++++++++++ 6 files changed, 196 insertions(+) create mode 100644 Tests/SpecialNumbers/Test_Armstrong.py create mode 100644 Tests/SpecialNumbers/Test_Automorphic.py create mode 100644 Tests/SpecialNumbers/Test_Neon.py create mode 100644 Tests/SpecialNumbers/Test_Palindrome.py create mode 100644 Tests/SpecialNumbers/Test_Perfect.py create mode 100644 Tests/SpecialNumbers/Test_strong.py diff --git a/Tests/SpecialNumbers/Test_Armstrong.py b/Tests/SpecialNumbers/Test_Armstrong.py new file mode 100644 index 00000000..680b7ccd --- /dev/null +++ b/Tests/SpecialNumbers/Test_Armstrong.py @@ -0,0 +1,27 @@ +import unittest +from Armstrong import is_armstrong + +class TestIsArmstrong(unittest.TestCase): + + def test_case_armstrong_number(self): + # Test with an Armstrong number (153) + self.assertEqual(is_armstrong(153), "153 is an Armstrong number.") + + def test_case_non_armstrong_number(self): + # Test with a non-Armstrong number (13) + self.assertEqual(is_armstrong(13), "13 is not an Armstrong number.") + + def test_case_single_digit(self): + # Test with a single digit number (all single digits are Armstrong numbers) + self.assertEqual(is_armstrong(5), "5 is an Armstrong number.") + + def test_case_large_armstrong_number(self): + # Test with a large Armstrong number (9474) + self.assertEqual(is_armstrong(9474), "9474 is an Armstrong number.") + + def test_case_large_non_armstrong_number(self): + # Test with a large non-Armstrong number (9475) + self.assertEqual(is_armstrong(9475), "9475 is not an Armstrong number.") + +if __name__ == "__main__": + unittest.main() diff --git a/Tests/SpecialNumbers/Test_Automorphic.py b/Tests/SpecialNumbers/Test_Automorphic.py new file mode 100644 index 00000000..f253d8f6 --- /dev/null +++ b/Tests/SpecialNumbers/Test_Automorphic.py @@ -0,0 +1,24 @@ +import unittest +from Automorphic import is_automorphic + +class TestIsAutomorphic(unittest.TestCase): + + def test_case_automorphic_number(self): + self.assertEqual(is_automorphic(25), "25 is an Automorphic number.") + self.assertEqual(is_automorphic(5), "5 is an Automorphic number.") + + def test_case_non_automorphic_number(self): + self.assertEqual(is_automorphic(16), "16 is not an Automorphic number.") + self.assertEqual(is_automorphic(7), "7 is not an Automorphic number.") + + def test_case_single_digit_automorphic(self): + self.assertEqual(is_automorphic(6), "6 is an Automorphic number.") # 6^2 = 36 + + def test_case_large_automorphic_number(self): + self.assertEqual(is_automorphic(376), "376 is an Automorphic number.") # 376^2 = 141376 + + def test_case_large_non_automorphic_number(self): + self.assertEqual(is_automorphic(123), "123 is not an Automorphic number.") + +if __name__ == "__main__": + unittest.main() diff --git a/Tests/SpecialNumbers/Test_Neon.py b/Tests/SpecialNumbers/Test_Neon.py new file mode 100644 index 00000000..0a7045d2 --- /dev/null +++ b/Tests/SpecialNumbers/Test_Neon.py @@ -0,0 +1,40 @@ +import unittest +from Neon import is_neon +from io import StringIO +import sys + +class TestIsNeon(unittest.TestCase): + + def setUp(self): + # Redirect stdout to capture print statements + self.held, sys.stdout = sys.stdout, StringIO() + + def tearDown(self): + # Restore original stdout + sys.stdout = self.held + + def test_case_neon_number(self): + is_neon(9) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "9 is a Neon number.") + + is_neon(1) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "1 is a Neon number.") + + def test_case_non_neon_number(self): + is_neon(20) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "20 is not a Neon number.") + + is_neon(5) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "5 is not a Neon number.") + + def test_case_large_non_neon_number(self): + is_neon(100) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "100 is not a Neon number.") + +if __name__ == "__main__": + unittest.main() diff --git a/Tests/SpecialNumbers/Test_Palindrome.py b/Tests/SpecialNumbers/Test_Palindrome.py new file mode 100644 index 00000000..25b8c242 --- /dev/null +++ b/Tests/SpecialNumbers/Test_Palindrome.py @@ -0,0 +1,35 @@ +import unittest +from Palindrome import is_palindrome +from io import StringIO +import sys + +class TestIsPalindrome(unittest.TestCase): + + def setUp(self): + # Redirect stdout to capture print statements + self.held, sys.stdout = sys.stdout, StringIO() + + def tearDown(self): + # Restore original stdout + sys.stdout = self.held + + def test_case_palindrome_number(self): + is_palindrome(101) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "101 is a palindrome.") + + is_palindrome(12321) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "12321 is a palindrome.") + + def test_case_non_palindrome_number(self): + is_palindrome(10) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "10 is not a palindrome.") + + is_palindrome(123) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "123 is not a palindrome.") + +if __name__ == "__main__": + unittest.main() diff --git a/Tests/SpecialNumbers/Test_Perfect.py b/Tests/SpecialNumbers/Test_Perfect.py new file mode 100644 index 00000000..a6fb017e --- /dev/null +++ b/Tests/SpecialNumbers/Test_Perfect.py @@ -0,0 +1,35 @@ +import unittest +from PerfectNumber import is_perfect +from io import StringIO +import sys + +class TestIsPerfect(unittest.TestCase): + + def setUp(self): + # Redirect stdout to capture print statements + self.held, sys.stdout = sys.stdout, StringIO() + + def tearDown(self): + # Restore original stdout + sys.stdout = self.held + + def test_case_perfect_number(self): + is_perfect(6) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "6 is a Perfect number.") + + is_perfect(28) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "28 is a Perfect number.") + + def test_case_non_perfect_number(self): + is_perfect(20) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "20 is not a Perfect number.") + + is_perfect(12) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "12 is not a Perfect number.") + +if __name__ == "__main__": + unittest.main() diff --git a/Tests/SpecialNumbers/Test_strong.py b/Tests/SpecialNumbers/Test_strong.py new file mode 100644 index 00000000..607dc074 --- /dev/null +++ b/Tests/SpecialNumbers/Test_strong.py @@ -0,0 +1,35 @@ +import unittest +from Strong import is_strong +from io import StringIO +import sys + +class TestIsStrong(unittest.TestCase): + + def setUp(self): + # Redirect stdout to capture print statements + self.held, sys.stdout = sys.stdout, StringIO() + + def tearDown(self): + # Restore original stdout + sys.stdout = self.held + + def test_case_strong_number(self): + is_strong(145) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "145 is a Strong number.") + + is_strong(1) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "1 is a Strong number.") + + def test_case_non_strong_number(self): + is_strong(134) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "134 is not a Strong number.") + + is_strong(10) + output = sys.stdout.getvalue().strip() + self.assertEqual(output, "10 is not a Strong number.") + +if __name__ == "__main__": + unittest.main()