Re: String Pattern Matching algo
- From: "Rod Pemberton" <do_not_have@xxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 20:29:00 -0400
<116Rohan@xxxxxxxxx> wrote in message
news:1146343948.469971.217040@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I came across a question in one of the Computing olympiad regarding
string pattern matching.
Write a program that will accept a fraction of the form N/D, where N is
the numerator and D is the denominator, that prints out the decimal
representation. If the decimal representation has a repeating sequence
of digits, it should be indicated by enclosing it in brackets. For
example, 1/3 = .33333333...is denoted as .(3), and 41/333 =
.123123123...is denoted as .(123).
Typical conversions are:
1/3 = .(3)
22/5 = 4.4
1/7 = .(142857)
3/8 = .375
45/56 = .803(571428)
Now I could not think how do I even start writing a logic for creating
a pattern.
IIRC, there is a recursive algorithm (using 1/X ?) to convert terminated
(non-repeating and not random values PI, etc.) decimals to the form N/D. If
so (and since they gave you a valid fraction), you could compute N/D as
decimal and then attempt to convert back to N/D. If the number is
non-repeating, the first N and D should be the same as the second conversion
N and D. If the number is repeating, it will be terminated by the precision
of the machine. This means that the first N and D will be different from
the conversion N and D, because it lost some precision. For the repeating
numbers, you then need to determine the length of the repeat. I don't know
if there is a simple method for that.
Rod Pemberton
.
- Follow-Ups:
- Re: String Pattern Matching algo
- From: Rod Pemberton
- Re: String Pattern Matching algo
- References:
- String Pattern Matching algo
- From: 116Rohan@xxxxxxxxx
- String Pattern Matching algo
- Prev by Date: Re: Boost process and C
- Next by Date: Re: Structure
- Previous by thread: Re: String Pattern Matching algo
- Next by thread: Re: String Pattern Matching algo
- Index(es):
Relevant Pages
|
|