intutils
API Reference¶
¶
intutils is a basic Python integer manipulation library.
days_in_month(month_number, year_int=0)
¶
Returns the number of days in the given month.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
month_number | int | The month. | required |
year_int |
| The year to check (for leap years). | 0 |
Returns:
Type | Description |
---|---|
int | The day count. |
Source code in intutils/__init__.py
def days_in_month(month_number: int, year_int=0) -> int:
"""
Returns the number of days in the given month.
Arguments:
month_number: The month.
year_int: The year to check (for leap years).
Returns:
The day count.
"""
if month_number >= 13:
raise ValueError("There isn't a month with a number bigger then 12!!")
if is_even(month_number) and not month_number == 2:
# even month, not Febrary
return 30
if is_odd(month_number):
# odd month
return 31
if divisible_by_no_decimals(year_int, 4):
# leap year
return 29
# typical year
return 28
divisible_by_no_decimals(number, divisor)
¶
Checks if the number
can be divided by the divisor
without the resulting number having decimals in it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number | int | The base number. | required |
divisor | int | The number to divide the | required |
Returns:
Type | Description |
---|---|
bool | If it is possible. |
Source code in intutils/__init__.py
def divisible_by_no_decimals(number: int, divisor: int) -> bool:
"""
Checks if the `number` can be divided by the `divisor` without the
resulting number having decimals in it.
Arguments:
number: The base number.
divisor: The number to divide the `number` by.
Returns:
If it is possible.
"""
return (number % divisor) == 0
is_even(number)
¶
Returns if the number
is even.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number | int | The number to check. | required |
Returns:
Type | Description |
---|---|
bool | If the number is even. |
Source code in intutils/__init__.py
def is_even(number: int) -> bool:
"""
Returns if the `number` is even.
Arguments:
number: The number to check.
Returns:
If the number is even.
"""
return divisible_by_no_decimals(number, 2)
is_int(testable)
¶
Returns if the passed item is an integer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number |
| The item to check. | required |
Returns:
Type | Description |
---|---|
bool | If the object is an integer or a string that can be an integer. |
Source code in intutils/__init__.py
def is_int(testable: Any) -> bool:
"""
Returns if the passed item is an integer.
Arguments:
number: The item to check.
Returns:
If the object is an integer or a string that can be an integer.
"""
if type(testable) == int:
return True
elif type(testable) == str:
try:
int(testable)
return True
except ValueError:
return False
return False
is_odd(number)
¶
Returns if the number
is odd.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number | int | The number to check. | required |
Returns:
Type | Description |
---|---|
bool | If the number is odd. |
Source code in intutils/__init__.py
def is_odd(number: int) -> bool:
"""
Returns if the `number` is odd.
Arguments:
number: The number to check.
Returns:
If the number is odd.
"""
return not is_even(number)
sort_greatest_to_least(mylist)
¶
Sorts the passed list of integers from greatest to least.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mylist | List[int] | The list to sort. | required |
Returns:
Type | Description |
---|---|
List[int] | The sorted list. |
Source code in intutils/__init__.py
def sort_greatest_to_least(mylist: List[int]) -> List[int]:
"""
Sorts the passed list of integers from greatest to least.
Arguments:
mylist: The list to sort.
Returns:
The sorted list.
"""
return list(
reversed(
_Sorting().quick_sort(mylist, 0, len(mylist) - 1)
)
)