Robbie Hatley's Solutions To The Weekly Challenge #235

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2023-09-17 through 2023-09-23) is weekly challenge #235.

Task 1 is as follows:

Task 1: Remove One
Submitted by: Mohammad S Anwar
Given an array of integers, write a script to find out if
removing ONLY one integer makes it strictly increasing order.

Example 1:
Input:  @ints = (0, 2, 9, 4, 6)
Output: true
Removing ONLY 9 in the array makes it strictly-increasing.

Example 2:
Input:  @ints = (5, 1, 3, 2)
Output: false

Example 3
Input:  @ints = (2, 2, 3)
Output: true

I solved this by making these two subs:

# Determine if a given array is strictly-increasing:
sub is_strictly_increasing;

# Determine if a given array can be made strictly-increasing
# by removing 1 element:
sub remove_one;

The script I came up with was this:

Robbie Hatley's Solution to The Weekly Challenge 235-1

Task 2 is as follows:

Task 2: Duplicate Zeros
Submitted by: Mohammad S Anwar
Given an array of integers, write a script to duplicate each
occurrence of ZERO in the given array and shift the remaining
to the right but make sure the size of array remains the same.

Example 1:
Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0)
Ouput: (1, 0, 0, 2, 3, 0, 0, 4)

Example 2:
Input: @ints = (1, 2, 3)
Ouput: (1, 2, 3)

Example 3:
Input: @ints = (0, 3, 0, 4, 5)
Ouput: (0, 0, 3, 0, 0)

I solved this by making a sub called "double_aught" which converts all single-aught buckshot into double-aught, discarding any pellets which are too large to fit into the shell.

The script I ended up with is this:

Robbie Hatley's Solution to The Weekly Challenge 235-2

That's it for 235; see you on 236!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #262