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

77%
+5 −0
Challenges Decode periodic decimal fractions

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 thre...

2 answers  ·  posted 3y ago by celtschk‭  ·  edited 3y ago by General Sebast1an‭

#3: Post edited by user avatar General Sebast1an‭ · 2021-09-25T11:31:36Z (about 3 years ago)
Decode periodic decimal fractions
  • 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.
  • This is code golf, that is the shortest code wins.
  • 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
  • ```
  • 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.
  • This is <a class="badge is-tag">code-golf</a>, that is the shortest code wins.
  • 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
  • ```
#2: Post edited by user avatar celtschk‭ · 2021-09-15T05:53:36Z (over 3 years ago)
Added forgotten scoring criterion
  • 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
  • ```
  • 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.
  • This is code golf, that is the shortest code wins.
  • 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
  • ```
#1: Initial revision by user avatar celtschk‭ · 2021-09-13T16:27:55Z (over 3 years ago)
Decode periodic decimal fractions
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
```