Task 1: Increment Decrement
"Stateful Strings: Tracking Value Through Operations!"
Given a list of operations in C-style syntax (++x, x++, --x, x--), calculate the final value starting from zero.
The Strategy: Initialize a variable
value to zero. Iterate through the list of operation strings. Since the task specifies only increment and decrement operations, we can simply check if the string contains the substring "++" or "--". Adjust the counter accordingly and return the final result.
Perl Implementation
sub final_value {
my (@ops) = @_;
my $value = 0;
for my $op (@ops) {
if ($op =~ /\+\+/) {
$value++;
} elsif ($op =~ /--/) {
$value--;
}
}
return $value;
}
Python Implementation
def final_value(ops: list[str]) -> int:
value = 0
for op in ops:
if "++" in op:
value += 1
elif "--" in op:
value -= 1
return value