diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-26 15:26:59 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-26 15:26:59 +0100 |
| commit | a781d76bd0b7823a127506b12ed75a372284fb78 (patch) | |
| tree | 073ccc49342a27d031a6789d05545b257ad65f8b | |
| parent | 707b0c338d30768c28e61631c27e2e29ccc75953 (diff) | |
| download | perlweeklychallenge-club-a781d76bd0b7823a127506b12ed75a372284fb78.tar.gz perlweeklychallenge-club-a781d76bd0b7823a127506b12ed75a372284fb78.tar.bz2 perlweeklychallenge-club-a781d76bd0b7823a127506b12ed75a372284fb78.zip | |
Add Perl solution to challenge 260
| -rw-r--r-- | challenge-260/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-260/paulo-custodio/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-260/paulo-custodio/perl/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-260/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-260/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 129 insertions, 0 deletions
diff --git a/challenge-260/paulo-custodio/Makefile b/challenge-260/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-260/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-260/paulo-custodio/perl/ch-1.pl b/challenge-260/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..9486b72fc7 --- /dev/null +++ b/challenge-260/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# Challenge 260 +# +# Task 1: Unique Occurrences +# Submitted by: Mohammad Sajid Anwar +# +# 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 + +use Modern::Perl; + +my @ints = @ARGV; +say uniq_occurences(@ints); + +sub uniq_occurences { + my(@ints) = @_; + my %count; + $count{$_}++ for @ints; + my %uniq; + for (values %count) { + return 0 if $uniq{$_}++; + } + return 1; +} diff --git a/challenge-260/paulo-custodio/perl/ch-2.pl b/challenge-260/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b256c5e878 --- /dev/null +++ b/challenge-260/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# Challenge 260 +# +# Task 2: Dictionary Rank +# Submitted by: Mark Anderson +# +# 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 + +use Modern::Perl; +use Algorithm::Combinatorics qw(permutations); +use List::Util 'uniq'; +use List::MoreUtils 'onlyidx'; + +say dictionary_rank(shift // ""); + +sub dictionary_rank { + my($word) = @_; + return + 1+(onlyidx {$_ eq $word} + sort {$a cmp $b} + uniq + map {join '', @$_} + permutations([split //, $word]))[0]; +} diff --git a/challenge-260/paulo-custodio/t/test-1.yaml b/challenge-260/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..afed95bb2a --- /dev/null +++ b/challenge-260/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 2 1 1 3 + input: + output: 1 +- setup: + cleanup: + args: 1 2 3 + input: + output: 0 +- setup: + cleanup: + args: -2 0 1 -2 1 1 0 1 -2 9 + input: + output: 1 diff --git a/challenge-260/paulo-custodio/t/test-2.yaml b/challenge-260/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..035ac3f62a --- /dev/null +++ b/challenge-260/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: CAT + input: + output: 3 +- setup: + cleanup: + args: GOOGLE + input: + output: 88 +- setup: + cleanup: + args: SECRET + input: + output: 255 |
