diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-01-20 01:06:46 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-20 01:06:46 +0000 |
| commit | fae2dad056875bb0545569824519223d2c829d65 (patch) | |
| tree | da48387286bbb195a388cf9b4eebe300d1f2b658 | |
| parent | 78814310e5668b18238cfe948732b8f448c4c308 (diff) | |
| parent | 586238bee9e48b41a4dd857cbcf2cb945c6a5ebc (diff) | |
| download | perlweeklychallenge-club-fae2dad056875bb0545569824519223d2c829d65.tar.gz perlweeklychallenge-club-fae2dad056875bb0545569824519223d2c829d65.tar.bz2 perlweeklychallenge-club-fae2dad056875bb0545569824519223d2c829d65.zip | |
Merge pull request #11462 from DevSanti12/master
Added perl challenges for 302
| -rw-r--r-- | challenge-302/santiago-leyva/perl/ch-01.pl | 80 | ||||
| -rw-r--r-- | challenge-302/santiago-leyva/perl/ch-02.pl | 50 | ||||
| -rw-r--r-- | challenge-303/santiago-leyva/perl/ch-01.pl | 66 | ||||
| -rw-r--r-- | challenge-303/santiago-leyva/perl/ch-02.pl | 63 |
4 files changed, 259 insertions, 0 deletions
diff --git a/challenge-302/santiago-leyva/perl/ch-01.pl b/challenge-302/santiago-leyva/perl/ch-01.pl new file mode 100644 index 0000000000..b2f58d3d84 --- /dev/null +++ b/challenge-302/santiago-leyva/perl/ch-01.pl @@ -0,0 +1,80 @@ +=begin +You are given an array of binary strings, @str, and two integers, $x and $y. + +Write a script to return the size of the largest subset of @str such that there are at most $x 0’s and $y 1’s in the subset. + +A set m is a subset of n if all elements of m are also elements of n. +Example 1 +Input: @str = ("10", "0001", "111001", "1", "0") + $x = 5 + $y = 3 +Output: 4 + +The largest subset with at most five 0's and three 1's: +("10", "0001", "1", "0") +Example 2 +Input: @str = ("10", "1", "0") + $x = 1 + $y = 1 +Output: 2 + +The largest subset with at most one 0's and one 1's: +("1", "0") +=cut + +use strict; +use warnings; + +sub findMaxForm { + my ($strs, $m, $n) = @_; + + # Get the size of the strs array + my $sz = scalar @$strs; + + # Initialize the DP table with 3D array + my @f; + for my $i (0..$sz) { + for my $j (0..$m) { + for my $k (0..$n) { + $f[$i][$j][$k] = 0; + } + } + } + + # Loop over all strings + for my $i (1..$sz) { + my $s = $strs->[$i - 1]; # Get the i-th string (1-indexed in loop) + my $a = ($s =~ tr/0//); # Count of '0's + my $b = ($s =~ tr/1//); # Count of '1's + + # Update the DP table + for my $j (0..$m) { + for my $k (0..$n) { + $f[$i][$j][$k] = $f[$i - 1][$j][$k]; # Not including the current string + if ($j >= $a && $k >= $b) { + # Include the current string if we have enough 0's and 1's + $f[$i][$j][$k] = max($f[$i][$j][$k], $f[$i - 1][$j - $a][$k - $b] + 1); + } + } + } + } + + return $f[$sz][$m][$n]; # Return the result stored in the bottom-right corner of the DP table +} + +# Helper function to get the maximum of two values +sub max { + my ($a, $b) = @_; + return $a > $b ? $a : $b; +} + +# Example usage +my @str1 = ("10", "0001", "111001", "1", "0"); +my $m1 = 5; +my $n1 = 3; +print findMaxForm(\@str1, $m1, $n1), "\n"; # Expected Output: 4 + +my @str2 = ("10", "1", "0"); +my $m2 = 1; +my $n2 = 1; +print findMaxForm(\@str2, $m2, $n2), "\n"; # Expected Output: 2
\ No newline at end of file diff --git a/challenge-302/santiago-leyva/perl/ch-02.pl b/challenge-302/santiago-leyva/perl/ch-02.pl new file mode 100644 index 0000000000..4f1102216c --- /dev/null +++ b/challenge-302/santiago-leyva/perl/ch-02.pl @@ -0,0 +1,50 @@ +=begin +You are given an array of integers, @ints. + +Write a script to find the minimum positive start value such that step by step sum is never less than one. + +Example 1 +Input: @ints = (-3, 2, -3, 4, 2) +Output: 5 + +For start value 5. +5 + (-3) = 2 +2 + (+2) = 4 +4 + (-3) = 1 +1 + (+4) = 5 +5 + (+2) = 7 +Example 2 +Input: @ints = (1, 2) +Output: 1 +Example 3 +Input: @ints = (1, -2, -3) +Output: 5 +=cut + +use strict; +use warnings; +use List::Util qw(min); + +my @nums = ([-3, 2, -3, 4, 2],[1, 2],[1, -2, -3]); + +foreach(@nums){ + my @arr = @$_; + my $result = findStart(\@arr); + print "For ",join(" ",@arr)," the start value is -> $result \n"; +} + +sub findStart { + my @A = @$_; + + my $s = 0; # Initialize the cumulative sum + my $t = 'inf'; # Initialize the minimum sum encountered + + for my $i (@A) { + $s += $i; # Update the cumulative sum + #print "$s\n"; + $t = min($t, $s); # Track the minimum cumulative sum + print "$t\n"; + } + + return $t < 0 ? 1 - $t : 1; # Return max(1, 1 - minimum cumulative sum) +}
\ No newline at end of file diff --git a/challenge-303/santiago-leyva/perl/ch-01.pl b/challenge-303/santiago-leyva/perl/ch-01.pl new file mode 100644 index 0000000000..a96ccbfee9 --- /dev/null +++ b/challenge-303/santiago-leyva/perl/ch-01.pl @@ -0,0 +1,66 @@ +=begin +You are given a list (3 or more) of positive integers, @ints. + +Write a script to return all even 3-digits integers that can be formed using the integers in the given list. + +Example 1 +Input: @ints = (2, 1, 3, 0) +Output: (102, 120, 130, 132, 210, 230, 302, 310, 312, 320) +Example 2 +Input: @ints = (2, 2, 8, 8, 2) +Output: (222, 228, 282, 288, 822, 828, 882) +=cut + +use strict; +#use warnings; +use List::Util 'sum'; +use Data::Dumper; + +my @nums = ([2, 1, 3, 0],[2, 2, 8, 8, 2]); + +foreach(@nums){ + my @arr = @$_; + my @result = findCombinations(\@arr); + print "For ",join(" ",@arr)," the combinations are value is -> ",join(" ",@result)," \n"; +} + +sub findCombinations { + + + my ($digits) = @_; + + # Count the frequency of each digit in the digits list + my %cnt; + $cnt{$_}++ for @$digits; + + my @ans; + + # Iterate through all 3-digit even numbers between 100 and 999 + for my $x (100..999) { + next unless $x % 2 == 0; # Skip odd numbers + + my %cnt1; + my $y = $x; + + # Count the frequency of digits in the current number x + while ($y) { + my $digit = $y % 10; + $cnt1{$digit}++; + $y = int($y / 10); + } + + # Check if we can form this number from the available digits + my $valid = 1; + for my $i (0..9) { + if ($cnt{$i} < $cnt1{$i}) { + $valid = 0; + last; + } + } + + # If valid, add to the result + push @ans, $x if $valid; + } + + return @ans; +}
\ No newline at end of file diff --git a/challenge-303/santiago-leyva/perl/ch-02.pl b/challenge-303/santiago-leyva/perl/ch-02.pl new file mode 100644 index 0000000000..2848d8360f --- /dev/null +++ b/challenge-303/santiago-leyva/perl/ch-02.pl @@ -0,0 +1,63 @@ +=begin +You are given an array of integers, @ints. + +Write a script to return the maximum number of points you can earn by applying the following operation some number of times. + +Pick any ints[i] and delete it to earn ints[i] points. +Afterwards, you must delete every element equal to ints[i] - 1 +and every element equal to ints[i] + 1. +Example 1 +Input: @ints = (3, 4, 2) +Output: 6 + +Delete 4 to earn 4 points, consequently, 3 is also deleted. +Finally delete 2 to earn 2 points. +Example 2 +Input: @ints = (2, 2, 3, 3, 3, 4) +Output: 9 + +Delete a 3 to earn 3 points. All 2's and 4's are also deleted too. +Delete a 3 again to earn 3 points. +Delete a 3 once more to earn 3 points. +=cut + +use strict; +use warnings; + +use strict; +use warnings; + +sub deleteAndEarn { + my ($nums) = @_; + + my $mx = -1; + foreach my $num (@$nums) { + $mx = $num if $num > $mx; + } + + my %total; + foreach my $num (@$nums) { + $total{$num} += $num; + } + + my $first = 0; + my $second = 0; + + # Iterate through the numbers in ascending order (sorted keys of the hash) + my @array = keys %total; + foreach my $i (sort { $a <=> $b } @array) { + # If the current number is adjacent to the previous one, we can't take both + my $cur = $first + $total{$i} > $second ? $first + $total{$i} : $second; + $first = $second; + $second = $cur; + } + + return $second; +} + +# Example usage +my @nums = (3, 4, 2); +print deleteAndEarn(\@nums), "\n"; # Expected output: 6 + +my @nums2 = (2, 2, 3, 3, 3, 4); +print deleteAndEarn(\@nums2), "\n"; # Expected output: 9
\ No newline at end of file |
