diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-07 08:56:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-07 08:56:40 +0100 |
| commit | f78f6d034e61e0fb3838e632b3914344cbe17754 (patch) | |
| tree | f7a79fdaeaf4ba0302431d288ed908b9f9401978 | |
| parent | 604f5214fd7a0076bf27cc78ec6117556a0cec51 (diff) | |
| parent | d4e0892c2069c2b6d8317938c1d8704a47fcb8ec (diff) | |
| download | perlweeklychallenge-club-f78f6d034e61e0fb3838e632b3914344cbe17754.tar.gz perlweeklychallenge-club-f78f6d034e61e0fb3838e632b3914344cbe17754.tar.bz2 perlweeklychallenge-club-f78f6d034e61e0fb3838e632b3914344cbe17754.zip | |
Merge pull request #8331 from ash/ash-224
ash 224, task 1
| -rw-r--r-- | challenge-224/ash/perl/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-224/ash/perl/ch-1.pl | 98 |
2 files changed, 99 insertions, 0 deletions
diff --git a/challenge-224/ash/perl/blog.txt b/challenge-224/ash/perl/blog.txt new file mode 100644 index 0000000000..8bcec81e08 --- /dev/null +++ b/challenge-224/ash/perl/blog.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2023/07/06/built-in-classes-in-perl-5-38/ diff --git a/challenge-224/ash/perl/ch-1.pl b/challenge-224/ash/perl/ch-1.pl new file mode 100644 index 0000000000..f4a8dede62 --- /dev/null +++ b/challenge-224/ash/perl/ch-1.pl @@ -0,0 +1,98 @@ +# A solution of Task 1 of the Weekly Challenge 224. +# https://theweeklychallenge.org/blog/perl-weekly-challenge-224/#TASK1 + +# Comments to the code: https://andrewshitov.com/2023/07/06/built-in-classes-in-perl-5-38/ + +# The solution demonstrates the use of the new Perl's feature, built-in native +# classes available since Perl 5.38: https://perldoc.perl.org/perlclass. + +# The below program is intentionnaly over-engineered to try as many new +# features as possible. Do not consider it as a practice to follow +# (but the algorithm in the 'SpecialNotes::solve' method is a good thing to use). + +# How to run it: +# $ perl5.38.0 ch-1.pl +# +# Expected output: +# 'abc' vs. 'xyz' => false +# 'scriptinglanguage' vs. 'perl' => true +# 'aabbcc' vs. 'abc' => true + +use v5.38; + +use feature 'class'; +no warnings 'experimental::class'; + +class MyString { + field $string :param; + + method string { + return $string; + } + + method chars { + return split //, $string; + } + + method count_chars { + my %count; + + for my $char ($self->chars) { + $count{$char}++; + } + + return %count; + } + + method compare($other_string) { + return $string eq $other_string->string; + } + + method len { + return length $string; + } +} + +class SpecialNotes { + field $source :param; + field $target :param; + + field $source_string; + field $target_string; + + ADJUST { + $source_string = MyString->new(string => $source); + $target_string = MyString->new(string => $target); + } + + method dump { + return "'$source' vs. '$target'"; + } + + method solve { + return 'true' if $source_string->compare($target_string); + + my %source_chars = $source_string->count_chars; + my %target_chars = $target_string->count_chars; + + my $count = 0; + for my $char (keys %target_chars) { + last unless exists $source_chars{$char}; + last if $source_chars{$char} < $target_chars{$char}; + $count++; + } + + return $count == $target_string->len ? 'true' : 'false'; + } +} + +my @tests = ( + [abc => 'xyz'], + [scriptinglanguage => 'perl'], + [aabbcc => 'abc'], +); + +for my $test (@tests) { + my $note = SpecialNotes->new(source => $test->[0], target => $test->[1]); + say $note->dump . " => " . $note->solve; +} |
