diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-12 17:22:46 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-12 17:22:46 +0000 |
| commit | 62cfb1fdb648a03cd7e1111dd26aa5081e91a693 (patch) | |
| tree | 87a34c2b8823caa2363299fa1f7c1c9832839ccc | |
| parent | 3b5b7c7d593b5882c64cecd5d00ae9fe736faaf4 (diff) | |
| parent | 92d7376ae47cb211e31049503c8ad34c48b74833 (diff) | |
| download | perlweeklychallenge-club-62cfb1fdb648a03cd7e1111dd26aa5081e91a693.tar.gz perlweeklychallenge-club-62cfb1fdb648a03cd7e1111dd26aa5081e91a693.tar.bz2 perlweeklychallenge-club-62cfb1fdb648a03cd7e1111dd26aa5081e91a693.zip | |
Merge pull request #9738 from pjcs00/wk260
Week 260
| -rw-r--r-- | challenge-260/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-260/peter-campbell-smith/perl/ch-1.pl | 45 | ||||
| -rwxr-xr-x | challenge-260/peter-campbell-smith/perl/ch-2.pl | 49 |
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-260/peter-campbell-smith/blog.txt b/challenge-260/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..b9471cd66c --- /dev/null +++ b/challenge-260/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/260 diff --git a/challenge-260/peter-campbell-smith/perl/ch-1.pl b/challenge-260/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..4571ca0520 --- /dev/null +++ b/challenge-260/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-03-11 +use utf8; # Week 260 - task 1 - Unique occurrences +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +unique_occurrences(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5); +unique_occurrences(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5); +unique_occurrences(1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5); + +my (@ints, $j); +for $j (0 .. 99) { + $ints[$j] = int(rand(21) - 10); +} +unique_occurrences(@ints); + +sub unique_occurrences { + + my (@ints, %occurs, $output, $reason, $value, %seen); + + # initialise + @ints = @_; + $output = 1; + $reason = ''; + say qq[\nInput: \@ints = (] . join(', ', @ints) . ')'; + + # count occurrences + $occurs{$_} ++ for @ints; + + # see if they are all 1 + for $value (keys %occurs) { + if (defined $seen{$occurs{$value}}) { + $output = 0; + $reason = qq[($value and $seen{$occurs{$value}} both occur $occurs{$value} times)]; + last; + } + $seen{$occurs{$value}} = $value; + } + + # show the answer + say qq[Output: $output $reason]; +} diff --git a/challenge-260/peter-campbell-smith/perl/ch-2.pl b/challenge-260/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..351f12c8c4 --- /dev/null +++ b/challenge-260/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-03-11 +use utf8; # Week 260 - task 2 - Dictionary rank +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +use Algorithm::Combinatorics 'permutations'; +use Time::HiRes 'time'; + +dictionary_rank('CAT'); +dictionary_rank('GOOGLE');; +dictionary_rank('SECRET'); +dictionary_rank('MARE'); +dictionary_rank('ZYMURGY'); +dictionary_rank('PENGUINS'); +dictionary_rank('CAMBRIDGE'); +dictionary_rank('DICTIONARY'); +dictionary_rank('FABRICATING'); + +sub dictionary_rank { + + my ($word, @letters, $iter, $test, $count, $this, %seen, $start); + + # initialise + $start = time; + $word = uc($_[0]); + @letters = split('', $word); + @letters = sort @letters; + + # iterate over permutations in lexicographic order + $iter = permutations(\@letters); + while ($test = $iter->next) { + $this = join('', @$test); + + # eliminate duplicates + next if $seen{$this}; + $seen{$this} = 1; + + # count them until we find $word + $count ++; + last if $this eq $word; + } + + say qq[\nInput: \$word = '$word']; + say sprintf(qq[Output: $count (%.2f seconds)], time - $start); +} |
