diff options
| author | Bob Lied <boblied+github@gmail.com> | 2024-01-15 07:37:38 -0600 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2024-01-15 07:37:38 -0600 |
| commit | 4f6ce750b551df16ffeb86f5010545cc4cefbe42 (patch) | |
| tree | 304dfb5c273ec0b1729734ebbf5fafaaa9ccb544 | |
| parent | 7efb373bb9adffa79f84825217015835805298b5 (diff) | |
| download | perlweeklychallenge-club-4f6ce750b551df16ffeb86f5010545cc4cefbe42.tar.gz perlweeklychallenge-club-4f6ce750b551df16ffeb86f5010545cc4cefbe42.tar.bz2 perlweeklychallenge-club-4f6ce750b551df16ffeb86f5010545cc4cefbe42.zip | |
Week 252 Task 1 done
| -rw-r--r-- | challenge-252/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-252/bob-lied/perl/ch-1.pl | 57 |
2 files changed, 60 insertions, 3 deletions
diff --git a/challenge-252/bob-lied/README b/challenge-252/bob-lied/README index 1fb5d8a320..8b958edfe8 100644 --- a/challenge-252/bob-lied/README +++ b/challenge-252/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 251 by Bob Lied +Solutions to weekly challenge 252 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-251/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-252/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-252/bob-lied diff --git a/challenge-252/bob-lied/perl/ch-1.pl b/challenge-252/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..35d16224fc --- /dev/null +++ b/challenge-252/bob-lied/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/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 252 Task 1 Special Numbers +#============================================================================= +# You are given an array of integers, @ints. +# Write a script to find the sum of the squares of all special elements of +# the given array. An element $int[i] of @ints is called special if +# i divides n, i.e. n % i == 0. Where n is the length of the given array. +# Also the array is 1-indexed for the task. +# Example 1 Input: @ints = (1, 2, 3, 4) +# Output: 21 +# There are exactly 3 special elements in the given array: +# $ints[1] since 1 divides 4, +# $ints[2] since 2 divides 4, and +# $ints[4] since 4 divides 4. +# Hence, the sum of the squares of all special elements of given array: +# 1 * 1 + 2 * 2 + 4 * 4 = 21. +# Example 2 Input: @ints = (2, 7, 1, 19, 18, 3) +# Output: 63 +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; +use List::Util qw/sum0/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub specialNumbers(@ints) +{ + my $len = @ints; + # Insert an extra element at the front to make it 1-indexed + unshift @ints, $len+1; + + return sum0 map { $_ * $_ } @ints[ grep { $len % $_ == 0 } 1 .. $len ]; +} + +sub runTest +{ + use Test2::V0; + + is( specialNumbers(1,2,3,4), 21, "Example 1"); + is( specialNumbers(2,7,1,19,18,3), 63, "Example 2"); + is( specialNumbers(8 ), 64, "Singleton"); + is( specialNumbers() , 0, "Empty list"); + + done_testing; +} |
