diff options
| author | vinodkk89 <vinodkk89@gmail.com> | 2025-05-05 16:28:41 +0530 |
|---|---|---|
| committer | vinodkk89 <vinodkk89@gmail.com> | 2025-05-05 16:28:41 +0530 |
| commit | f644b7a23e7fa4ffe75e92ddeee53532728d3f95 (patch) | |
| tree | 2f8ad8ebf6b728eddc93c558d148bbb76b214803 | |
| parent | 4865a9cce3c19c5a1def20017a083f17ef9a0809 (diff) | |
| download | perlweeklychallenge-club-f644b7a23e7fa4ffe75e92ddeee53532728d3f95.tar.gz perlweeklychallenge-club-f644b7a23e7fa4ffe75e92ddeee53532728d3f95.tar.bz2 perlweeklychallenge-club-f644b7a23e7fa4ffe75e92ddeee53532728d3f95.zip | |
Solutions for Challenge-320
| -rw-r--r-- | challenge-320/vinod-k/perl/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-320/vinod-k/perl/ch-2.pl | 55 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-320/vinod-k/perl/ch-1.pl b/challenge-320/vinod-k/perl/ch-1.pl new file mode 100644 index 0000000000..c161c29fd5 --- /dev/null +++ b/challenge-320/vinod-k/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl + +=com +You are given an array of integers. + +Write a script to return the maximum between the number of positive and negative integers. Zero is neither positive nor negative. + + +Example 1 +Input: @ints = (-3, -2, -1, 1, 2, 3) +Output: 3 + +There are 3 positive integers. +There are 3 negative integers. +The maximum between 3 and 3 is 3. + +Example 2 +Input: @ints = (-2, -1, 0, 0, 1) +Output: 2 + +There are 1 positive integers. +There are 2 negative integers. +The maximum between 2 and 1 is 2. + +Example 3 +Input: @ints = (1, 2, 3, 4) +Output: 4 + +There are 4 positive integers. +There are 0 negative integers. +The maximum between 4 and 0 is 4. +=cut + +use strict; +use warnings; +use Data::Dumper; +use List::Util qw/max/; + +my @array = qw/-2 -1 0 0 1/; + +my (@negative_nums, @positive_nums); + +foreach (@array){ + if ($_ > 0){ + push (@positive_nums, $_); + } elsif ($_ < 0) { + push (@negative_nums, $_); + } +} + +print "There are ".scalar(@positive_nums)." positive integers."; +print "\nThere are ".scalar(@negative_nums)." negative integers."; + +my $max = (scalar(@positive_nums), scalar(@negative_nums)); +print "\nThe maximum between ".scalar(@positive_nums)." and ".scalar(@negative_nums)." is ".$max."\n";
\ No newline at end of file diff --git a/challenge-320/vinod-k/perl/ch-2.pl b/challenge-320/vinod-k/perl/ch-2.pl new file mode 100644 index 0000000000..e19bcad5d3 --- /dev/null +++ b/challenge-320/vinod-k/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +=com +You are given an array of positive integers. + +Write a script to return the absolute difference between digit sum and element sum of the given array. + + +Example 1 +Input: @ints = (1, 23, 4, 5) +Output: 18 + +Element sum: 1 + 23 + 4 + 5 => 33 +Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +Absolute difference: | 33 - 15 | => 18 + +Example 2 +Input: @ints = (1, 2, 3, 4, 5) +Output: 0 + +Element sum: 1 + 2 + 3 + 4 + 5 => 15 +Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +Absolute difference: | 15 - 15 | => 0 + +Example 3 +Input: @ints = (1, 2, 34) +Output: 27 + +Element sum: 1 + 2 + 34 => 37 +Digit sum: 1 + 2 + 3 + 4 => 10 +Absolute difference: | 37 - 10 | => 27 +=cut + +use strict; +use warnings; + +my @array = (1, 23, 4, 5); + +my $sum = 0; +my $digit_sum = 0; + +foreach (@array){ + $sum += $_; + + my @inside_arr; + my $val = 0; + if ($_ > 9){ + @inside_arr = split('', $_); + foreach my $element (@inside_arr){ $digit_sum += $element; } + } else { + $digit_sum += $_; + } +} +print "Sum: $sum\n"; +print "Digit Sum: $digit_sum\n"; +print "Absolute difference: | ". $sum ." - ". $digit_sum ." | => ". ($sum - $digit_sum);
\ No newline at end of file |
