S1Q1 · Sum of Squares and Absolute Difference of Squares¶
⚡ Quick Reference
Function: sum_squares_abs_diff_squares(a: int, b: int) -> tuple
Core idea: compute a² and b², return both their sum and their absolute difference.
Key rules:
- First element: a² + b²
- Second element: |a² - b²|
- abs() handles the case where b² > a²
Problem Statement¶
Problem
Write a function sum_squares_abs_diff_squares(a, b) that returns a tuple of:
1. The sum of the squares: a² + b²
2. The absolute difference of the squares: |a² - b²|
Example:
a=3, b=4
(25, 7)
Verification: 3²=9, 4²=16, sum=9+16=25, abs diff=|9-16|=7
Solution approaches¶
Key takeaways¶
abs() for absolute difference
abs(a² - b²) handles both cases: when a² > b² and when b² > a². Never write if a**2 > b**2: ... else: ... when abs() does it in one call.
Compute squares once, reuse
If using the explanatory approach, store sq_a and sq_b - avoids computing a**2 twice. For two values it's minor, but it's a good habit.
Return a tuple, not a list
The problem specifies a tuple return type. (x, y) creates a tuple; [x, y] creates a list. They're different types - check the expected return carefully.