Task 1: 3-digits Even
You are given a list (3 or more) of positive integers, @ints.
Write a script to return all even 3-digit 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)
Logic
Count digit frequencies. For each valid hundreds digit (1-9), decrement its count, then for each valid tens digit (0-9), decrement its count, then for each even units digit (0,2,4,6,8), check if available. Restore counts after each iteration. Collect results in a set for uniqueness, then sort.
Perl Solution
ch-1.pl
#!/usr/bin/env perl
use v5.38;
use warnings;
use feature 'signatures';
no warnings 'experimental::signatures';
use List::Util qw(uniq);
sub three_digits_even ($ints) {
my %count;
$count{$_}++ for @$ints;
my @out;
for my $a (1 .. 9) {
next if !$count{$a};
$count{$a}--;
for my $b (0 .. 9) {
next if !$count{$b};
$count{$b}--;
for my $c (0, 2, 4, 6, 8) {
next if !$count{$c};
push @out, 100*$a + 10*$b + $c;
}
$count{$b}++;
}
$count{$a}++;
}
@out = sort { $a <=> $b } uniq(@out);
return \@out;
}
Python Solution
ch-1.py
from collections import Counter
def three_digits_even(ints: list[int]) -> list[int]:
counts = Counter(ints)
out: set[int] = set()
for a in range(1, 10):
if counts[a] == 0: continue
counts[a] -= 1
for b in range(10):
if counts[b] == 0: continue
counts[b] -= 1
for c in (0, 2, 4, 6, 8):
if counts[c] > 0:
out.add(100*a + 10*b + c)
counts[b] += 1
counts[a] += 1
return sorted(out)
Task 2: Delete and Earn
You are given an array of integers, @ints.
Pick any element to earn its points, then delete all elements equal to (value - 1) and (value + 1). Repeat to maximize total points.
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.
Delete a 3 again to earn 3 points.
Delete a 3 once more to earn 3 points.
Logic
This is the classic "House Robber" DP over value space. For each value v, compute total gain = v * count[v]. Then dp[v] = max(dp[v-1], dp[v-2] + gain[v]). Either skip value v (take dp[v-1]) or take all of v (gain[v] + dp[v-2], since v-1 is then unavailable).
Perl Solution
ch-2.pl
#!/usr/bin/env perl
use v5.38;
use warnings;
use feature 'signatures';
no warnings 'experimental::signatures';
use List::Util qw(max);
sub delete_and_earn ($ints) {
return 0 if !@$ints;
my %count;
$count{$_}++ for @$ints;
my $prev2 = 0;
my $prev1 = 0;
for my $v (0 .. max(keys %count)) {
my $gain = ($count{$v} // 0) * $v;
my $cur = $prev1 > ($prev2 + $gain) ? $prev1 : ($prev2 + $gain);
($prev2, $prev1) = ($prev1, $cur);
}
return $prev1;
}
Python Solution
ch-2.py
from collections import Counter
def delete_and_earn(ints: list[int]) -> int:
if not ints:
return 0
counts = Counter(ints)
max_v = max(counts)
prev2 = prev1 = 0
for v in range(max_v + 1):
gain = v * counts.get(v, 0)
current = max(prev1, prev2 + gain)
prev2, prev1 = prev1, current
return prev1