diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-24 11:14:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-24 11:14:49 +0100 |
| commit | 14f4b86a7bdc6c065a07cdc25ccc0281f9313d1f (patch) | |
| tree | 926ab5f543e29be25c6a6e22176d3d0763c84b5d /challenge-161 | |
| parent | 52db9b6bbb9b577fa5cfc9d886afeca13c7bfbed (diff) | |
| parent | 10625f4cbc0293a89ea5e60a615d91817bfbdb5a (diff) | |
| download | perlweeklychallenge-club-14f4b86a7bdc6c065a07cdc25ccc0281f9313d1f.tar.gz perlweeklychallenge-club-14f4b86a7bdc6c065a07cdc25ccc0281f9313d1f.tar.bz2 perlweeklychallenge-club-14f4b86a7bdc6c065a07cdc25ccc0281f9313d1f.zip | |
Merge pull request #5987 from Hiranyaloka/ch-161
ch-161 raku Abecedarian Words and Pangrams
Diffstat (limited to 'challenge-161')
| -rw-r--r-- | challenge-161/rick-bychowski/README | 1 | ||||
| -rwxr-xr-x | challenge-161/rick-bychowski/raku/task-1.raku | 31 | ||||
| -rwxr-xr-x | challenge-161/rick-bychowski/raku/task-2.raku | 62 |
3 files changed, 94 insertions, 0 deletions
diff --git a/challenge-161/rick-bychowski/README b/challenge-161/rick-bychowski/README new file mode 100644 index 0000000000..945c8663e9 --- /dev/null +++ b/challenge-161/rick-bychowski/README @@ -0,0 +1 @@ +Solution by Rick Bychowski diff --git a/challenge-161/rick-bychowski/raku/task-1.raku b/challenge-161/rick-bychowski/raku/task-1.raku new file mode 100755 index 0000000000..ea8c864156 --- /dev/null +++ b/challenge-161/rick-bychowski/raku/task-1.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku +# No errs? My first effort is all below, begin beers now. Hoot! + +use HTTP::UserAgent; +use Data::Dump; + +my $ua = HTTP::UserAgent.new; +$ua.timeout = 5; +my $url = "https://raw.githubusercontent.com/manwar/perlweeklychallenge-club/master/data/dictionary.txt"; +my $response = $ua.get($url); +my $dict = do with $response { + .is-success ?? .content !! die .status-line +} + +my @abcd = (); + +for $dict.lines -> $word { + next unless $word ~~ /^<[a .. z]>**2..*$/; + my $dorw = $word.split('', :skip-empty).sort.join(''); + @abcd.push($word) if $word eq $dorw; +} + +sub print-cols(@wordlist) { + for @wordlist -> $a, $b='', $c='', $d='', $e='', $f='', $g='', $h='' { + printf "%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", + $a, $b, $c, $d, $e, $f, $g, $h; + } +} + +print-cols(reverse sort &chars, @abcd); + diff --git a/challenge-161/rick-bychowski/raku/task-2.raku b/challenge-161/rick-bychowski/raku/task-2.raku new file mode 100755 index 0000000000..61e1545b35 --- /dev/null +++ b/challenge-161/rick-bychowski/raku/task-2.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env raku + +sub MAIN( + Str $url = "https://raw.githubusercontent.com/manwar/perlweeklychallenge-club/master/data/dictionary.txt", + Int :$min = 3, + Int :$max = 5 +) { + my $dict = get-dict( $url ); + my %words-grouped-by-letter = group-words-by-letter( $dict, $min, $max ); + say find-panagram( %words-grouped-by-letter); +} + +sub get-dict( Str $url ) returns Str { + use HTTP::UserAgent; + my $ua = HTTP::UserAgent.new; + $ua.timeout = 5; + my $response = $ua.get($url); + my $dict = do with $response { + .is-success ?? .content !! die .status-line + } +} + +sub group-words-by-letter( + Str $dict, + Int $min, + Int $max +) returns Hash { + my %words-grouped-by-letter; + for $dict.lines -> $word { + next unless $min <= $word.chars <= $max; + my %seen-letter; + for $word.split('', :skip-empty) -> $w { + next if defined %seen-letter{$w}; + if defined %words-grouped-by-letter{$w} { + %words-grouped-by-letter{$w}.push: $word; + } else { + %words-grouped-by-letter{$w} = [$word]; + } + %seen-letter{$w} = 1; + } + } + return %words-grouped-by-letter +} + +sub find-panagram( + %words-grouped-by-letter +) returns Array { + my @panagram; + my %have-letter; + for 'a' ... 'z' -> $a { + next if defined %have-letter{$a}; + for %words-grouped-by-letter{$a} -> @a-words { + for @a-words.pick -> $word { + for $word.split('', :skip-empty ) -> $w { + %have-letter{$w} = 1; + } + @panagram.push: $word; + } + } + } + return @panagram; +} |
