diff options
| author | Bob Lied <boblied+github@gmail.com> | 2023-09-22 16:43:08 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2023-09-22 16:43:08 -0500 |
| commit | 80cb3971469f530bdd052cf6b5b5e9926e0aca6e (patch) | |
| tree | 7637d5cfaee83c021c290c4bc5eeb222cc6fc175 | |
| parent | aeb59e02a203ef8d99c83c5eef6ba78a54678859 (diff) | |
| download | perlweeklychallenge-club-80cb3971469f530bdd052cf6b5b5e9926e0aca6e.tar.gz perlweeklychallenge-club-80cb3971469f530bdd052cf6b5b5e9926e0aca6e.tar.bz2 perlweeklychallenge-club-80cb3971469f530bdd052cf6b5b5e9926e0aca6e.zip | |
Week 173 Task 1 solution
| -rw-r--r-- | challenge-173/bob-lied/perl/ch-1.pl | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-173/bob-lied/perl/ch-1.pl b/challenge-173/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..940733d29c --- /dev/null +++ b/challenge-173/bob-lied/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge 173 Task 1 Esthetic Number +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given a positive integer, $n. +# Write a script to find out if the given number is Esthetic Number. +# An esthetic number is a positive integer where every adjacent digit +# differs from its neighbour by 1. For example, +# 5456 is an esthetic number as |5 - 4| = |4 - 5| = |5 - 6| = 1 +# 120 is not an esthetic numner as |1 - 2| != |2 - 0| != 1 +#============================================================================= + +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; + +for my $N ( @ARGV ) +{ + say "$N ", ( isEsthetic($N) ? "IS " : "is not" ), " esthetic"; +} + +sub isEsthetic($n) +{ + my $isEth = true; + + my @d = split(//, $n); + my $first = shift @d; + while ( defined (my $next = shift @d) && $isEth ) + { + $isEth &&= ( ($first - $next == 1) || ( $next - $first == 1 ) ); + $first = $next; + } + return $isEth; +} + +sub runTest +{ + use Test2::V0; + no warnings "experimental::builtin"; + + is( isEsthetic(5456), true, "Example 1"); + is( isEsthetic( 120), false, "Example 2"); + + done_testing; +} + |
