aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@users.noreply.github.com>2025-05-28 17:57:55 +0200
committerGitHub <noreply@github.com>2025-05-28 17:57:55 +0200
commit3943ea3376e7c71bda8aa99c41384f76af2254b9 (patch)
tree009bde5ad3301a652acbac5b32802561beb3be09
parent1f000a6d1875beabc5938005e4445127d5a1bb3d (diff)
downloadperlweeklychallenge-club-3943ea3376e7c71bda8aa99c41384f76af2254b9.tar.gz
perlweeklychallenge-club-3943ea3376e7c71bda8aa99c41384f76af2254b9.tar.bz2
perlweeklychallenge-club-3943ea3376e7c71bda8aa99c41384f76af2254b9.zip
Create ch-1.pl
-rw-r--r--challenge-323/wanderdoc/perl/ch-1.pl66
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-323/wanderdoc/perl/ch-1.pl b/challenge-323/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..b3e829cbb0
--- /dev/null
+++ b/challenge-323/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a list of operations.
+
+Write a script to return the final value after performing the given operations in order. The initial value is always 0.
+
+Possible Operations:
+++x or x++: increment by 1
+--x or x--: decrement by 1
+
+
+Example 1
+
+Input: @operations = ("--x", "x++", "x++")
+Output: 1
+
+Operation "--x" => 0 - 1 => -1
+Operation "x++" => -1 + 1 => 0
+Operation "x++" => 0 + 1 => 1
+
+
+Example 2
+
+Input: @operations = ("x++", "++x", "x++")
+Output: 3
+
+
+Example 3
+
+Input: @operations = ("x++", "++x", "--x", "x--")
+Output: 0
+
+Operation "x++" => 0 + 1 => 1
+Operation "++x" => 1 + 1 => 2
+Operation "--x" => 2 - 1 => 1
+Operation "x--" => 1 - 1 => 0
+
+=cut
+
+use Test2::V0 -no_srand => 1;
+
+is(task1("--x", "x++", "x++"), 1, 'Example 1');
+is(task1("x++", "++x", "x++"), 3, 'Example 2');
+is(task1("x++", "++x", "--x", "x--"), 0, 'Example 3');
+done_testing();
+
+sub task1
+{
+ my @arr = @_;
+ my $value = 0;
+ for my $operator ( @arr )
+ {
+ if ($operator =~ /\+/)
+ {
+ $value++;
+ }
+ elsif ( $operator =~ /\-/ )
+ {
+ $value--;
+ }
+ }
+ return $value;
+}