From 95fc5529ffa6e135f10707cd17b8710630d317c2 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 8 Jan 2024 15:57:57 -0600 Subject: Week 251 Task 1 done --- challenge-251/bob-lied/README | 6 ++-- challenge-251/bob-lied/perl/ch-1.pl | 65 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 challenge-251/bob-lied/perl/ch-1.pl diff --git a/challenge-251/bob-lied/README b/challenge-251/bob-lied/README index a315d04ddf..1fb5d8a320 100644 --- a/challenge-251/bob-lied/README +++ b/challenge-251/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 250 by Bob Lied +Solutions to weekly challenge 251 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-250/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-250/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-251/bob-lied diff --git a/challenge-251/bob-lied/perl/ch-1.pl b/challenge-251/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..240336e6d7 --- /dev/null +++ b/challenge-251/bob-lied/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-1.pl Perl Weekly Challenge 251 Task 1 Concatenation Value +#============================================================================= +# You are given an array of integers, @ints. Write a script to find the +# concatenation value of the given array. The concatenation of two numbers +# is the number formed by concatenating their numerals. +# For example, the concatenation of 10, 21 is 1021. +# The concatenation value of @ints is initially equal to 0. +# Perform this operation until @ints becomes empty: +# If there exists more than one number in @ints, pick the first element +# and last element in @ints respectively and add the value of their +# concatenation to the concatenation value of @ints, then delete the +# first and last element from @ints. +# If one element exists, add its value to the concatenation value of +# @ints, then delete it. +# Example 1 Input: @ints = (6, 12, 25, 1) +# Output: 1286 +# 1st operation: concatenation of 6 and 1 is 61 +# 2nd operation: concaternation of 12 and 25 is 1225 +# Concatenation Value => 61 + 1225 => 1286 +# Example 2 Input: @ints = (10, 7, 31, 5, 2, 2) +# Output: 489 +# 1st operation: concatenation of 10 and 2 is 102 +# 2nd operation: concatenation of 7 and 2 is 72 +# 3rd operation: concatenation of 31 and 5 is 315 +# Concatenation Value => 102 + 72 + 315 => 489 +# Example 3 Input: @ints = (1, 2, 10) +# Output: 112 +# 1st operation: concatenation of 1 and 10 is 110 +# 2nd operation: only element left is 2 +# Concatenation Value => 110 + 2 => 112 +#============================================================================= + +use v5.38; +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub cval(@ints) +{ + my $val = 0; + $val += (shift(@ints) + pop(@ints)) while @ints; + return $val; +} + +sub runTest +{ + use Test2::V0; + + is( cval(6,12,25,1), 1286, "Example 1"); + is( cval(10,7,31,5,2,2), 489, "Example 2"); + is( cval(1,2,10), 112, "Example 3"); + + done_testing; +} -- cgit