aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-05-16 09:13:15 -0400
committerAdam Russell <ac.russell@live.com>2021-05-16 09:13:15 -0400
commitb84da8b604b8d4eee8f5a0b1afe62b46f67b9a34 (patch)
tree84b70612b26f4eb6acec3db26d517239f44098ab /challenge-112
parent8107c3cab76155d9554949b64df865f12b2da1ca (diff)
downloadperlweeklychallenge-club-b84da8b604b8d4eee8f5a0b1afe62b46f67b9a34.tar.gz
perlweeklychallenge-club-b84da8b604b8d4eee8f5a0b1afe62b46f67b9a34.tar.bz2
perlweeklychallenge-club-b84da8b604b8d4eee8f5a0b1afe62b46f67b9a34.zip
updated solutions
Diffstat (limited to 'challenge-112')
-rw-r--r--challenge-112/adam-russell/perl/ch-1.pl56
-rw-r--r--challenge-112/adam-russell/perl/ch-2.pl64
-rw-r--r--challenge-112/adam-russell/prolog/ch-2.p22
3 files changed, 142 insertions, 0 deletions
diff --git a/challenge-112/adam-russell/perl/ch-1.pl b/challenge-112/adam-russell/perl/ch-1.pl
index e69de29bb2..31bce25f8d 100644
--- a/challenge-112/adam-russell/perl/ch-1.pl
+++ b/challenge-112/adam-russell/perl/ch-1.pl
@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+##
+# Write a script to convert the given absolute path to the simplified canonical path.
+# The canonical path format:
+# - The path starts with a single slash '/'.
+# - Any two directories are separated by a single slash '/'.
+# - The path does not end with a trailing '/'.
+# - The path only contains the directories on the path from the root directory to the target file or directory
+##
+sub leading_slash{
+ my($path) = @_;
+ $path = "/" . $path if substr($path, 0, 1) ne "/";
+ return $path;
+}
+
+sub single_seperator{
+ my($path) = @_;
+ $path =~ s#\/\/#\/#;
+ return $path;
+}
+
+sub trailing_slash{
+ my($path) = @_;
+ chop($path) if substr($path, length($path) - 1, 1) eq "/";
+ return $path;
+}
+
+sub up_stay{
+ my($path) = @_;
+ my @directories = split(/\//, substr($path, 1));
+ my @temp_path;
+ for my $d (@directories){
+ push @temp_path, $d if $d ne "." && $d ne "..";
+ pop @temp_path if $d eq "..";
+ next if $d eq ".";
+ }
+ return "/" . join("/", @temp_path);
+}
+
+sub canonical_path{
+ my($path) = @_;
+ return up_stay(trailing_slash(single_seperator(leading_slash($path))));
+}
+
+MAIN:{
+ while(<DATA>){
+ chomp;
+ print canonical_path($_) . "\n";
+ }
+}
+
+__DATA__
+/a/
+/a/b//c/
+/a/b/c/../..
diff --git a/challenge-112/adam-russell/perl/ch-2.pl b/challenge-112/adam-russell/perl/ch-2.pl
index e69de29bb2..a238544999 100644
--- a/challenge-112/adam-russell/perl/ch-2.pl
+++ b/challenge-112/adam-russell/perl/ch-2.pl
@@ -0,0 +1,64 @@
+use strict;
+use warnings;
+##
+# 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.
+##
+use Array::Compare;
+use Algorithm::Combinatorics q/variations_with_repetition/;
+
+sub steps{
+ my($k) = @_;
+ my @data = (0, 1, 2);
+ my @steps;
+ my $comparison = new Array::Compare();
+ my $iterator = variations_with_repetition(\@data, $k);
+ while(my $combination = $iterator->next()){
+ if(unpack("%32C*", pack("C*", @{$combination})) == $k){
+ my $step = [grep {$_ != 0} @{$combination}];
+ push @steps, $step if(!grep {$comparison->compare($_, $step)} @steps);
+ }
+ }
+ return @steps;
+}
+
+MAIN:{
+ my @steps;
+ @steps = steps(3);
+ print "k = 3\n";
+ for my $steps (@steps){
+ my $option;
+ for my $step (@{$steps}){
+ $option .= "$step step + " if $step == 1;
+ $option .= "$step steps + " if $step == 2;
+ }
+ chop($option);
+ chop($option);
+ print "$option\n";
+ }
+ @steps = steps(4);
+ print "\nk = 4\n";
+ for my $steps (@steps){
+ my $option;
+ for my $step (@{$steps}){
+ $option .= "$step step + " if $step == 1;
+ $option .= "$step steps + " if $step == 2;
+ }
+ chop($option);
+ chop($option);
+ print "$option\n";
+ }
+ @steps = steps(5);
+ print "\nk = 5\n";
+ for my $steps (@steps){
+ my $option;
+ for my $step (@{$steps}){
+ $option .= "$step step + " if $step == 1;
+ $option .= "$step steps + " if $step == 2;
+ }
+ chop($option);
+ chop($option);
+ print "$option\n";
+ }
+}
diff --git a/challenge-112/adam-russell/prolog/ch-2.p b/challenge-112/adam-russell/prolog/ch-2.p
index e69de29bb2..0d5f837298 100644
--- a/challenge-112/adam-russell/prolog/ch-2.p
+++ b/challenge-112/adam-russell/prolog/ch-2.p
@@ -0,0 +1,22 @@
+:-initialization(main).
+
+steps --> [].
+steps --> step, steps.
+step --> [0]; [1]; [2].
+
+sum_steps(Steps, Goal):-
+ length(Steps, Goal),
+ phrase(steps, Steps),
+ sum_list(Steps, Goal).
+
+zero_remove([], []).
+zero_remove([H|T], [ZR_H|ZR_T]):-
+ delete(H, 0, ZR_H),
+ zero_remove(T, ZR_T).
+
+main:-
+ findall(Steps, sum_steps(Steps, 4), S),
+ zero_remove(S, SZR),
+ sort(SZR, SZR_Unique),
+ write(SZR_Unique), nl,
+ halt.