Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This is not a solution, but a tale of a solution that didn't work out.

I thought: hey, why not solve it with regexes?

First, convert the number $n to a string of length $n,  1 x $n. Then write a regex that matches only strings of lengths that are powers of two. then do (1 x $n) =~ /^(?:($that_regex)(?{ say length $1}))*

and be happy.

You can recursively define powers of two as

n_0 = 1 n_i = n_(i-1) + n_(i-1)

That should be straight forward to mold into a regex, no? After all, our regexes are recursive:

say (1 x 5) =~ /^(1|((?1))\2)*$/

Unfortunately Perl 5 does not agree: Infinite recursion in regex

The problem is that as soon as all the 1s are exhausted, the regex engine endlessly recurses (the (?1) branch) while not consuming any characters.

So, if only the end is the problem, maybe we can just stop recursing when we're at the end of the string? Look-aheads to the rescue:

(1 x 5) =~ /^(?:(1|((?=1)(?1))\2)(?{ say length $1 }))*$/

And indeed, it matches. But it only ever matches chunks of length 1, because the left alternative is tried first. So the |1 would need to go at the end, but then it's still infinite recursion, because the recursion occurs before consuming any characters.

I know this isn't what regexes are meant for, but I'd still like it to work. Unfortunately I've run out of ideas for now. Does anybody know how to fix it?

In reply to Re: mini-golf: convert integer into list of powers of two by moritz
in thread mini-golf: convert integer into list of powers of two by xyzzy

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2021-08-03 18:39 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (39 votes). Check out past polls.