gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Given an int n, return the absolute difference between n and 21, except return double…
- User
Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21. diff21(19) → 2 diff21(10) → 11 diff21(21) → 0
- Assistant
You can solve this problem by checking if
nis greater than 21 or not:- If
nis less than or equal to 21, return the absolute difference betweennand 21. - If
nis greater than 21, return double the absolute difference.
Here's a simple Python function that implements this:
pythondef diff21(n): if n <= 21: return 21 - n else: return 2 * (n - 21)Test cases:
pythonprint(diff21(19)) # Output: 2 print(diff21(10)) # Output: 11 print(diff21(21)) # Output: 0 print(diff21(25)) # Output: 8 (since 25 > 21, double the difference: (25 - 21) * 2 = 4 * 2 = 8)Let me know if you want this solution in another programming language!
- If