Quote notation is a way of expressing rational numbers based on the concept of \$p\$-adic numbers, written in the form \$x'y\$.
The quote indicates that the number to it's left (\$x\$) is "repeated" infinitely to the left, then prefixed to the number on the right (\$y\$). For example \$3' = \: ...3333\$ and \$764'31 = \: ...76476431\$. We can then consider the geometric series:
$$\cdots + 10^{3k} + 10^{2k} + 10^{k} + 10^{0} = \frac 1 {1 - 10^k}$$
By setting \$k\$ to be equal to the number of digits of \$x\$, we can transform this "infinite" number into a value which converges:
$$\begin{align} 3' & = \: ...333 & 764'31 & = \: ...76476431 \\ & & & = 31 + 764'00 \\ & = \cdots + 3\cdot10^3 + 3\cdot10^2 + 3\cdot10^1 + 3\cdot10^0 & & = 31 + 100 \times 764'\\ & = 3(\cdots + 10^3 + 10^2 + 10^1 + 10^0) & & = 31 + 100 \times 764(\cdots + 10^9 + 10^6 + 10^3 + 10^0)\\ & = 3\left( \frac 1 {1 - 10} \right) & & = 31 + 76400\left( \frac 1 {1 - 10^3} \right) \\ & = \frac {3} {-9} & & = 31 - \frac {76400} {999}\\ & = - \frac 1 3 & & = - \frac {45431} {999} \end{align}$$
Note that \$9'0 \ne 9'\$ as the first equals \$-10\$ and the second \$-1\$. Additionally, note that leading zeros on \$y\$ do affect the output: \$81'09 = -\frac {801} {11} \ne \frac {9} {11} = 81'9\$ Therefore, a value after the \$'\$ (\$y\$) may be omitted in the input.
You are to take, in any reasonable format, up to 2 non-negative integers \$x\$ and \$y\$, and output the fraction \$\frac a b\$ represented by \$x'y\$. Reasonable formats include:
- A string, delimited by a non-digit character, such as
'
, e.g.9'
or9'0
. The string will always begin with a digit; if there is no \$x\$ value, it will be a \$0\$ (e.g.0'3
) - A list of either 1 or 2 non-negative integers, represented as strings or lists of digits. If there is only 1, it represents \$x'\$. 2 integers represent \$x'y\$.
- A list of 2 elements. The last element may be either a non-negative integer (as a string or digit list), or a consistent value that is not a non-negative integer (e.g.
null
orNone
or-1
etc.) that indicates that there is no \$y\$ value. The first value will always be a non-negative integer.
This is not an exhaustive list, if you have another method, feel free to ask.
You may output the two integers \$a\$ and \$b\$ instead of the fraction \$\frac a b\$. The fraction must be exact, and fully simplified (i.e. \$a\$ and \$b\$ are coprime). If \$b\$ is 1, outputting just \$a\$ is acceptable. For negative outputs, if outputting \$a\$ and \$b\$ separately, either may have the negative sign, but not both.
You may input and output in any convenient method. This is code-golf so the shortest code in bytes wins.
Modified from the linked Wikipedia page:
Let \$x\$ and \$y\$ be sequences of digits, as in \$x'y\$
Let \$z\$ be the digit \$1\$ followed by a sequence of zeros of the same length as \$y\$.
Let \$w\$ be a sequence of \$9\$s of the same length as \$x\$.
Then the number represented by \$x'y\$ is given by \$y-\frac{xz}w\$
Test cases
x'y = a / b
31'491 = 17609 / 99
844'80 = -4480 / 999
4'128 = -2848 / 9
247'0 = -2470 / 999
0'716 = 716 / 1
592' = -16 / 27
3'45 = 35 / 3
9'7 = -3 / 1
9' = -1 / 1
3'0 = -10 / 3
764'31 = -45431 / 999
81'09 = -801 / 11
81'9 = 9 / 11
123456' = -41152 / 333333
081'09 = 33/37
\$\endgroup\$081'10
would be written810'8110 = 70/37
? \$\endgroup\$108'110
, I guess. \$\endgroup\$