Math and string utilities built from first principles - no dependencies
Project description
omath
A Python library built from first principles for educational purposes.
Philosophy
This library intentionally avoids:
- External dependencies
- Standard library helpers (
str.lower(),str.split(),re,collections,itertools) - Built-in shortcuts
Everything is implemented using only:
- Loops and conditionals
- Lists and dicts
- Strings
ord()andchr()
Why? To understand how things work at the lowest level. Every function in this library could be a teaching moment.
Installation
From PyPI
pip install omath
From GitHub
pip install git+https://github.com/OmMishra16/omath.git
From Source
git clone https://github.com/OmMishra16/omath.git
cd omath
pip install -e .
Quick Start
import omath
# Arithmetic
result = omath.multiply(7, 8) # 56 (uses repeated addition internally)
# Geometry
area = omath.circle_area(5) # 78.53975
# Strings
lower = omath.to_lowercase("HELLO") # "hello" (uses ord/chr, not str.lower)
API Reference
Arithmetic Module
All arithmetic operations are implemented from first principles:
multiply()uses repeated additiondivide()uses repeated subtractionpower()uses repeated multiplication
add(a, b)
Returns the sum of two numbers.
>>> omath.add(10, 5)
15
>>> omath.add(-3, 7)
4
subtract(a, b)
Returns the difference of two numbers.
>>> omath.subtract(10, 3)
7
>>> omath.subtract(5, 8)
-3
multiply(a, b)
Returns the product of two numbers using repeated addition.
>>> omath.multiply(7, 8)
56
>>> omath.multiply(-4, 5)
-20
>>> omath.multiply(0, 100)
0
How it works internally:
multiply(3, 4) = 3 + 3 + 3 + 3 = 12
divide(a, b)
Returns the integer quotient of a / b using repeated subtraction.
>>> omath.divide(20, 4)
5
>>> omath.divide(17, 5)
3
>>> omath.divide(100, 7)
14
>>> omath.divide(10, 0)
None # Division by zero returns None
How it works internally:
divide(17, 5):
17 - 5 = 12 (count: 1)
12 - 5 = 7 (count: 2)
7 - 5 = 2 (count: 3)
2 < 5, stop
Result: 3
modulo(a, b)
Returns the remainder of a / b.
>>> omath.modulo(17, 5)
2
>>> omath.modulo(100, 7)
2
>>> omath.modulo(10, 0)
None # Modulo by zero returns None
power(base, exponent)
Returns base raised to exponent (non-negative integers only).
>>> omath.power(2, 10)
1024
>>> omath.power(5, 3)
125
>>> omath.power(7, 0)
1
>>> omath.power(2, -1)
None # Negative exponents not supported
How it works internally:
power(2, 4) = multiply(multiply(multiply(1, 2), 2), 2), 2)
= 1 * 2 * 2 * 2 * 2 = 16
absolute(n)
Returns the absolute value of n.
>>> omath.absolute(-42)
42
>>> omath.absolute(42)
42
>>> omath.absolute(0)
0
PI
A constant approximation of pi (3.14159).
>>> omath.PI
3.14159
Geometry Module
Functions for calculating areas, perimeters, and volumes.
circle_area(radius)
Returns the area of a circle (PI * r^2).
>>> omath.circle_area(5)
78.53975
>>> omath.circle_area(1)
3.14159
>>> omath.circle_area(10)
314.159
circle_circumference(radius)
Returns the circumference of a circle (2 * PI * r).
>>> omath.circle_circumference(5)
31.4159
>>> omath.circle_circumference(1)
6.28318
rectangle_area(width, height)
Returns the area of a rectangle.
>>> omath.rectangle_area(4, 5)
20
>>> omath.rectangle_area(10, 10)
100
rectangle_perimeter(width, height)
Returns the perimeter of a rectangle (2 * (width + height)).
>>> omath.rectangle_perimeter(4, 5)
18
>>> omath.rectangle_perimeter(10, 10)
40
triangle_area(base, height)
Returns the area of a triangle (base * height / 2).
>>> omath.triangle_area(10, 5)
25
>>> omath.triangle_area(8, 6)
24
square_area(side)
Returns the area of a square (side^2).
>>> omath.square_area(5)
25
>>> omath.square_area(12)
144
cube_volume(side)
Returns the volume of a cube (side^3).
>>> omath.cube_volume(3)
27
>>> omath.cube_volume(5)
125
Strings Module
All string operations are implemented using ord() and chr() — no built-in string methods like str.lower() or str.split().
to_lowercase(s)
Converts a string to lowercase using ASCII math.
>>> omath.to_lowercase("HELLO WORLD")
'hello world'
>>> omath.to_lowercase("PyThOn")
'python'
>>> omath.to_lowercase("123 ABC")
'123 abc'
How it works internally:
'A' = chr(65), 'a' = chr(97)
Difference = 32
So: chr(ord('A') + 32) = 'a'
to_uppercase(s)
Converts a string to uppercase using ASCII math.
>>> omath.to_uppercase("hello world")
'HELLO WORLD'
>>> omath.to_uppercase("PyThOn")
'PYTHON'
split_by_char(s, delimiter)
Splits a string by a delimiter character.
>>> omath.split_by_char("a,b,c,d", ",")
['a', 'b', 'c', 'd']
>>> omath.split_by_char("hello world", " ")
['hello', 'world']
>>> omath.split_by_char("one-two-three", "-")
['one', 'two', 'three']
strip_whitespace(s)
Removes leading and trailing whitespace (spaces, tabs, newlines).
>>> omath.strip_whitespace(" hello ")
'hello'
>>> omath.strip_whitespace("\t\n text \n\t")
'text'
>>> omath.strip_whitespace("no_whitespace")
'no_whitespace'
contains(haystack, needle)
Checks if needle exists in haystack.
>>> omath.contains("hello world", "world")
True
>>> omath.contains("hello world", "xyz")
False
>>> omath.contains("abcdef", "cde")
True
>>> omath.contains("short", "very long string")
False
reverse_string(s)
Reverses a string.
>>> omath.reverse_string("hello")
'olleh'
>>> omath.reverse_string("Python")
'nohtyP'
>>> omath.reverse_string("12345")
'54321'
is_palindrome(s)
Checks if a string is a palindrome (ignores case and non-letter characters).
>>> omath.is_palindrome("racecar")
True
>>> omath.is_palindrome("A man a plan a canal Panama")
True
>>> omath.is_palindrome("hello")
False
>>> omath.is_palindrome("Was it a car or a cat I saw")
True
char_count(s)
Counts occurrences of each character in a string.
>>> omath.char_count("hello")
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
>>> omath.char_count("aaa")
{'a': 3}
>>> omath.char_count("abab")
{'a': 2, 'b': 2}
Submodule Access
You can also import submodules directly:
# Import specific module
from omath import arithmetic
from omath import geometry
from omath import strings
# Use functions
arithmetic.multiply(5, 5)
geometry.circle_area(10)
strings.reverse_string("test")
Or import specific functions:
from omath import add, multiply, power
from omath import circle_area, rectangle_area
from omath import to_lowercase, is_palindrome
result = multiply(add(2, 3), power(2, 3)) # (2+3) * 2^3 = 40
Educational Value
This library is designed to help you understand:
- How multiplication works — It's just repeated addition
- How division works — It's just repeated subtraction
- How string methods work — ASCII codes and character manipulation
- How Python packages work — Module structure,
__init__.py, imports
Example: Understanding to_lowercase()
Instead of using str.lower(), we use ASCII math:
def to_lowercase(s):
result = ''
for char in s:
code = ord(char)
# A-Z are 65-90, a-z are 97-122
# Difference is 32
if 65 <= code <= 90:
result = result + chr(code + 32)
else:
result = result + char
return result
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Om Mishra - GitHub
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file omath-0.1.1.tar.gz.
File metadata
- Download URL: omath-0.1.1.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47f3b58d83e490b30172cf930d8d8e1d2019ffe5c598b33347a49559bb282b1a
|
|
| MD5 |
33bc37c6f8b6a2fd96710d55243be2da
|
|
| BLAKE2b-256 |
0cc2c26ae4f41998957cd0c74823d38140e5f997981293eed84284eff1dbb3c5
|
File details
Details for the file omath-0.1.1-py3-none-any.whl.
File metadata
- Download URL: omath-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b16774d29544985e83b70f69089b2d0c0a67043245f29dc81a50d4f8770d1620
|
|
| MD5 |
9e7fed9b8ee9f2f2219c397a219db0c5
|
|
| BLAKE2b-256 |
038b76682ddba0fca31699f910dfca043ee704113be947e123d2ec8f6b0360f9
|