diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-25 19:38:44 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-25 19:38:44 +0000 |
| commit | bd0647b5bc866ce70cfbfd1cd26142892d375ed9 (patch) | |
| tree | 08acff85245c0f3d8a92da3d607caf684a8fcf36 | |
| parent | 62350c79706716758300dc0c7cacdc76331a6c4e (diff) | |
| parent | fc65dff1333ad5f377cbe33af6373490e4f629b5 (diff) | |
| download | perlweeklychallenge-club-bd0647b5bc866ce70cfbfd1cd26142892d375ed9.tar.gz perlweeklychallenge-club-bd0647b5bc866ce70cfbfd1cd26142892d375ed9.tar.bz2 perlweeklychallenge-club-bd0647b5bc866ce70cfbfd1cd26142892d375ed9.zip | |
Merge pull request #7625 from Solathian/branch-for-challenge-205
Adding file for challenge 205
| -rw-r--r-- | challenge-205/solathian/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-205/solathian/perl/ch-2.pl | 36 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-205/solathian/perl/ch-1.pl b/challenge-205/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..315ed16cbb --- /dev/null +++ b/challenge-205/solathian/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!usr/bin/perl +use v5.36; + +# Challange 205 - 1 - Third Highest +# You are given an array of integers. +# Write a script to find out the Third Highest if found otherwise return the maximum. + +thirdHighest(5,4,4,3); # 3 # First highest is 5. Second highest is 4. Third highest is 3. +thirdHighest(5,6); # 6 # First highest is 6. Second highest is 5. Third highest is missing, so maximum is returned. +thirdHighest(5,3,4); # 3 # First highest is 5. Second highest is 4. Third highest is 3. +thirdHighest(); # Array is empty +thirdHighest(5,5,5); +thirdHighest(5,5,4,4,3,3); + +sub trimArray($arrRef) # remove duplicated elements from the array +{ + my @trimmedArr; + + if(@$arrRef > 0) + { + @$arrRef = sort @$arrRef; + + my $last = $arrRef->[0]; + push(@trimmedArr, $last); + + for(my $i = 1; $i < @$arrRef; $i++) + { + if($last != $arrRef->[$i]) + { + $last = $arrRef->[$i]; + push(@trimmedArr, $last); + + } + } + } + + return @trimmedArr; +} + +sub thirdHighest(@array) +{ + @array = trimArray(\@array); + + if(@array == 0) {say "Array is empty."} + elsif(@array < 3) {say $array[-1]} + else {say $array[-3]} +}
\ No newline at end of file diff --git a/challenge-205/solathian/perl/ch-2.pl b/challenge-205/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..2bfcb001c0 --- /dev/null +++ b/challenge-205/solathian/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!usr/bin/perl +use v5.36; + +# Challange 205 - 2 - Maximum XOR +# You are given an array of integers. +# Write a script to find the highest value obtained by XORing any two distinct members of the array. + + +# Input: @array = (1,2,3,4,5,6,7) +# Output: 7 +# The maximum result of 1 xor 6 = 7. + +# Input: @array = (2,4,1,3) +# Output: 7 +# The maximum result of 4 xor 3 = 7. + +# Input: @array = (10,5,7,12,8) +# Output: 15 +# The maximum result of 10 xor 5 = 15. + +maXor(1,2,3,4,5,6,7); # 7 +maXor(2,4,1,3); # 7 +maXor(10,5,7,12,8); # 15 + +sub maXor(@array) +{ + my $max = 0; + + foreach my $value (@array) + { + map{ $max = $_ ^ $value if(($_ ^ $value) > $max) } @array; + + } + + say $max; +}
\ No newline at end of file |
