Problem 157044 · hard · Level 01 Prerequisites & Setup

The Calculator That Never Rounds

functions · long division · remainders · lists · strings

A calculator for a maths class never rounds. It shows the exact value of num / den as a string:

  • a leading - when the value is negative (never for zero),
  • the whole-number part,
  • if the value is not a whole number, a . followed by the digits after the point; if the digits repeat forever, the digits that repeat are written once inside parentheses, and the parentheses start as early as possible.

Write as_decimal(num, den) that returns that string.

Examples

Input:  num = 1, den = 6
Output: "0.1(6)"

Input:  num = 22, den = 7
Output: "3.(142857)"

Input:  num = -1, den = 4
Output: "-0.25"

Input:  num = 6, den = -3
Output: "-2"

Constraints

  • -10**9 <= num <= 10**9
  • 1 <= abs(den) <= 10**5

Goals

  • Carry out long division one digit at a time
  • Detect a repeating remainder with a lookup list
  • Handle signs and exact division cleanly
Starting Python…