Circles and multiples
Weekly challenge 115 — 31 May 2021
Week 115: 31 May 2021
You are given $N, a list of positive single digit integers (0-9).
Write a script to find the largest multiple of 2 that can be formed from the list.
Example 1 Input: @N = (1, 0, 2, 6) Output: 6210 Example 2 Input: @N = (1, 4, 2, 8) Output: 8412 Example 3 Input: @N = (4, 1, 7, 6) Output: 7614
The answer to this is:
$s, which will
be the smallest even digit in the list
$s
$s
$sConveniently that can be done with a single regex.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2021-05-31 use utf8; # Week 115 - task 2 - Largest multiple use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; largest_multiple(1, 2, 3, 4, 5); largest_multiple(1, 0, 2, 6); largest_multiple(1, 4, 2, 8); largest_multiple(4, 1, 7, 6); largest_multiple(1, 3, 5, 7); largest_multiple(4, 8, 6, 2); my @x; push @x, int(rand(10)) for 1 .. 20; largest_multiple(@x); sub largest_multiple { my ($numbers); # initialise say qq[\nInput: (] . join(', ', @_) . ')'; # concatenate digits in descending order $numbers = join('', reverse sort {$a <=> $b} @_); # find smallest multiple of 2 and move it to the end if ($numbers =~ m|^([0-9]*)([02468])([0-9]*)|) { say qq[Output: $1$3$2]; } else { say qq[Output: no multiple of 2 in input]; } }
last updated 2026-03-29 — 8 lines of code
Input: (1, 2, 3, 4, 5) Output: 54312 Input: (1, 0, 2, 6) Output: 6210 Input: (1, 4, 2, 8) Output: 8412 Input: (4, 1, 7, 6) Output: 7614 Input: (1, 3, 5, 7) Output: no multiple of 2 in input Input: (4, 8, 6, 2) Output: 8642 Input: (7, 9, 0, 3, 4, 9, 7, 4, 6, 7, 8, 1, 5, 0, 7, 1, 3, 8, 8, 6) Output: 99888777766544331100
Any content of this website which has been created by Peter Campbell Smith is in the public domain