sum_squares_abs_diff_squares
Section 1 | Problem 1
๐งฎ Function: sum_squares_abs_diff_squares
Let’s create a helpful function to work with squares and differences! ๐ฏ
๐ฉ What should the function do?
- Inputs: Two integers (
a
,b
) - Returns: A tuple with:
- Sum of the squares of
a
andb
- Absolute difference of their squares
๐งโ๐ป Function Implementation
def sum_squares_abs_diff_squares(a, b):
sum_squares = a**2 + b**2
abs_diff_squares = abs(a**2 - b**2)
return (sum_squares, abs_diff_squares)
๐ Step-by-Step Explanation
- Square Each Number:
- Compute
a**2
andb**2
.
- Compute
- Sum of Squares:
- Add them:
a**2 + b**2
- Add them:
- Absolute Difference of Squares:
- Subtract:
a**2 - b**2
- Apply
abs()
to always get a non-negative result.
- Subtract:
- Return as a Tuple:
- Return both in the order specified.
๐งช Practice Questions
Practice 1
result = sum_squares_abs_diff_squares(5, 3)
print(result) # What do you expect?
- $5^2 + 3^2 = 25 + 9 = 34$
- $\lvert 25 - 9 \rvert = 16$
- Output:
(34, 16)
Practice 2
result = sum_squares_abs_diff_squares(-4, 2)
print(result)
- $(-4)^2 + 2^2 = 16 + 4 = 20$
- $\lvert 16 - 4 \rvert = 12$
- Output:
(20, 12)
Practice 3
result = sum_squares_abs_diff_squares(7, 7)
print(result)
- $7^2 + 7^2 = 49 + 49 = 98$
- $\lvert 49 - 49 \rvert = 0$
- Output:
(98, 0)
โจ Key Points
- Always returns a tuple with (sum, absolute difference).
- Works for positive, negative, or zero values of
a
andb
. - No printing or inputโjust define & use the function in your code!
Happy Coding! ๐