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

Re: Puzzle Regex: Letter Frequency Arithmetic Sequence

by LanX (Saint)
on Oct 17, 2017 at 15:42 UTC ( [id://1201509]=note: print w/replies, xml ) Need Help??


in reply to Puzzle Regex: Letter Frequency Arithmetic Sequence

The link you gave shows an example but doesn't phrase criteria, which is unfortunate for a puzzle.

Please see How do I post a question effectively?

Supposing the frequency of letters has to be an ascending sequence. ..

I'm not aware of a possibility to sum the count of matches in pure ² m// regexes so probably it's possible in tr// but those can't backtrack.

Probably if you code 1..n matches for a fixed n into your regex and combine n look-aheads you can built an and condition. *

This wouldn't work for arbitrary big n though.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

*) that is find one letter exactly once and find one letter exactly twice and so on.

already the first term is too tricky for me...

²) no Perl embedded

update

First step: What is a regex to find one letter exactly once?

update

Waiting for tybalt89 ... tick tock tick tock ... ;)

Replies are listed 'Best First'.
Re^2: Puzzle Regex: Letter Frequency Arithmetic Sequence
by tybalt89 (Monsignor) on Oct 17, 2017 at 23:47 UTC

    A regex to find if a letter occurs exactly $n times.

    Is this what you were looking for as a first step?

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1201500 use strict; use warnings; my @words = glob '{i,x}' x 6; print "@words\n"; my $n = 1; # number of times a letter occurs in a word my @oneletter = grep /^(?| (?=.*?(.)(?!.*\1)) (?: (?:(?!\1).)* \1 ){$n} (?:(?!\1).)* | (?=.*(.)) (?: (?:(?!\1).)* \1 ){$n} (?:(?!\1).)* )$ /x, @words; print "\n@oneletter\n\n", scalar @oneletter, "\n";

    Update: fails for some test cases

      Can you share some interesting test cases?

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re^2: Puzzle Regex: Letter Frequency Arithmetic Sequence
by tybalt89 (Monsignor) on Oct 19, 2017 at 09:32 UTC

    Too much cheating?

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1201500 use strict; use warnings; my $file = '/usr/share/dict/words'; open my $fh, '<', $file or die "$! opening $file"; chomp( my @words = grep /^[a-z]{5,}$/, <$fh>); my @good = grep frequencysequence($_), @words; print "@good\n\n" . @good . "\n"; sub frequencysequence { for my $n ( 1 .. ((-1 + sqrt 1 + 8 * length) / 2) =~ s/^(\d+)\..*/$1 + + 1/er ) { "@_\n@_" =~ / (.).* \n (?: (?:(?!\1).)* \1 ){$n} (?:(?!\1).)* $ /x or return 0; } return 1; }

    On my system outputs:

    acacia allele assays banana baobab bedded bonobo bowwow cocoon deadhea +ded deemed doodad eddied eerier effete fesses heeded horror hubbub in +ning lessee lollop mammal manana messes needed papaya peeped peeper p +epped pepper pippin powwow reefer revere rococo salsas seeded senses +sereneness settee sleeveless tattoo teeter teethe wedded weeded xxvii +i xxxvii 49
      Nice solution of the "variable length lookbehind" not allowed problem!

      But why \n instead of $ ? *

      edit

      I suppose it's necessary because the following assertion wouldn't be executed?

      update

      *) well, I just noticed the "@_\n@_" part where you are doubling the input, which explains the "\n" and some other confusion about pos.

      I agree, "too much cheating!" ;-p

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re^2: Puzzle Regex: Letter Frequency Arithmetic Sequence
by QM (Parson) on Oct 18, 2017 at 17:18 UTC
    ...sum the count of matches...

    There's always the brute force method of generating every possible regex, assuming arithmetic sequence starting at 1. For a max frequency of 6, this covers 21 letter words. It makes me wonder what the breaking point is of my machine.

    Note that in (3), only lengths that are triangular will do.

    I'll have to find some time to chase this -- perhaps tomorrow.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      I just remembered that Perl supports recursive regexes and relative back-references, so you might not be limited to a hard limit.

      Unfortunately I don't have the time to play with it.

      FWIW if using s///g was allowed you just needed a pattern which repeatedly deleted one character of each group, iff exactly one of them is unique.

      Your criterion was met, if the string is empty afterwards.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-26 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found