This project implements a symbolic polynomial expression system in Python that allows you to build and represent polynomial expressions using a tree-like structure. This is a student exercise where you'll implement missing functionality.
The code provides the following classes with incomplete implementations:
X
: Represents the variable X in polynomial expressions ✅ CompleteInt
: Represents integer constants in polynomial expressions ✅ CompleteAdd
: Represents addition of two polynomial expressions ✅ CompleteMul
: Represents multiplication of two polynomial expressions ✅ CompleteSub
: Represents subtraction of two polynomial expressions ❌ Needs ImplementationDiv
: Represents division of two polynomial expressions ❌ Needs Implementation
__repr__
: String representation ✅ Complete for X, Int, Add, Mulevaluate(x_value)
: Computes polynomial value for given X ❌ Needs Implementationsimplify()
: Simplifies expressions ❌ Optional Exercise
You have 3 exercises to complete:
- Exercise 1: Implement
Sub
andDiv
classes - Exercise 2: Implement
evaluate
methods for all classes - Exercise 3: Implement
simplify
methods (Optional)
Complete the Sub
and Div
classes to handle polynomial subtraction and division.
class Sub:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def __repr__(self):
# TODO: Implement string representation for subtraction
# Should handle parentheses similar to Mul class
# Hint: Look at how Mul class handles parentheses
pass
class Div:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def __repr__(self):
# TODO: Implement string representation for division
# Should handle parentheses similar to Mul class
# Hint: Look at how Mul class handles parentheses
pass
- String Representation: Handle parentheses correctly for mathematical notation
- Precedence Rules: Follow the same pattern as
Mul
class - Examples:
Sub(Int(10), Int(3))
should print as10 - 3
Sub(Add(Int(2), Int(3)), Int(4))
should print as( 2 + 3 ) - 4
Div(Int(15), Int(3))
should print as15 / 3
Div(Sub(Int(10), Int(2)), Int(4))
should print as( 10 - 2 ) / 4
- Look at how the
Mul
class handles parentheses in its__repr__
method - Use
isinstance()
to check if operands need parentheses - Subtraction and division have the same precedence as addition and subtraction respectively
Add evaluate(x_value)
methods to all classes to compute polynomial values.
def evaluate(self, x_value):
# TODO: Implement evaluation logic
pass
- Should return
Int(x_value)
- the value of X
- Should return
Int(self.i)
- the stored integer value
- Should evaluate both operands and return their sum
- Example:
Add(Int(3), Int(5)).evaluate(0)
should returnInt(8)
- Should evaluate both operands and return their product
- Example:
Mul(Int(3), Int(5)).evaluate(0)
should returnInt(15)
- Should evaluate both operands and return their difference
- Example:
Sub(Int(10), Int(3)).evaluate(0)
should returnInt(7)
- Should evaluate both operands and return their quotient (use integer division
//
) - Example:
Div(Int(15), Int(3)).evaluate(0)
should returnInt(5)
# Simple evaluation
poly = Add(Mul(Int(2), X()), Int(3)) # 2*X + 3
result = poly.evaluate(5) # Should return Int(13)
# Complex evaluation
complex_poly = Add(Sub(Mul(Int(2), X()), Int(1)), Div(Int(6), Int(2)))
result = complex_poly.evaluate(4) # Should return Int(10)
Add simplify()
methods to all classes to simplify polynomial expressions.
def simplify(self):
# TODO: Implement simplification logic
pass
- Cannot be simplified further, return
self
X + 0
→X
0 + X
→X
3 + 5
→8
X + X + X
→3 * X
(advanced)
X * 0
→0
X * 1
→X
3 * 5
→15
X - 0
→X
5 - 3
→2
X / 1
→X
6 / 2
→3
- Combine like terms:
X + X + X
→3 * X
- Factor expressions:
2 * X + 4 * X
→6 * X
# Before simplification
expr = Add(Add(X(), Int(0)), Mul(Int(2), Int(3)))
print(expr) # X + 0 + 2 * 3
# After simplification
simplified = expr.simplify()
print(simplified) # X + 6
python polynomial.py
# Run comprehensive test suite
python test_polynomial.py
# Or run through main file
python polynomial.py --test
The test suite will show you which exercises you've completed and which still need work.
polynomial/
├── polynomial.py # Main implementation file (your work goes here)
├── test_polynomial.py # Comprehensive test suite
├── README.md # This file
├── .gitignore # Python gitignore file
└── .github/
└── workflows/
└── test.yml # GitHub Actions workflow
This repository includes a GitHub Actions workflow that automatically runs tests when you push code or create pull requests. The workflow:
- Tests on multiple Python versions (3.8, 3.9, 3.10, 3.11, 3.12)
- Runs all test suites to verify your implementations
- Analyzes exercise progress and shows which parts are completed
- Checks code quality with linting tools (flake8, black, isort)
- Validates syntax to catch basic errors
- ✅ Multi-version testing: Ensures your code works across Python versions
- ✅ Progress tracking: Shows which exercises you've completed
- ✅ Code quality checks: Helps maintain clean, readable code
- ✅ Automatic triggers: Runs on push, pull requests, and manual triggers
- ✅ Detailed feedback: Clear status indicators for each exercise
- Go to the Actions tab in your GitHub repository
- Click on the latest workflow run
- View detailed results for each Python version
- Check the "exercise-progress" job for completion status
- Start with Exercise 1: Implement the
Sub
andDiv
classes - Run tests: Use
python test_polynomial.py
to see your progress - Move to Exercise 2: Implement the
evaluate
methods - Test again: Verify all evaluation tests pass
- Try Exercise 3: Implement simplification (optional)
- Read the existing code: Look at how
Add
andMul
classes work - Use the tests: They'll guide you on what to implement
- Start simple: Get basic functionality working first
- Test frequently: Run tests after each small change
- Ask questions: The TODO comments provide hints
Good luck! 🚀