Convert a Decimal Number to an Octal Number. >>> all(decimal_to_octal(i) == oct(i) for i ... in (0, 2, 8, 64, 65, 216, 255, 256, 512)) True
(num: int)
| 7 | |
| 8 | |
| 9 | def decimal_to_octal(num: int) -> str: |
| 10 | """Convert a Decimal Number to an Octal Number. |
| 11 | |
| 12 | >>> all(decimal_to_octal(i) == oct(i) for i |
| 13 | ... in (0, 2, 8, 64, 65, 216, 255, 256, 512)) |
| 14 | True |
| 15 | """ |
| 16 | octal = 0 |
| 17 | counter = 0 |
| 18 | while num > 0: |
| 19 | remainder = num % 8 |
| 20 | octal = octal + (remainder * math.floor(math.pow(10, counter))) |
| 21 | counter += 1 |
| 22 | num = math.floor(num / 8) # basically /= 8 without remainder if any |
| 23 | # This formatting removes trailing '.0' from `octal`. |
| 24 | return f"0o{int(octal)}" |
| 25 | |
| 26 | |
| 27 | def main() -> None: |