The weekly challenge 330 - Task 1: Clear Digits
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-330/#TASK1 3 # 4 # Task 1: Clear Digits 5 # ==================== 6 # 7 # You are given a string containing only lower case English letters and digits. 8 # 9 # Write a script to remove all digits by removing the first digit and the 10 # closest non-digit character to its left. 11 # 12 ## Example 1 13 ## 14 ## Input: $str = "cab12" 15 ## Output: "c" 16 ## 17 ## Round 1: remove "1" then "b" => "ca2" 18 ## Round 2: remove "2" then "a" => "c" 19 # 20 # 21 ## Example 2 22 ## 23 ## Input: $str = "xy99" 24 ## Output: "" 25 ## 26 ## Round 1: remove "9" then "y" => "x9" 27 ## Round 2: remove "9" then "x" => "" 28 # 29 # 30 ## Example 3 31 ## 32 ## Input: $str = "pa1erl" 33 ## Output: "perl" 34 # 35 ############################################################ 36 ## 37 ## discussion 38 ## 39 ############################################################ 40 # 41 # Long description short, as long as there is still a non-digit 42 # before a digit, remove those two characters. 43 # Note: If the string starts with a digit, but then has other characters before 44 # other digits, this will still remove the later digit. It's not 100% clear 45 # whether this is an allowed input though. 46 47 use v5.36; 48 49 clear_digits("cab12"); 50 clear_digits("xy99"); 51 clear_digits("pa1erl"); 52 53 sub clear_digits($str) { 54 say "Input: \"$str\""; 55 while($str =~ s/[^\d]\d//) { 56 } 57 say "Output: \"$str\""; 58 }