diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-03-17 14:36:21 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-03-17 14:36:21 +0000 |
| commit | 3b32b0fa6c8b0ce0b4516e0a71a8ae22f7efc54e (patch) | |
| tree | 4b4bbe9522291cafeade0ba583e812a3e6bd8d40 /challenge-260 | |
| parent | 7177fae93dcaab8a8838b3af1c450e95b70ff4af (diff) | |
| download | perlweeklychallenge-club-3b32b0fa6c8b0ce0b4516e0a71a8ae22f7efc54e.tar.gz perlweeklychallenge-club-3b32b0fa6c8b0ce0b4516e0a71a8ae22f7efc54e.tar.bz2 perlweeklychallenge-club-3b32b0fa6c8b0ce0b4516e0a71a8ae22f7efc54e.zip | |
- Added solutions by Lubos Kolouch.
- Added solutions by Adam Russell.
- Added solutions by Robert Ransbottom.
- Added solutions by Simon Green.
- Added blog post by Roger Bell_West.
- Added solutions by Wanderdoc.
Diffstat (limited to 'challenge-260')
| -rwxr-xr-x | challenge-260/wanderdoc/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-260/wanderdoc/perl/ch-2.pl | 46 |
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-260/wanderdoc/perl/ch-1.pl b/challenge-260/wanderdoc/perl/ch-1.pl new file mode 100755 index 0000000000..08d3bf89fb --- /dev/null +++ b/challenge-260/wanderdoc/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given an array of integers, @ints. Write a script to return 1 if the number of occurrences of each value in the given array is unique or 0 otherwise.
+Example 1 Input: @ints = (1,2,2,1,1,3) Output: 1
+The number 1 occurred 3 times.
+The number 2 occurred 2 times.
+The number 3 occurred 1 time.
+
+All occurrences are unique, therefore the output is 1.
+
+Example 2 Input: @ints = (1,2,3) Output: 0
+Example 3 Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) Output: 1
+=cut
+
+use Test2::V0;
+
+is(unique_occurences(1,2,2,1,1,3), 1, 'Example 1');
+is(unique_occurences(1,2,3), 0, , 'Example 2');
+is(unique_occurences(-2,0,1,-2,1,1,0,1,-2,9), 1, 'Example 3');
+done_testing();
+
+sub unique_occurences
+{
+ my @arr = @_;
+ my %values;
+ do { $values{$_}++ } for @arr;
+ my %unique;
+ @unique{values %values} = undef;
+ return (scalar keys %unique == scalar keys %values) ? 1 : 0;
+}
diff --git a/challenge-260/wanderdoc/perl/ch-2.pl b/challenge-260/wanderdoc/perl/ch-2.pl new file mode 100755 index 0000000000..9c6fc2dabd --- /dev/null +++ b/challenge-260/wanderdoc/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a word, $word. Write a script to compute the dictionary rank of the given word.
+
+Example 1 Input: $word = 'CAT' Output: 3
+All possible combinations of the letters: CAT, CTA, ATC, TCA, ACT, TAC
+Arrange them in alphabetical order: ACT, ATC, CAT, CTA, TAC, TCA
+CAT is the 3rd in the list. Therefore the dictionary rank of CAT is 3.
+
+
+Example 2 Input: $word = 'GOOGLE' Output: 88
+Example 3 Input: $word = 'SECRET' Output: 255
+=cut
+
+
+use Algorithm::Combinatorics qw(permutations);
+# Tuples are generated in lexicographic order, except in subsets().
+use Test2::V0;
+
+is(dictionary_rank('CAT'), 3, 'Example 1');
+is(dictionary_rank('GOOGLE'), 88, 'Example 2');
+is(dictionary_rank('SECRET'), 255, 'Example 3');
+done_testing();
+
+sub dictionary_rank
+{
+ my $word = $_[0];
+ my @data = sort { $a cmp $b } split(//, $word);
+ my $counter = 0;
+ my %seen;
+ my $iter = permutations(\@data);
+ while (my $c = $iter->next)
+ {
+ my $tuple = join('', @$c);
+ next if (exists $seen{$tuple});
+ $seen{$tuple} = undef;
+ $counter++;
+ if ( $tuple eq $word )
+ {
+ return $counter;
+ }
+ }
+}
|
