Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Puzzle Regex: Letter Frequency Arithmetic Sequence

by choroba (Cardinal)
on Oct 17, 2017 at 15:31 UTC ( [id://1201504]=note: print w/replies, xml ) Need Help??


in reply to Puzzle Regex: Letter Frequency Arithmetic Sequence

Not sure about regexes, but counting frequencies means hashes.
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $dict = '/var/lib/dict/words'; # YMMV sub each_char_has_different_freq { my ($r, $f) = @_; keys %$r == keys %$f } sub frequencies_form_a_sequence { my ($r) = @_; keys %$r == grep $r->{$_}, 1 .. keys %$r } my @linenlessnesses; open my $IN, '<', $dict or die $!; while (<$IN>) { chomp; next if /\W/; my %freq; $freq{$_}++ for split //; my %r = reverse %freq; push @linenlessnesses, $_ if each_char_has_different_freq(\%r, \%freq) && frequencies_form_a_sequence(\%r); } say for sort { length $a <=> length $b } @linenlessnesses;

Interesting output (the longest words) on my machine:

deadheaded evennesses keennesses peppertree rememberer restresses sanenesses sereneness sleeveless sussararas susurruses

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

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

    "counting frequencies means hashes" - not always :)

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1201500 use strict; use warnings; my $file = '/usr/share/dict/words'; # YMMV open my $fh, '<', $file or die "$! opening $file"; while(<$fh>) { /^[a-z]{6,}$/i or next; my $word = $_; my @counts; $counts[ s/$&//gi ] .= $& while s/.//; grep(!$_ || /../, @counts) or print $word; }
Re^2: Puzzle Regex: Letter Frequency Arithmetic Sequence
by vagabonding electron (Curate) on Oct 24, 2017 at 14:16 UTC

    I played a lot with your solution to understand it well, so first of all - thank you, choroba.

    Just my two cents: your function frequencies_form_a_sequence assumes that the frequencies start with one. The words with letter sequencies 2,3,4 etc. will not be selected then.

    Should it not be in the following form (min, max and all are from List::Util and List::MoreUtils):

    sub frequencies_form_a_sequence { my ($r) = @_; my $min = min keys %$r; return 0 if $min == 1; # Just for test. my $max = max keys %$r; return 0 if $min == $max; # Probably not a sequence. my $bool = all { defined $r->{$_} } $min .. $max; }

    I find the following words then (cannot pretend to know what they mean though :) ):

    addda ajaja alala anana arara cocco essee esses igigi nanna pappa peepe reree susus taata tatta ululu xxiii deedeed sasararas sassarara
      Why not, before the update, the challenge wasn't exactly specified, so I understood it as 1 .. n.

      I'd just use exists instead of defined in the all block.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found