Camel
Peter
Peter Campbell Smith

Bigger, big and little

Weekly challenge 368 — 6 April 2026

Week 368: 6 Apr 2026

Task 1

Task — Make it bigger

You are given a given a string which is a number and a character which is a digit.

Write a script to remove exactly one occurrence of the digit from the string that results in the maximum value of the string.

Examples


Example 1
Input: $str = '15456', $char = '5'
Output: '1546'
Removing the second '5' is better because the digit 
   following it (6) is greater than 5. In the first case,
   5 was followed by 4 (a decrease),
   which makes the resulting number smaller.

Example 2
Input: $str = '7332', $char = '3'
Output: '732'

Example 3
Input: $str = '2231', $char = '2'
Output: '231'
Removing either '2' results in the same string here. By 
   removing a '2', we allow the '3' to move up into a 
   higher decimal place.

Example 4
Input: $str = '543251', $char = '5'
Output: '54321'
If we remove the first '5', the number starts with 4. If 
   we remove the second '5', the number still starts with 
   5. Keeping the largest possible digit in the highest 
   place value is almost always the priority.

Example 5
Input: $str = '1921', $char = '1'
Output: '921'

Analysis

I had to dig deep in my memory to remember that a regex match sets two variables, $` and $', which contain respectively the part of the string being matched before and after the match.

Given that, a simple

while ($number =~ m|($digit)|g)

gives us all the prescribed deletions in $`$' and we need only find the numerically largest.

Try it 

Try running the script with any input:



example: 565453595



example: 5

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-04-06
use utf8;     # Week 368 - task 1 - Make it bigger
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

make_it_bigger(15456, 5);
make_it_bigger(7332, 3);
make_it_bigger(2231, 2);
make_it_bigger(543251, 5);
make_it_bigger(1921, 1);

sub make_it_bigger {
    
    my ($number, $digit, $largest);
    
    # initialise
    ($number, $digit) = @_;
    $largest = 0;

    # delete successive occurrences of $digit
    while ($number =~ m|($digit)|g) {
        $largest = qq[$`$'] if qq[$`$'] > $largest;
    }

    # report
    say qq[\nInput:  \$str = $number, \$char = $digit];
    say qq[Output: $largest];
}

last updated 2026-04-06 — 8 lines of code

Output


Input:  $str = 15456, $char = 5
Output: 1546

Input:  $str = 7332, $char = 3
Output: 732

Input:  $str = 2231, $char = 2
Output: 231

Input:  $str = 543251, $char = 5
Output: 54321

Input:  $str = 1921, $char = 1
Output: 921

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain