diff options
| author | DevSanti12 <santiagoleyva2013@gmail.com> | 2025-01-19 19:39:15 -0500 |
|---|---|---|
| committer | DevSanti12 <santiagoleyva2013@gmail.com> | 2025-01-19 19:39:15 -0500 |
| commit | 586238bee9e48b41a4dd857cbcf2cb945c6a5ebc (patch) | |
| tree | 7339faf69ff2624d15ca7490d1d1025e5341e4ed | |
| parent | 72cf87713e575f50e347d79f688476f4a105ab5f (diff) | |
| download | perlweeklychallenge-club-586238bee9e48b41a4dd857cbcf2cb945c6a5ebc.tar.gz perlweeklychallenge-club-586238bee9e48b41a4dd857cbcf2cb945c6a5ebc.tar.bz2 perlweeklychallenge-club-586238bee9e48b41a4dd857cbcf2cb945c6a5ebc.zip | |
Added perl challenges for 303
| -rw-r--r-- | challenge-303/santiago-leyva/perl/ch-01.pl | 66 | ||||
| -rw-r--r-- | challenge-303/santiago-leyva/perl/ch-02.pl | 63 |
2 files changed, 129 insertions, 0 deletions
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 |
