/
ch-2.pl
executable file
·104 lines (84 loc) · 3.41 KB
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl -CSDA
=pod
--------------------------------------------------------------------------------------------------------------
COLOPHON:
This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A").
¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。
--------------------------------------------------------------------------------------------------------------
TITLE BLOCK:
Solutions in Perl for The Weekly Challenge 235-2.
Written by Robbie Hatley on Wed Sep 20, 2023.
--------------------------------------------------------------------------------------------------------------
PROBLEM DESCRIPTION:
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)
--------------------------------------------------------------------------------------------------------------
PROBLEM NOTES:
I'll solve 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.
--------------------------------------------------------------------------------------------------------------
IO NOTES:
Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
double-quoted array of arrays of integers, in proper Perl syntax, like so:
./ch-2.pl "([3, 7, 0, -2, 13],[7, 0, 0, 0, 17, 45, 62, 10])"
Output is to STDOUT and will be each input array followed by the corresponding output.
=cut
# ------------------------------------------------------------------------------------------------------------
# PRELIMINARIES:
use v5.38;
use strict;
use warnings;
use utf8;
use warnings FATAL => 'utf8';
use Sys::Binmode;
use Time::HiRes 'time';
# ------------------------------------------------------------------------------------------------------------
# SUBROUTINES:
sub double_aught ($aref) {
my @double = @$aref;
my $last_idx = $#double;
for ( my $idx = 0 ; $idx <= $#double ; ++$idx ) {
if ( 0 == $double[$idx] ) {
splice @double, $idx+1, 0, 0;
# Increment $idx here so that it points to the added 0,
# so that the "++$idx" above will skip past it:
++$idx;
}
}
# Trim size of @double back to its original size
# (right-most elements will be removed as-necessary):
$#double = $last_idx;
return @double;
}
# ------------------------------------------------------------------------------------------------------------
# MAIN BODY OF PROGRAM:
# Start timer:
my $t0 = time;
# Inputs:
my @arrays = @ARGV ? eval($ARGV[0]) :
(
[1, 0, 2, 3, 0, 4, 5, 0],
[1, 2, 3],
[0, 3, 0, 4, 5],
);
# Main loop:
for my $aref (@arrays) {
say '';
say ' Original array = (', join(', ', @$aref), ')';
say 'Double-aught array = (', join(', ', double_aught($aref)), ')';
}
# Determine and print execution time:
my $µs = 1000000 * (time - $t0);
printf("\nExecution time was %.0fµs.\n", $µs);