diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2021-03-06 12:19:56 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2021-03-06 12:19:56 +0100 |
| commit | b1706186629563669fdfc3ef8916f89197165568 (patch) | |
| tree | 493a2011e4af304eea218a1b4f1400044cda592e | |
| parent | 495c32be24dcfc94b86c78fc4e3729274b1f112c (diff) | |
| download | perlweeklychallenge-club-b1706186629563669fdfc3ef8916f89197165568.tar.gz perlweeklychallenge-club-b1706186629563669fdfc3ef8916f89197165568.tar.bz2 perlweeklychallenge-club-b1706186629563669fdfc3ef8916f89197165568.zip | |
Challenge 102 LK Perl
| -rw-r--r-- | challenge-102/lubos-kolouch/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-102/lubos-kolouch/perl/ch-2.pl | 50 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-102/lubos-kolouch/perl/ch-1.pl b/challenge-102/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..066d25b295 --- /dev/null +++ b/challenge-102/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge #102 +# Task 1 - Rare Numbers +# +# AUTHOR: Lubos Kolouch +# CREATED: 03/06/2021 11:18:30 AM +#=============================================================================== + +use strict; +use warnings; + +sub get_rare_numbers { + my $what = shift; + + my $pos = 1 . '0'x($what-1); + + my @output; + while (1) { + $pos++; + last unless (length($pos) == $what); + my $rev_num = reverse $pos; + + next unless $pos - $rev_num > 0; + next unless sqrt($pos - $rev_num) == int(sqrt($pos - $rev_num)); + next unless sqrt($pos + $rev_num) == int(sqrt($pos + $rev_num)); + print "$pos \n"; + + push @output, $pos; + } + + return \@output; +} + +use Test::More; + +is_deeply(get_rare_numbers(2), [65]); +is_deeply(get_rare_numbers(6), [621770]); +is_deeply(get_rare_numbers(9), [281089082]); + diff --git a/challenge-102/lubos-kolouch/perl/ch-2.pl b/challenge-102/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..600a29572a --- /dev/null +++ b/challenge-102/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: Perl Weekly Challenge #102 +# Task 2 - Hash-counting String +# +# AUTHOR: Lubos Kolouch +# CREATED: 03/06/2021 11:18:30 AM +#=============================================================================== + +use strict; +use warnings; + +sub get_hash { + my $what = shift; + + # we know the last position has length + # of the input. So we can just backtrack + + my $output = ''; + + my $pos = $what; + while ($pos > 0) { + my $append_str = $pos.'#'; + + if ($pos > 1) { + $output = $pos.'#'.$output; + } else { + $output = '#'.$output; + } + + $pos -= length($append_str); + } + + return $output; +} + +use Test::More; + +is_deeply(get_hash(1), '#'); +is_deeply(get_hash(2), '2#'); +is_deeply(get_hash(3), '#3#'); +is_deeply(get_hash(10), '#3#5#7#10#'); +is_deeply(get_hash(14), '2#4#6#8#11#14#'); + +done_testing; |
