diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2020-08-03 15:12:56 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2020-08-03 15:12:56 -0400 |
| commit | 7799c640df177f0fafbb8b69b81a440358fc7291 (patch) | |
| tree | 4347b824e76281d614bd2d510a95a9d6164de715 /challenge-060 | |
| parent | 699b6492f575260664ec19786af984c15cbeac47 (diff) | |
| download | perlweeklychallenge-club-7799c640df177f0fafbb8b69b81a440358fc7291.tar.gz perlweeklychallenge-club-7799c640df177f0fafbb8b69b81a440358fc7291.tar.bz2 perlweeklychallenge-club-7799c640df177f0fafbb8b69b81a440358fc7291.zip | |
Challenge 60 by Jaldhar H. Vyas
Diffstat (limited to 'challenge-060')
| -rw-r--r-- | challenge-060/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-060/jaldhar-h-vyas/perl/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-060/jaldhar-h-vyas/perl/ch-2.pl | 45 | ||||
| -rwxr-xr-x | challenge-060/jaldhar-h-vyas/raku/ch-1.p6 | 27 | ||||
| -rwxr-xr-x | challenge-060/jaldhar-h-vyas/raku/ch-2.p6 | 14 |
5 files changed, 135 insertions, 0 deletions
diff --git a/challenge-060/jaldhar-h-vyas/blog.txt b/challenge-060/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..9410a1d93a --- /dev/null +++ b/challenge-060/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2020/08/perl_weekly_challenge_week_60.html diff --git a/challenge-060/jaldhar-h-vyas/perl/ch-1.pl b/challenge-060/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..48f90cf375 --- /dev/null +++ b/challenge-060/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; + +sub usage { + print<<"-USAGE-"; +Usage: + $PROGRAM_NAME <column> + $PROGRAM_NAME <num> + + <column> string of capital letters only + <num> integer > 0 +-USAGE- + exit 0; +} + +sub columnToNum { + my ($column) = @_; + my @chars = reverse split //, $column; + my $output = 0; + + for my $i (0 .. scalar @chars - 1) { + $output += + (ord($chars[$i]) - ord('A') + 1) * 26 ** $i; + } + return $output; +} + +sub numToColumn { + my ($num) = @_; + my $output = 'A'; + + for (1 .. $num - 1) { + $output++; + } + return $output; +} + +my $arg = shift // usage(); + +if ($arg =~ /^[A-Z]+$/) { + say columnToNum($arg); +} elsif ($arg =~ /^[0-9]+$/ && $arg > 0) { + say numToColumn($arg); +} else { + usage(); +} diff --git a/challenge-060/jaldhar-h-vyas/perl/ch-2.pl b/challenge-060/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..e350dd2ae6 --- /dev/null +++ b/challenge-060/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; +sub usage { + say<<"-USAGE-"; + $PROGRAM_NAME <X> <Y> [<L> ...] + + <X> number of digits in output numbers + <Y> output numbers must be less than this amount + [<L> ...] list of input numbers +-USAGE- + exit 0; +} + +sub combinations { + my @list = @{$_[0]}; + my $length = $_[1]; + + if ($length <= 1) { + return map [$_], @list; + } + + my @combos; + + for (my $i = 0; $i + $length <= scalar @list; $i++) { + my $val = $list[$i]; + my @rest = @list[$i + 1 .. $#list]; + for my $c (combinations(\@rest, $length - 1)) { + push @combos, [$val, @{$c}] ; + } + } + + return @combos; +} + +my $X = shift // usage(); +my $Y = shift // usage(); +my @L = @ARGV; + +my @output = map { (join q{}, @{$_}) + 0, (join q{}, reverse @{$_}) } + combinations(\@L, $X); +push @output, map { ($_ x $X) + 0 } @L; + +say join q{, }, (grep { length == $X && $_ < $Y } sort { $a <=> $b } @output); diff --git a/challenge-060/jaldhar-h-vyas/raku/ch-1.p6 b/challenge-060/jaldhar-h-vyas/raku/ch-1.p6 new file mode 100755 index 0000000000..6515fd559e --- /dev/null +++ b/challenge-060/jaldhar-h-vyas/raku/ch-1.p6 @@ -0,0 +1,27 @@ +#!/usr/bin/perl6 + +multi sub MAIN( + Str $column #= string of capital letters only + where { $column ~~ / ^ <[A..Z]>+ $/ } +) { + my @chars = $column.flip.comb; + my $output = 0; + + for 0 ..^ @chars.elems -> $i { + $output += + (@chars[$i].ord() - 'A'.ord() + 1) * 26 ** $i; + } + say $output; +} + +multi sub MAIN( + Int $num #= integer > 0 + where {$num > 0 } +) { + my $output = 'A'; + + for 1 ..^ $num { + $output++; + } + say $output; +}
\ No newline at end of file diff --git a/challenge-060/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-060/jaldhar-h-vyas/raku/ch-2.p6 new file mode 100755 index 0000000000..b84b680919 --- /dev/null +++ b/challenge-060/jaldhar-h-vyas/raku/ch-2.p6 @@ -0,0 +1,14 @@ +#!/usr/bin/perl6 + +sub MAIN( + Int $X, #= number of digits in output numbers + Int $Y, #= output numbers must be less than this amount + *@L #= list of input numbers +) { + my @output = + @L.combinations($X).map( { | ( $_.join() + 0, $_.join().flip + 0); }); + + @output.push(| @L.map({ ($_ x $X) + 0; }) ); + + @output.sort.grep({ $_.chars == $X && $_ < $Y; }).join(q{, }).say; +}
\ No newline at end of file |
