Develop a simple function
- Document name: Add2Num
- Project: Add 2 numbers
- Creator: Hoang Timothy
This project implements a core function that adds two large numbers represented as strings, using the same step-by-step method that students use in primary school.
The core logic is wrapped in a separate class named MyBigNumber, so another team can reuse it from a UI, console app, or a larger system.
- The content in section 1 of the requirement document.
- Python 3.13 or update
unittestfor unit testing- Logging via the Python
loggingmodule
The core code is implemented in src/add2num.py.
The main class is MyBigNumber, with method sum(stn1, stn2).
Implementation approach:
- Scan both strings from right to left.
- Read one digit from each string in each step.
- Add two digits together with the current carry.
- Store the current digit in the result and remember the new carry.
- Repeat until both strings and the carry are exhausted.
- Reverse the collected digits to get the final result string.
The method also records each arithmetic step by logging, so the addition process can be traced easily.
Where the logs appear:
- When you run
src/add2num.py, the demo configuresloggingto print INFO-level messages to the console. - When you run the unit tests, the logs are captured by the test fixture in
tests/unittest.pyand are asserted in memory instead of being printed. - If you want to see the log stream while testing, run the module directly or attach a stream handler in your own script.
Example:
sum("1234", "897")returns2131- Step 1:
4 + 7 = 11, write1, carry1 - Step 2:
3 + 9 + 1 = 13, write3, carry1 - Continue the same way until all digits are processed.
Assumption used in this project:
- Input values are valid digit strings only.
- No invalid data handling is required for this task.
Deliverables:
- Source code is stored in the Git repository.
- Unit tests are included in
tests/unittest.py.```
Run the unit tests:
python -m tests.unittestRun the demo from the module directly:
python src/add2num.pyThis prints the step-by-step addition log to the console.
The repository includes unit tests for:
- simple addition
- carry propagation
- different length inputs
- leading zeros
- zero + zero
- logging output of each step