aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-06-12 04:35:38 +0100
committerGitHub <noreply@github.com>2023-06-12 04:35:38 +0100
commitc921773c36903b2299125fbae8a8dc2d963cbb81 (patch)
treef377d49ce8640c812193295d639c58ddc31bc915
parentab1ca122042342027fd40f91019b82d98355505a (diff)
parent39d148e0729445579695a9a7b37ec9c30c74a1ac (diff)
downloadperlweeklychallenge-club-c921773c36903b2299125fbae8a8dc2d963cbb81.tar.gz
perlweeklychallenge-club-c921773c36903b2299125fbae8a8dc2d963cbb81.tar.bz2
perlweeklychallenge-club-c921773c36903b2299125fbae8a8dc2d963cbb81.zip
Merge pull request #8215 from Solathian/branch-for-challenge-220
Added files for challenge
-rw-r--r--challenge-220/solathian/perl/ch-1.pl48
-rw-r--r--challenge-220/solathian/perl/ch-2.pl55
2 files changed, 103 insertions, 0 deletions
diff --git a/challenge-220/solathian/perl/ch-1.pl b/challenge-220/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..e5f15780bc
--- /dev/null
+++ b/challenge-220/solathian/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!usr/bin/perl
+use v5.36;
+use builtin qw(indexed);
+no warnings 'experimental';
+
+# Challenge 220 - 1 - Common Characters
+# You are given a list of words.
+# Write a script to return the list of common characters (sorted alphabeticall) found in every word of the given list.
+
+cc("Perl", "Rust", "Rakur"); # r
+cc("love", "live", "leave"); # ("e", "l", "v")
+
+sub cc(@wordArray)
+{
+ my %resultHash;
+ my @results;
+
+ die("Not enough words are given") if( @wordArray <= 2);
+
+ foreach my ($index, $word) (indexed @wordArray)
+ {
+ foreach my $char ($word =~ /(.)/g)
+ {
+ $char = lc $char;
+
+ if( (not defined $resultHash{$char}) && ($index == 0))
+ {
+ $resultHash{$char} = 1;
+ }
+
+ # this way it will be only incremented once
+ elsif( defined($resultHash{$char}) && ($resultHash{$char} == $index) )
+ {
+ $resultHash{$char}++;
+ }
+
+ }
+
+ }
+
+ # collect and print the results
+ foreach my ($key, $value) (%resultHash)
+ {
+ push(@results, $key) if($value == @wordArray); # so it is in all of the words
+ }
+
+ say join(',', sort @results);
+} \ No newline at end of file
diff --git a/challenge-220/solathian/perl/ch-2.pl b/challenge-220/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..9313a277f5
--- /dev/null
+++ b/challenge-220/solathian/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!usr/bin/perl
+use v5.36;
+
+# Challenge 220 - 2 - Squareful
+use Algorithm::Combinatorics qw(permutations);
+use builtin qw(true false);
+
+no warnings 'experimental';
+
+# Challenge 220 - 1 - Squareful
+
+# You are given an array of integers, @ints.
+# An array is squareful if the sum of every pair of adjacent elements is a perfect square.
+# Write a script to find all the permutations of the given array that are squareful.
+
+squareful(1,17,8);
+squareful(2, 2, 2);
+
+sub squareful(@list)
+{
+ die if(@list < 2);
+
+ my @permutations = permutations(\@list);
+ my @last;
+
+ foreach my $permutation (permutations(\@list))
+ {
+ # check if the last permutation is the same as current one, to overcome issue from n, n, n arrays
+ if(@last)
+ {
+ next if(@last ~~ @$permutation);
+ }
+ @last = @$permutation;
+
+ my $okFlag = true;
+
+ for(my $i = 0; $i < (@$permutation -1); $i++)
+ {
+ my $sqrt = sqrt($permutation->[$i] + $permutation->[$i + 1]);
+
+
+ if( $sqrt != int($sqrt))
+ {
+ $okFlag = false;
+ last;
+ }
+ }
+
+ if($okFlag)
+ {
+ say join(',',@$permutation);
+ }
+
+ }
+} \ No newline at end of file