Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: The Oldest Plays the Piano

by CountZero (Bishop)
on Sep 21, 2009 at 22:33 UTC ( #796623=note: print w/ replies, xml ) Need Help??


in reply to The Oldest Plays the Piano

Solving diophantine equations works in Perl 5.8 as well:

# Solve 12x + 15y + 16z = 281 $_ = 'o' x 281; m/^(o*)\1{11}(o*)\2{14}(o*)\3{15}$/; print length($1),"\t", length($2),"\t", length($3),"\n";
PS: I did not invent this, but found it somewhere and cannot remember where.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


Comment on Re: The Oldest Plays the Piano
Download Code
Re^2: The Oldest Plays the Piano
by didier (Vicar) on Sep 24, 2009 at 19:42 UTC
    Very impressive.
    Could you describe the way the regex engine act to solve this?

    Many thanks for the answer if any :)

      The way I think it works is that the regex-engine starts by grabbing the whole of the string for the first capture and then sees that it cannot match anything anymore and thus starts "giving back" character by character to match the other elements of the regex.

      As 'x' has to come in blocks of 12 elements, the regex for that variable is (o*)\1{11}, being whatever the regex-engine in trying, followed by 11 times that what is being tried for the first capture. 11 + 1 = 12, so you have your '12x'. The same goes for the other variables.

      Probably inside the regex there are some shortcuts and optimizations, but basically it is just "try everything until you find a solution".

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://796623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2014-10-20 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls