Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges Decode periodic decimal fractions

JavaScript (Node.js), 242 234 bytes s=>([l,p,i,j,_,r]=/(-)?\+?(\d*)\.?(\d*?)(((\d+)\6|\d)\.|$)/.exec(s),[i,r]=c(c([i|0,1],[j|0,10**(l=j.length)]),[r=r||'0',10**l*(10**r.length-1)]),[(p?-i:i)/(j...

posted 3y ago by Moshi‭  ·  edited 3y ago by Moshi‭

Answer
#2: Post edited by user avatar Moshi‭ · 2021-09-14T21:31:21Z (about 3 years ago)
  • # [JavaScript (Node.js)], 242 bytes
  • ```javascript
  • s=>([l,p,i,j,_,r]=/(-)?\+?(\d*)\.?(\d*?)((((\d+)\7|\d)(?=\.))|$)/.exec(s),r=r||'0',[i,r]=c(c([i|0,1],[j|0,10**(l=j.length)]),[r,10**l*(10**r.length-1)]),[(p?-i:i)/(j=d(i,r)),r/j])
  • c=(a,b)=>[a[0]*b[1]+b[0]*a[1],a[1]*b[1]]
  • d=(a,b)=>b?d(b,a%b):a
  • ```
  • [Try it online!][TIO-ktj69bfo]
  • Regexes are love, regexes are life.
  • ## Explanation
  • The meat of it is this regex here:
  • ```
  • /(-)?\+?(\d*)\.?(\d*?)((((\d+)\7|\d)(?=\.))|$)/
  • ```
  • In order:
  • 1. `/(-)?\+?` Captures the optional negative or positive sign (we don't actually care about the positive sign, so it is outside the group.
  • 2. `(\d*)` Capture the integer part, if any
  • 3. `\.?` Optional period
  • 4. `(\d*?)` Captures the non-repeating decimal part
  • 5. `((((\d+)\7|\d)(?=\.))|$)` The complicated one. Captures either:
  • - `(\d+)\7` A repeating part of the decimal followed by a period `(?=\.)`
  • - `\d` The last digit followed by a period `(?=\.)`
  • - Nothing, we've reached the end of the string (`$`) and there is no period, meaning there is no repeating part
  • Once that's done it's a relatively simple formula to get the fraction. We just add together the integer part, the decimal part, and the fractional part as fractions, for which we have a helper method `c` which takes two fractions and outputs their sum as a (non-reduced) fraction:
  • <p>$$
  • \frac{i}{1}+\frac{j}{10^{\text{#digits of }j}}+\frac{r}{10^{\text{#digits of }j}\cdot(10^{\text{#digits of }r}-1)}
  • $$</p>
  • Finally, we have a helper method `d` which computes the greatest common divisor using Euclid's method. We divide by this to reduce the fraction, and at the same time multiply the numerator by whatever the sign was to get the final answer.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-ktj69bfo]: https://tio.run/##pVLNbuIwGDyTp7AQW2ywjZ0EaFoZTrsv0GMSbUNi2qCoQU521RXk2akd8gN0Vz1sDrG/me@bGVveRb@jIlbpviRveSJPW3EqxAr6Gd7jFO/wT6xCMYMErYPpGgbJBAW0XtcI6i9IpihYHoMEwbUIKELHEZpR@S5jWCCshDoex2yM/dTIxDCGfnpkmIfY35mVTSYwEzuaybeX8hWFCPuqRrMJNItqGMJrDu7XJH1I0QzuRAK1JtIes12IrFjACG@QWPmRz8LJxufhdGN2kd5h86ux0Eraxs06gRscfdugh@gU529FCUpZlAUQwLcG/pANMfBN1BCbktzUU0ZvENc2tWv3LTXQ12RharLoAfusQfoeds/nBtJLh9Eb55ua3NT0U9JPUQmtoxGO5w3iUO4aiM@XeM5aJYfSWpxjpzPnvAO9BuSUdVhrYVOvwZz@cJTb3TTzsMfYJWM3lIsd5wrn/yLsXowbtS64pi5Y28ae53XXc8V5zSS7pltLvTOzLUu47bjzBTWL4zY9ZME5Z8t7zrHrmcuzwkfL2uYKwP5dgXx7fl8IHKzBGS9KpV@bQfVLfWxR@b6XcSmTluKGKtUfM9e0bFUUa3oLtQLSXoNBugXQoFoICCE6EVPf3dUDWuia4mGd5SyaZ5Jm@Qt8fvoVx7IoHsDoAMfDMZjWMadA7xHdR8lTGakScgeDMRijCpCV7mysKzBrCx5WzzraYFABmRXyL0Y/ojSTyVc@7tc@AHZXNjpcHPzcdXHcCjWRLJ0qjsr4FUqlUHex/xHtu1K5qtUrqzp9AA "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], ~~242~~ 234 bytes
  • ```javascript
  • s=>([l,p,i,j,_,r]=/(-)?\+?(\d*)\.?(\d*?)(((\d+)\6|\d)\.|$)/.exec(s),[i,r]=c(c([i|0,1],[j|0,10**(l=j.length)]),[r=r||'0',10**l*(10**r.length-1)]),[(p?-i:i)/(j=d(i,r)),r/j])
  • c=(a,b)=>[a[0]*b[1]+b[0]*a[1],a[1]*b[1]]
  • d=(a,b)=>b?d(b,a%b):a
  • ```
  • [Try it online!][TIO-ktj69bfo]
  • Regexes are love, regexes are life.
  • ## Explanation
  • The meat of it is this regex here:
  • ```
  • /(-)?\+?(\d*)\.?(\d*?)(((\d+)\6|\d)\.|$)/
  • ```
  • In order:
  • 1. `/(-)?\+?` Captures the optional negative or positive sign (we don't actually care about the positive sign, so it is outside the group.
  • 2. `(\d*)` Capture the integer part, if any
  • 3. `\.?` Optional period
  • 4. `(\d*?)` Captures the non-repeating decimal part
  • 5. `(((\d+)\6|\d)\.|$)` The complicated one. Captures either:
  • - `(\d+)\6` A repeating part of the decimal followed by a period `\.`
  • - `\d` The last digit followed by a period `\.`
  • - Nothing, we've reached the end of the string (`$`) and there is no period, meaning there is no repeating part
  • Once that's done it's a relatively simple formula to get the fraction. We just add together the integer part, the decimal part, and the fractional part as fractions, for which we have a helper method `c` which takes two fractions and outputs their sum as a (non-reduced) fraction.
  • <p>$$
  • \frac{i}{1}+\frac{j}{10^{\text{#digits of }j}}+\frac{r}{10^{\text{#digits of }j}\cdot(10^{\text{#digits of }r}-1)}
  • $$</p>
  • Finally, we have a helper method `d` which computes the greatest common divisor using Euclid's method. We divide by this to reduce the fraction, and at the same time multiply the numerator by whatever the sign was to get the final answer.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-ktj69bfo]: https://tio.run/##pVLbbqMwFHwOX2FF2WIntmMDSUsqkqfdH@gjQVsupiVCITLsqquGb09twiVJd9WHfeH4zMyZM1jehb/DMpbZoSL7IhGn1DuV3hr6OT7gDO/wTywDbw4J2mxnG7hNpmhLm7pBEKo6Q9vlcZso9DhBcyreRAxLhP1Mz8Uwhn52ZJgH2N/pyqZTmHs7mov9S/WKAqWUnjweTWY2ZD6FushWQHgjgYcNyVYZmsOdl0BljRCW812AjNiDIY6Qt/ZDnwXTyOfBLNKnUJ2w/jRYYCSdMNokMMLhtwitwlNc7MsKVKKsSuAB3xj5YzbGwNeJA6xbctPPGL1BHEv3jjVIGmDoyVL3ZDkA1tmDDBr2wBcaUqXH6M3mm57c9PRT0k9RCW2iEY4XLWJT7miIL@7xgnVONqWNOcd2v5zzHnRbkFPWY90Ki7otZg8/R7nVTzMXu4xdMlZLOdi2r3D@L8IazLh264Mr6oK1LOy6bn89V5zbTrJrulupTnq2Ywm3bGexpLrYTqshS845u3/gHDuuvjwjeDSMtJAADu8KFOn5fSHwbozOeFlJ9do0ql7qY4eKt4OIK5F0FNdUJf/ouVaSyjBWdAqVA1K7RqMsBVCjygh4nteb6P7urhlQRtcUD5osZ9MiFzQvXuDz0684FmW5ApN3aI5NMGtizoA6I3oIk6cqlBXkNgYmMFENyFop29U1mHcND@pnFW00qoHIS/GXRT/CLBfJV3ucr/cA2F/Z5P3ix8@qi9@tURvJUKnisIpfoZAS9Rf7H9G@S1nIxr026tMH "JavaScript (Node.js) – Try It Online"
#1: Initial revision by user avatar Moshi‭ · 2021-09-13T21:53:28Z (about 3 years ago)
# [JavaScript (Node.js)], 242 bytes

```javascript
s=>([l,p,i,j,_,r]=/(-)?\+?(\d*)\.?(\d*?)((((\d+)\7|\d)(?=\.))|$)/.exec(s),r=r||'0',[i,r]=c(c([i|0,1],[j|0,10**(l=j.length)]),[r,10**l*(10**r.length-1)]),[(p?-i:i)/(j=d(i,r)),r/j])
c=(a,b)=>[a[0]*b[1]+b[0]*a[1],a[1]*b[1]]
d=(a,b)=>b?d(b,a%b):a
```

[Try it online!][TIO-ktj69bfo]


Regexes are love, regexes are life.

## Explanation

The meat of it is this regex here: 

```
/(-)?\+?(\d*)\.?(\d*?)((((\d+)\7|\d)(?=\.))|$)/
```
In order:

 1. `/(-)?\+?` Captures the optional negative or positive sign (we don't actually care about the positive sign, so it is outside the group.

 2. `(\d*)` Capture the integer part, if any

 3. `\.?` Optional period

 4. `(\d*?)` Captures the non-repeating decimal part

 5. `((((\d+)\7|\d)(?=\.))|$)` The complicated one. Captures either: 

    - `(\d+)\7` A repeating part of the decimal followed by a period `(?=\.)`
    - `\d` The last digit followed by a period `(?=\.)`
    - Nothing, we've reached the end of the string (`$`) and there is no period, meaning there is no repeating part

Once that's done it's a relatively simple formula to get the fraction. We just add together the integer part, the decimal part, and the fractional part as fractions, for which we have a helper method `c` which takes two fractions and outputs their sum as a (non-reduced) fraction:

<p>$$
\frac{i}{1}+\frac{j}{10^{\text{#digits of }j}}+\frac{r}{10^{\text{#digits of }j}\cdot(10^{\text{#digits of }r}-1)}
$$</p>

Finally, we have a helper method `d` which computes the greatest common divisor using Euclid's method. We divide by this to reduce the fraction, and at the same time multiply the numerator by whatever the sign was to get the final answer.

[JavaScript (Node.js)]: https://nodejs.org
[TIO-ktj69bfo]: https://tio.run/##pVLNbuIwGDyTp7AQW2ywjZ0EaFoZTrsv0GMSbUNi2qCoQU521RXk2akd8gN0Vz1sDrG/me@bGVveRb@jIlbpviRveSJPW3EqxAr6Gd7jFO/wT6xCMYMErYPpGgbJBAW0XtcI6i9IpihYHoMEwbUIKELHEZpR@S5jWCCshDoex2yM/dTIxDCGfnpkmIfY35mVTSYwEzuaybeX8hWFCPuqRrMJNItqGMJrDu7XJH1I0QzuRAK1JtIes12IrFjACG@QWPmRz8LJxufhdGN2kd5h86ux0Eraxs06gRscfdugh@gU529FCUpZlAUQwLcG/pANMfBN1BCbktzUU0ZvENc2tWv3LTXQ12RharLoAfusQfoeds/nBtJLh9Eb55ua3NT0U9JPUQmtoxGO5w3iUO4aiM@XeM5aJYfSWpxjpzPnvAO9BuSUdVhrYVOvwZz@cJTb3TTzsMfYJWM3lIsd5wrn/yLsXowbtS64pi5Y28ae53XXc8V5zSS7pltLvTOzLUu47bjzBTWL4zY9ZME5Z8t7zrHrmcuzwkfL2uYKwP5dgXx7fl8IHKzBGS9KpV@bQfVLfWxR@b6XcSmTluKGKtUfM9e0bFUUa3oLtQLSXoNBugXQoFoICCE6EVPf3dUDWuia4mGd5SyaZ5Jm@Qt8fvoVx7IoHsDoAMfDMZjWMadA7xHdR8lTGakScgeDMRijCpCV7mysKzBrCx5WzzraYFABmRXyL0Y/ojSTyVc@7tc@AHZXNjpcHPzcdXHcCjWRLJ0qjsr4FUqlUHex/xHtu1K5qtUrqzp9AA "JavaScript (Node.js) – Try It Online"