diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-23 18:39:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-23 18:39:16 +0100 |
| commit | acdd474f9a066d601b384c0e8bf044fb462f8503 (patch) | |
| tree | d5ea9454459d5b1da52f49f5cee738647e629efc | |
| parent | 267257709c625272c01b178ff6389303cd5a41c1 (diff) | |
| parent | 93d99055be7e6d642ff592950cd43449a06180da (diff) | |
| download | perlweeklychallenge-club-acdd474f9a066d601b384c0e8bf044fb462f8503.tar.gz perlweeklychallenge-club-acdd474f9a066d601b384c0e8bf044fb462f8503.tar.bz2 perlweeklychallenge-club-acdd474f9a066d601b384c0e8bf044fb462f8503.zip | |
Merge pull request #5981 from LubosKolouch/master
feat(challenge-161/lubos-kolouch/perl/ch-{1,2}.pl): Challenge 161 LK Perl Task 1 2
| -rw-r--r-- | challenge-161/lubos-kolouch/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-161/lubos-kolouch/perl/ch-2.pl | 57 | ||||
| -rw-r--r-- | challenge-161/lubos-kolouch/python/ch-1.py | 17 | ||||
| -rw-r--r-- | challenge-161/lubos-kolouch/python/ch-2.py | 52 |
4 files changed, 155 insertions, 0 deletions
diff --git a/challenge-161/lubos-kolouch/perl/ch-1.pl b/challenge-161/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5389c6dd2d --- /dev/null +++ b/challenge-161/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,29 @@ +package main; +use strict; +use warnings; + +# glory for best belly boy! + +sub is_abcd { + my $what = shift; + + return $what eq join( '', sort( split //, $what ) ); +} + +open my $file, '<', 'dictionary.txt'; + +while (<$file>) { + chomp; + + print "$_ is abcdrian \n" if is_abcd($_); +} +close $file; + +use Test::More; + +is( is_abcd('knotty'), 1 ); +is( is_abcd('knotts'), '' ); + +done_testing; + +1; diff --git a/challenge-161/lubos-kolouch/perl/ch-2.pl b/challenge-161/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ddde2dbfe0 --- /dev/null +++ b/challenge-161/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,57 @@ +package main; +use strict; +use warnings; + +my %seen_letters; +my @output; + +sub is_abcd { + my $what = shift; + + return $what eq join( '', sort( split //, $what ) ); +} + +sub process_word { + my $what = shift; + + my %this_run_letters = %seen_letters; + + for my $i ( split //, $what ) { + $this_run_letters{$i} = 1; + } + my $letters_count = scalar keys %this_run_letters; + + if ( scalar keys %seen_letters == $letters_count - 1 ) { + + # we added a letter + push @output, $_; + %seen_letters = %this_run_letters; + } + + return; +} + +sub get_abcdrian_pangram { + + # only abecederian words solving exactly one letter + open my $file, '<', 'dictionary.txt'; + + while (<$file>) { + chomp; + next unless is_abcd($_); + process_word($_); + return join( ' ', @output ) if scalar keys %seen_letters == 26; + } + close $file; + + return; +} +use Test::More; + +is( get_abcdrian_pangram(), + 'a ad adds ado ago ah ahoy all allot allow almost amp an art ax blot buy buzz cc cell chi deft envy jot knot qt' +); + +done_testing; + +1; diff --git a/challenge-161/lubos-kolouch/python/ch-1.py b/challenge-161/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..7627473c05 --- /dev/null +++ b/challenge-161/lubos-kolouch/python/ch-1.py @@ -0,0 +1,17 @@ +""" glory for best belly boy! """ + + +def is_abcd(what: str) -> bool: + """Check if the str is in the right order""" + + return what == "".join(sorted(what)) + + +with open("dictionary.txt") as in_file: + line = in_file.readline() + + if is_abcd(line): + print(f"{line} is abcdrian") + +assert is_abcd("knotty") is True +assert is_abcd("knotts") is False diff --git a/challenge-161/lubos-kolouch/python/ch-2.py b/challenge-161/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..7936fc9117 --- /dev/null +++ b/challenge-161/lubos-kolouch/python/ch-2.py @@ -0,0 +1,52 @@ +""" Challenge 161 LK Python """ + + +class Pangram: + """Challenge 161 Task 2""" + + def __init__(self) -> None: + self.seen_letters: dict = {} + self.output: list = [] + + @staticmethod + def is_abcd(what: str) -> bool: + """Check if the file is in the right order""" + return what == "".join(sorted(what)) + + def process_word(self, what: str) -> None: + """Check if the word matches""" + + this_run_letters = dict(self.seen_letters) + + for char in what: + this_run_letters[char] = 1 + + if len(self.seen_letters.keys()) == len(this_run_letters.keys()) - 1: + # we added a letter + self.output.append(what) + self.seen_letters = dict(this_run_letters) + + return + + def get_abcdrian_pangram(self) -> str: + """only abecederian words solving exactly one letter""" + with open("dictionary.txt") as in_file: + + while line := in_file.readline().strip(): + if not self.is_abcd(line): + continue + + self.process_word(line) + + # seen all letters? + if len(self.seen_letters.keys()) == 26: + return " ".join(self.output) + + return "" + + +pangram = Pangram() +assert ( + pangram.get_abcdrian_pangram() + == "a ad adds ado ago ah ahoy all allot allow almost amp an art ax blot buy buzz cc cell chi deft envy jot knot qt" +) |
