diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-17 23:27:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-17 23:27:20 +0100 |
| commit | ece1ce26167bd64733c4a96535d72f1f84855429 (patch) | |
| tree | a1da0a92c053e2efe36a87a86185138466388e59 | |
| parent | 5791f3cd997b180c5cfede0a95cb43639db9f341 (diff) | |
| parent | 06d804b406a1fa8c3767a038f6db69fea521fe22 (diff) | |
| download | perlweeklychallenge-club-ece1ce26167bd64733c4a96535d72f1f84855429.tar.gz perlweeklychallenge-club-ece1ce26167bd64733c4a96535d72f1f84855429.tar.bz2 perlweeklychallenge-club-ece1ce26167bd64733c4a96535d72f1f84855429.zip | |
Merge pull request #8716 from Solathian/branch-for-challenge-234
Added files
| -rw-r--r-- | challenge-234/solathian/perl/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-234/solathian/perl/ch-2.pl | 38 |
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-234/solathian/perl/ch-1.pl b/challenge-234/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..aa6c60e704 --- /dev/null +++ b/challenge-234/solathian/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!usr/bin/perl +use v5.38; + +# Challenge 234 - 1 - Common Characters +# You are given an array of words made up of alphabetic characters only. +# Write a script to return all alphabetic characters that show up in all words including duplicates. + + +words("cool", "lock", "cook"); # Output: ("c", "o") +words("bella", "label", "roller"); # Output: ("e", "l", "l") +words("java", "javascript", "julia"); # Output: ("j", "a") +words("java", "jjjjjavascript", "julia"); # Output: ("j", "a") + +sub words(@words) +{ + + my @result; + + + +OUTER: + for(my $i = ord('a'); $i <= ord('z'); $i++) + { + my $ch = chr $i; # the current character + + for(my $j = 0; $j < @words; $j++) + { + last OUTER if ( $words[$j] eq ""); # end if one words is empty already + next if ( $j == $i); # jump to next word, do not compare with ourself + next OUTER if( $words[$j] !~ m/$ch/); # jump to next char, if it does not match + + } + + # do stuff if we reached this + s/$ch// foreach(@words); # remove the common character + push @result, $ch; # store for result + redo; # redo since we might need to capture this character again + + } + + # print result + say join ",", sort @result; +} + diff --git a/challenge-234/solathian/perl/ch-2.pl b/challenge-234/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..da6a7f62bc --- /dev/null +++ b/challenge-234/solathian/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!usr/bin/perl +use v5.38; + +use builtin 'indexed'; +no warnings 'experimental'; +# Challenge 234 - 2 - Unequal Triplets + +# You are given an array of positive integers. +# Write a script to find the number of triplets (i, j, k) +# that satisfies num[i] != num[j], num[j] != num[k] and num[k] != num[i]. + + +uneqTrip(4, 4, 2, 4, 3); # Output 3 +uneqTrip(1, 1, 1, 1, 1); # Ouput: 0 +uneqTrip(4, 7, 1, 10, 7, 4, 1, 1); # Output: 28 + + + +sub uneqTrip( @list) +{ + my $count = 0; + OUTER: + foreach my($i, $val_i) (indexed @list) + { + foreach my($j, $val_j) (indexed @list) + { + # last if last if( $i == $j || $j == $k || $i == $k); + last if( $i == $j ); + foreach my($k, $val_k) (indexed @list) + { + last if( $j == $k ); + $count++ if( $val_i != $val_j and $val_j != $val_k and $val_k != $val_i); + } + } + } + + say $count; +}
\ No newline at end of file |
