Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

challenge_26 solutions #31

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
35 changes: 35 additions & 0 deletions solutions/challenge_26/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Prime Number Checker

A prime number is a natural number greater than 1 that has no divisors
other than 1 and itself. This function checks if the input number meets
the criteria of being a prime number.

Args:
number (int): The number to be checked.

Returns:
str: A string indicating whether the number is prime or not.
Returns "is prime" if the number is prime, otherwise "not prime".

Algorithm:
1. If the number is less than or equal to 1, it is not a prime number.
2. Iterate from 2 to the square root of the number (inclusive).
- If the number is divisible by any of these values,
it is not a prime number.
3. If no divisors are found, the number is a prime number.

Example Usage:
>>> prime_numbers(9)
'not prime'
>>> prime_numbers(7)
'is prime'
>>> prime_numbers(1)
'not prime'
>>> prime_numbers(13)
'is prime'

Note:
- This function uses a simple algorithm to check for primality by
testing divisors up to the square root of the input number for efficiency.
- The result is returned as a string, which may be useful for simple
display or testing
Empty file.
53 changes: 53 additions & 0 deletions solutions/challenge_26/prime_numbers.py
AnaiMurillo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tuesday 31 17:00:00 2024

@author: Cliforde Exael
"""


def prime_numbers(number: int) -> str:
"""
Determines whether a given number is a prime number.

A prime number is a natural number greater than 1 that has no divisors other than 1 and itself.
This function checks if the input number meets the criteria of being a prime number.

Args:
number (int): The number to be checked.

Returns:
str: A string indicating whether the number is prime or not.
Returns "is prime" if the number is prime, otherwise "not prime".

Algorithm:
1. If the number is less than or equal to 1, it is not a prime number.
2. Iterate from 2 to the square root of the number (inclusive).
- If the number is divisible by any of these values, it is not a prime number.
3. If no divisors are found, the number is a prime number.

Example Usage:
>>> prime_numbers(9)
'not prime'
>>> prime_numbers(7)
'is prime'
>>> prime_numbers(1)
'not prime'
>>> prime_numbers(13)
'is prime'

Note:
- This function uses a simple algorithm to check for primality by testing divisors
up to the square root of the input number for efficiency.
- The result is returned as a string, which may be useful for simple display or testing.
"""
if number <= 1:
return "not prime"
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return "not prime"
return "is prime"



Empty file.
136 changes: 136 additions & 0 deletions solutions/tests/challenge_26/test_prime_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# _*_coding:utf-8_*_
"""
Created on Tuesday 31 17:00:00 2024


@author: Cliforde Exael
"""

import unittest

from solutions.challenge_26.prime_numbers import prime_numbers


class TestPrimeNumbers(unittest.TestCase):
"""
Unit tests for the prime_numbers function, which checks if a number is a prime number.
These tests cover typical cases, boundary cases, and defensive assertions.
"""

def test_prime_number(self):
"""
Test that the function correctly identifies a prime number.
"""
number = 7
expected = "is prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_non_prime_number(self):
"""
Test that the function correctly identifies a non-prime number.
"""
number = 9
expected = "not prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_one_is_not_prime(self):
"""
Test that the function correctly identifies that 1 is not a prime number.
"""
number = 1
expected = "not prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_zero_is_not_prime(self):
"""
Test that the function correctly identifies that 0 is not a prime number.
"""
number = 0
expected = "not prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_negative_number_is_not_prime(self):
"""
Test that the function correctly identifies that negative numbers are not prime.
"""
number = -5
expected = "not prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_large_prime_number(self):
"""
Test that the function correctly identifies a large prime number.
"""
number = 29
expected = "is prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_large_non_prime_number(self):
"""
Test that the function correctly identifies a large non-prime number.
"""
number = 100
expected = "not prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_smallest_prime_number(self):
"""
Test that the function correctly identifies the smallest prime number (2).
"""
number = 2
expected = "is prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_defensive_type_check_string_input(self):
"""
Test that the function raises a TypeError when given a string input.
"""
number = "text"
with self.assertRaises(TypeError, msg=f"Expected TypeError for input {number}"):
prime_numbers(number)

def test_boundary_case_next_to_prime(self):
"""
Test that the function correctly identifies a number next to a prime number.
"""
number = 4
expected = "not prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)

def test_boundary_case_very_large_number(self):
"""
Test that the function correctly identifies a very large non-prime number.
"""
number = 10**6
expected = "not prime"
actual = prime_numbers(number)
self.assertEqual(
actual, expected, f"Expected {expected} but got {actual} for input {number}"
)
Loading