aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-24 22:24:14 +0100
committerGitHub <noreply@github.com>2023-09-24 22:24:14 +0100
commite454cd12843354e880cab8a361c6357357f8c785 (patch)
treee21726713c235b7b3757748004924df7abebef69
parentbdec35717971a5e60227cb25205e52ece8200204 (diff)
parentf67fed2220d76560534f66f235c45a86103cc4b3 (diff)
downloadperlweeklychallenge-club-e454cd12843354e880cab8a361c6357357f8c785.tar.gz
perlweeklychallenge-club-e454cd12843354e880cab8a361c6357357f8c785.tar.bz2
perlweeklychallenge-club-e454cd12843354e880cab8a361c6357357f8c785.zip
Merge pull request #8759 from spadacciniweb/PWC-235
PWC-235 ch-1.pl
-rw-r--r--challenge-235/spadacciniweb/perl/ch-2.pl35
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-235/spadacciniweb/perl/ch-2.pl b/challenge-235/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..6b6fb179a7
--- /dev/null
+++ b/challenge-235/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+# Task 2: Duplicate Zeros
+# Submitted by: Mohammad S Anwar
+#
+# You are 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 remain 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)
+
+
+use strict;
+use warnings;
+
+my @input = @ARGV;
+die "Input error\n"
+ if scalar @input < 1
+ or
+ (scalar map { $_ =~ /[\-\d]/ ? () : 1 }
+ @input) != 0;
+
+printf "(%s)\n", join ', ', (map { $_ == 0 ? (0, 0) : $_ }
+ @input)[0..$#input];