diff options
| -rw-r--r-- | challenge-107/paulo-custodio/perl/ch-1.pl | 73 | ||||
| -rw-r--r-- | challenge-107/paulo-custodio/perl/ch-2.pl | 45 | ||||
| -rw-r--r-- | challenge-107/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-107/paulo-custodio/t/test-2.yaml | 9 | ||||
| -rwxr-xr-x | challenge-107/paulo-custodio/test.pl | 7 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/perl/ch-2.pl | 2 |
6 files changed, 139 insertions, 2 deletions
diff --git a/challenge-107/paulo-custodio/perl/ch-1.pl b/challenge-107/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..29b92e378f --- /dev/null +++ b/challenge-107/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl + +# Challenge 107 +# +# TASK #1 - Self-descriptive Numbers +# Submitted by: Mohammad S Anwar +# Write a script to display the first three self-descriptive numbers. As per +# wikipedia, the definition of Self-descriptive Number is +# +# In mathematics, a self-descriptive number is an integer m that in a given +# base b is b digits long in which each digit d at position n (the most +# significant digit being at position 0 and the least significant at position +# b−1) counts how many instances of digit n are in m. +# +# For example: +# +# 1210 is a four-digit self-descriptive number: +# +# position 0 has value 1 i.e. there is only one 0 in the number +# position 1 has value 2 i.e. there are two 1 in the number +# position 2 has value 1 i.e. there is only one 2 in the number +# position 3 has value 0 i.e. there is no 3 in the number +# Output +# 1210, 2020, 21200 + +use Modern::Perl; + +print_self_descriptive(3); + +sub print_self_descriptive { + my($num) = @_; + my $found = 0; + for (my $base = 4; 1; $base++) { + my @n = (1, (0) x ($base-1)); + while (@n == $base) { + if (is_self_descriptive(@n)) { + print ", " if $found > 0; + print join '', @n; + $found++; + if ($found >= $num) { + print "\n"; + return; + } + } + increment(\@n, $base); + } + } +} + +sub increment { + my($n, $base) = @_; + my $i = $#$n; + while ($i >= 0) { + $n->[$i]++; + if ($n->[$i] < $base) { + return; + } + else { + $n->[$i] = 0; + $i--; + } + } + unshift @$n, 1; +} + +sub is_self_descriptive { + my(@n) = @_; + for my $i (0..$#n) { + my $count = scalar grep {$_==$i} @n; + return if $n[$i] != $count; + } + return 1; +} diff --git a/challenge-107/paulo-custodio/perl/ch-2.pl b/challenge-107/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..6cfeb0fc21 --- /dev/null +++ b/challenge-107/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 107 +# +# TASK #2 - List Methods +# Submitted by: Mohammad S Anwar +# Write a script to list methods of a package/class. +# +# Example +# package Calc; +# +# use strict; +# use warnings; +# +# sub new { bless {}, shift; } +# sub add { } +# sub mul { } +# sub div { } +# +# 1; +# Output +# BEGIN +# mul +# div +# new +# add + +use Modern::Perl; +{ + package Calc; + sub new { bless {}, shift; } + sub add { } + sub mul { } + sub div { } +} + +show_methods('Calc'); + +sub show_methods { + my($package) = @_; + no strict 'refs'; + for my $symbol (sort keys %{$package."::"}) { + say $symbol if exists &{$package."::".$symbol}; + } +} diff --git a/challenge-107/paulo-custodio/t/test-1.yaml b/challenge-107/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..345674d234 --- /dev/null +++ b/challenge-107/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 1210, 2020, 21200 diff --git a/challenge-107/paulo-custodio/t/test-2.yaml b/challenge-107/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..680592ee58 --- /dev/null +++ b/challenge-107/paulo-custodio/t/test-2.yaml @@ -0,0 +1,9 @@ +- setup: + cleanup: + args: + input: + output: | + add + div + mul + new diff --git a/challenge-107/paulo-custodio/test.pl b/challenge-107/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-107/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-116/paulo-custodio/perl/ch-2.pl b/challenge-116/paulo-custodio/perl/ch-2.pl index a84fe54eec..9b26c9afa0 100644 --- a/challenge-116/paulo-custodio/perl/ch-2.pl +++ b/challenge-116/paulo-custodio/perl/ch-2.pl @@ -34,5 +34,3 @@ sub sum_of_squares_is_perfect_square { my $sqrt_int = int(sqrt($sum)); return $sqrt_int*$sqrt_int==$sum; } - - |
