aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-16 18:32:04 +0100
committerGitHub <noreply@github.com>2021-05-16 18:32:04 +0100
commitda0e50e761d9c6fb56ceea5d0dbd3b9d4b81bbb9 (patch)
tree2763b1f7a448067dbf28e6979108bb9301bf6da4
parentbea1130b7a6333ac14279bc8ed3dac05df129523 (diff)
parenta4e711f960843c641ce8ecdbd8309a83b4ec42fa (diff)
downloadperlweeklychallenge-club-da0e50e761d9c6fb56ceea5d0dbd3b9d4b81bbb9.tar.gz
perlweeklychallenge-club-da0e50e761d9c6fb56ceea5d0dbd3b9d4b81bbb9.tar.bz2
perlweeklychallenge-club-da0e50e761d9c6fb56ceea5d0dbd3b9d4b81bbb9.zip
Merge pull request #4083 from wanderdoc/master
Solution to task #2 challenge-112
-rw-r--r--challenge-112/wanderdoc/perl/ch-2.pl48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-112/wanderdoc/perl/ch-2.pl b/challenge-112/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..0a6dcd827b
--- /dev/null
+++ b/challenge-112/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given $n steps to climb
+
+Write a script to find out the distinct ways to climb to the top. You are allowed to climb either 1 or 2 steps at a time.
+Example
+
+Input: $n = 3
+Output: 3
+
+ Option 1: 1 step + 1 step + 1 step
+ Option 2: 1 step + 2 steps
+ Option 3: 2 steps + 1 step
+
+Input: $n = 4
+Output: 5
+
+ Option 1: 1 step + 1 step + 1 step + 1 step
+ Option 2: 1 step + 1 step + 2 steps
+ Option 3: 2 steps + 1 step + 1 step
+ Option 4: 1 step + 2 steps + 1 step
+ Option 5: 2 steps + 2 steps
+
+=cut
+
+
+use List::Util qw(sum);
+my $n = shift or die "Number of steps to climb?$/";
+my $opt = 1;
+for my $m ( $n/2 .. $n )
+{
+ my @steps =
+ grep { sum(@$_) == $n }
+ map { [split(//,$_)] }
+ glob q[{1,2}] x $m;
+
+
+ for my $step ( @steps )
+ {
+ @$step = map {$_ == 1 ? $_ . ' step' : $_ . ' steps'} @$step;
+
+ print "Option ${opt}: ", join(' + ', @$step), $/;
+ $opt++;
+ }
+} \ No newline at end of file