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 »
Sandbox

Comments on Decode periodic decimal fractions [FINALIZED]

Post

Decode periodic decimal fractions [FINALIZED]

+4
−0

Rational numbers in decimal representation can have an infinite periodic part. One common way to write this down is to repeat the periodic digits and then add three dots. Numbers without those three dots are taken to be non-periodic (or equivalently, have a periodic 0 after the given digits). Your task is to decode this representation into a fully cancelled fraction.

In particular, the numbers to decode are given as follows:

  • There is an optional sign (+ or -). If omitted, + is assumed.

  • There is an integral part. If empty, it is taken to be 0.

  • There is a decimal point, which is optional if the number is an integer and the integral part was not omitted.

  • There is an fractional part following the integer.

  • If the fractional part has at least one digit, it may be followed by three dots.

The program shall take a string as input, and give a fully cancelled fraction (as pair numerator/denominator) as result. It may assume that the given string conforms to the number format.

The string has to be interpreted as follows:

  • If the string does not end in three dots, it is interpreted as exact number.

  • If the string does end in three dots, find the longest repeating digit sequence in the fractional part preceding the three dots. If no such repeating sequence is found, the period consists of just the last digit. Otherwise it consists of that longest repeating digit sequence.

  • Output the fraction that corresponds to the determined periodic digit sequence. The denominator shall be positive. The fraction shall be completely cancelled.

  • Your code must handle at least up to 6 digits before and up to 6 digits after the decimal point.

Test cases:

                "0" -> 0/1
               "-0" -> 0/1
             "+0.0" -> 0/1
               "42" -> 42/1
               "+2" -> 2/1
               "-6" -> -6/1
             "-2.0" -> -2/1
             "0815" -> 815/1
                "." -> 0/1
               "+." -> 0/1
               "-." -> 0/1
               ".0" -> 0/1
            "+00.0" -> 0/1
              "-.2" -> -1/5
             "3.14" -> 157/50
            ".3..." -> 1/3
          "+.11..." -> 1/9
           "1.0..." -> 1/1
           "2.9..." -> 3/1
         "0.121..." -> 109/900
        "0.1212..." -> 4/33
       "0.12121..." -> 4/33
       "0.12122..." -> 1091/9000
       ".122122..." -> 122/999
       ".022122..." -> 1991/90000
      ".0221221..." -> 221/9990
"-123456.123434..." -> -611107811/4950
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Test cases (4 comments)
Test cases
Hakerh400‭ wrote about 3 years ago

611107811/4950 should be negative. Can you also add -0, -., and a few test cases with explicit +?

celtschk‭ wrote about 3 years ago

Oops, thank you for catching this. I'll fix and also add the extra test cases.

Hakerh400‭ wrote about 3 years ago · edited about 3 years ago

"+2" is 2/1, not 2/2. Also, ".122122..." is 122/999, not 211/999

celtschk‭ wrote about 3 years ago

Oops, right, thank you again. I'll immediately fix this.