Reduces large number to a more manageable number >>> res(5, 7) 4.892790030352132 >>> res(0, 5) 0 >>> res(3, 0) 1 >>> res(-1, 5) Traceback (most recent call last): ... ValueError: expected a positive input
(x, y)
| 4 | |
| 5 | |
| 6 | def res(x, y): |
| 7 | """ |
| 8 | Reduces large number to a more manageable number |
| 9 | >>> res(5, 7) |
| 10 | 4.892790030352132 |
| 11 | >>> res(0, 5) |
| 12 | 0 |
| 13 | >>> res(3, 0) |
| 14 | 1 |
| 15 | >>> res(-1, 5) |
| 16 | Traceback (most recent call last): |
| 17 | ... |
| 18 | ValueError: expected a positive input |
| 19 | """ |
| 20 | if 0 not in (x, y): |
| 21 | # We use the relation x^y = y*log10(x), where 10 is the base. |
| 22 | return y * math.log10(x) |
| 23 | elif x == 0: # 0 raised to any number is 0 |
| 24 | return 0 |
| 25 | elif y == 0: |
| 26 | return 1 # any number raised to 0 is 1 |
| 27 | raise AssertionError("This should never happen") |
| 28 | |
| 29 | |
| 30 | if __name__ == "__main__": # Main function |
no outgoing calls
no test coverage detected