pip install -U funcboxfrom funcbox import *
is_prime(17) # True
classify_numbers([2, 3, 4, 5, 6]) # {'primes': [2, 3, 5], 'composites': [4, 6], 'neither': []}
fibonacci(10) # 55
get_factors(12) # [1, 2, 3, 4, 6]| Function | Description |
|---|---|
| is_prime | Determines whether a given integer is prime |
| classify_numbers | Categorizes integers into prime, composite, and neutral subsets |
| fibonacci | Computes the |
| get_factors | Computes all proper divisors of an integer |
| isAnagram | Computes if two strings are anagrams of each other optimally |
| binary_search | Searches for a value in a sorted sequence |
| dijkstra | Calculates shortest paths in a graph using Dijkstra's algorithm |
| primes | Generates primes within a range via the Sieve of Eratosthenes |
is_prime(n)Determines whether a given integer is prime.
Signature
is_prime(n: int) -> boolParameters
n(int): The integer to evaluate. Must be a plain integer (not aboolorfloat).
Raises
TypeError: Raised ifnis not a plain integer.
Returns
bool:Trueif the integer is prime,Falseotherwise.
Examples
from funcbox import is_prime
print(is_prime(7)) # True
print(is_prime(10)) # Falseclassify_numbers(numbers)Categorizes a sequence of integers into prime, composite, and neutral sets (0, 1, or negative numbers).
Signature
classify_numbers(numbers: list[int]) -> dict[str, list[int]]Parameters
numbers(list[int]): A list of integers to categorize. All elements must be plain integers.
Raises
TypeError: Raised ifnumbersis not a list, or if any element is not a plain integer.
Returns
dict: A dictionary containing three lists:'primes': Integers that are prime.'composites': Integers that are composite (greater than 1 and not prime).'neither': Integers that are neither prime nor composite (< 2).
Examples
from funcbox import classify_numbers
print(classify_numbers([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
# {'primes': [2, 3, 5, 7], 'composites': [4, 6, 8, 9], 'neither': [0, 1]}
print(classify_numbers([-5, 0, 1, 13, 15]))
# {'primes': [13], 'composites': [15], 'neither': [-5, 0, 1]}fibonacci(n, output_type="int")Computes Fibonacci sequence values. Supports retrieving an individual
Signature
fibonacci(n: int, output_type: str = "int") -> int | list[int]Parameters
-
n(int): The sequence index (0-indexed) or the total count of elements to generate. Must be a plain integer. -
output_type(str): Specification for the return format.-
"int"(default): Returns a single integer corresponding to the$n$ -th term. -
"list": Returns a list consisting of the firstnterms.
-
Returns
intifoutput_typeis"int".list[int]ifoutput_typeis"list".
Raises
TypeError: Raised ifnis not a plain integer oroutput_typeis not a string.ValueError: Raised ifnis a negative integer or if an unsupportedoutput_typeis provided.
Examples
from funcbox import fibonacci
print(fibonacci(0)) # 0
print(fibonacci(5)) # 5
print(fibonacci(5, output_type="list")) # [0, 1, 1, 2, 3]get_factors(num)Computes all proper divisors (factors) of an integer, excluding the number itself.
Signature
get_factors(num: int) -> list[int]Parameters
num(int): The target integer to compute factors for. Must be a plain integer (not aboolorfloat).
Raises
TypeError: Raised ifnumis not a plain integer.
Returns
list[int]: A sorted list of all proper factors.
Examples
from funcbox import get_factors
print(get_factors(12)) # [1, 2, 3, 4, 6]
print(get_factors(7)) # [1]isAnagram(str1, str2)Checks if two strings are anagrams of each other.
Signature
isAnagram(str1: str, str2: str) -> boolParameters
str1(str): First string to compare.str2(str): Second string to compare.
Raises
TypeError: Raised ifstr1orstr2is not a string.
Returns
bool:Trueif the strings are anagrams,Falseotherwise.
Examples
from funcbox import isAnagram
print(isAnagram("listen", "silent")) # True
print(isAnagram("hello", "world")) # Falsebinary_search(arr, target)Searches for a target value in a sorted sequence.
Signature
binary_search(arr: Sequence, target: Any) -> intParameters
arr(Sequence): A sorted sequence to search through (e.g.list,tuple,range).target(Any): The value to search for.
Returns
int: The index of the target if found,-1otherwise.
Raises
TypeError: Raised ifarris not aSequence.
Examples
from funcbox import binary_search
print(binary_search([1, 3, 5, 7, 9], 7)) # 3
print(binary_search([1, 3, 5, 7, 9], 4)) # -1dijkstra(graph, start_node, end_node=None)Calculates the shortest paths from a source node to all other reachable nodes in a weighted graph using Dijkstra's algorithm.
Signature
from typing import Any
dijkstra(graph: dict, start_node: Any, end_node: Any = None) -> dictParameters
graph(dict): An adjacency list where each node maps to adictof{neighbor: weight}pairs. All weights must be non-negative numbers and all neighbor keys must be nodes in the graph.start_node: The origin node for pathfinding computation.end_node: Optional terminal node. If provided, the algorithm terminates early once the shortest path to this node is found.
Raises
ValueError: Raised ifgraphis not adict, any node's adjacency value is not adict, any neighbor key is not present in the graph, any edge weight is not a number or is negative,start_nodeis not in the graph, orend_nodeis specified but not in the graph.
Returns
dict: A resultant dictionary comprised of two objects:'distances': The calculated minimum distances from thestart_nodeto all resolved nodes. Unreachable nodes evaluate to positive infinity (float('inf')).'paths': Ordered sequences of nodes representing the shortest path from thestart_node. Unreachable nodes map toNone.
Examples
from funcbox import dijkstra
graph = {
'A': {'B': 4, 'C': 2},
'B': {'D': 5, 'E': 1},
'C': {'B': 1, 'E': 3},
'D': {'F': 2},
'E': {'D': 1, 'F': 4},
'F': {}
}
result = dijkstra(graph, 'A')
print(result['distances'])
print(result['paths'])
result = dijkstra(graph, 'A', 'F')
print(result['distances']['F'])
print(result['paths']['F'])primes(start=2, limit)Generates a sequence of prime numbers within a specified bounds utilizing the Sieve of Eratosthenes algorithm.
Signature
primes(start: int = 2, limit: int) -> list[int]Parameters
start(int): The inclusive lower bound for prime generation. Defaults to 2.limit(int): The inclusive upper bound for prime generation.
Returns
list[int]: An ordered list of prime numbers fromstartboundary up to the specifiedlimit.
Raises
TypeError: Raised ifstartorlimitis not a plain integer.ValueError: Raised if eitherlimitorstartare evaluated to be less than 2.
Examples
from funcbox import primes
print(primes(limit=10)) # [2, 3, 5, 7]
print(primes(start=10, limit=20)) # [11, 13, 17, 19]FuncBox provides utility functions for general use. The developer is not responsible for any issues caused by improper use or abuse of the library.
Contributions are welcome! Feel free to fork the repository and submit a pull request with your improvements.
This project is licensed under the MIT License. See the LICENSE file for more details.