diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-08-26 03:55:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-26 03:55:26 +0100 |
| commit | a96e7639fea17e7c1468e6362d67c63914eefe84 (patch) | |
| tree | ae55aa60b4c6eac947c47cf9610cfe0549de8ba8 | |
| parent | 9b474d6285b684e79cad30025a05aca0a51cb5b4 (diff) | |
| parent | 3a146ad30d66d65ba634f95b81f42586534f06ed (diff) | |
| download | perlweeklychallenge-club-a96e7639fea17e7c1468e6362d67c63914eefe84.tar.gz perlweeklychallenge-club-a96e7639fea17e7c1468e6362d67c63914eefe84.tar.bz2 perlweeklychallenge-club-a96e7639fea17e7c1468e6362d67c63914eefe84.zip | |
Merge pull request #6653 from aut0exec/master
adds aut0exec's challenge 1
| -rwxr-xr-x | challenge-179/aut0exec/perl/C1.pl | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-179/aut0exec/perl/C1.pl b/challenge-179/aut0exec/perl/C1.pl new file mode 100755 index 0000000000..bfcdc27fd6 --- /dev/null +++ b/challenge-179/aut0exec/perl/C1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +# +# You are given a positive number, $n. +# Write a script to spell the ordinal number. + +use strict; +use warnings; + +sub ones { + my $value = shift; + my %digit = ( + 0 => '', 1 => 'First', 2 => 'Second', 3 => 'Third', 4 => 'Fourth', + 5 => 'Fifth', 6 => 'Sixth', 7 => 'Seventh', 8 => 'Eighth', 9 => 'Nineth' + ); + + return "$digit{$value}\n"; +} + +sub teens { + my $value = shift; + my %digit = ( + 11 => 'Eleventh', 12 => 'Twelveth', 13 => 'Thirteenth', 14 => 'Fourteenth', + 15 => 'Fifteenth', 16 => 'Sixteenth', 17 => 'Seventeenth', 18 => 'Eighteenth', + 19 => 'Nineteenth' + ); + + return "$digit{$value}\n"; +} + +sub tens { + my $value = shift; + my %digit = ( + 0 => '', 2 => 'Twenty', 3 => 'Thirty', 4 => 'Forty', 5 => 'Fifty', + 6 => 'Sixty', 7 => 'Seventy', 8 => 'Eighty', 9 => 'Ninety' + ); + + return "$digit{$value}"; +} + +my $ordinal_num = shift; +my @ordinal = split (//,$ordinal_num); +my $dig_len = length($ordinal_num); + +if ( $dig_len > 2 or $ordinal_num lt 1) +{ print ("$ordinal_num is invalid for this program!\n"); exit 1; } +elsif ( $dig_len == 1 ) +{ print (ones($ordinal_num)); } +elsif ($ordinal[-2] == 1 and $dig_len < 3) +{ print (teens ($ordinal_num)); } +elsif ($ordinal[-2] != 1 and $ordinal[-1] == 0) +{ print (tens ($ordinal[-2]) . "\n"); } +else +{ print ( tens($ordinal[0]) . "-" . ones($ordinal[1]) ); } |
