From 9f9a45e099e9d4675962476178ed03926f7204c7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 15 Jun 2021 22:37:49 +0100 Subject: Add Perl solution to challenge 112 --- .gitignore | 1 + challenge-112/paulo-custodio/perl/ch-1.pl | 50 ++++++++++++++++++++++++++++++ challenge-112/paulo-custodio/perl/ch-2.pl | 48 ++++++++++++++++++++++++++++ challenge-112/paulo-custodio/t/test-1.yaml | 20 ++++++++++++ challenge-112/paulo-custodio/t/test-2.yaml | 25 +++++++++++++++ challenge-112/paulo-custodio/test.pl | 7 +++++ 6 files changed, 151 insertions(+) create mode 100644 challenge-112/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-112/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-112/paulo-custodio/t/test-1.yaml create mode 100644 challenge-112/paulo-custodio/t/test-2.yaml create mode 100755 challenge-112/paulo-custodio/test.pl diff --git a/.gitignore b/.gitignore index a70b9e5e03..db4cc30c24 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .precomp/ .DS_Store .vstags +.vscode .*.swp *~ *.class diff --git a/challenge-112/paulo-custodio/perl/ch-1.pl b/challenge-112/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..058a6b9e1f --- /dev/null +++ b/challenge-112/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# Challenge 112 +# +# TASK #1 - Canonical Path +# Submitted by: Mohammad S Anwar +# You are given a string path, starting with a slash ‘/'. +# +# Write a script to convert the given absolute path to the simplified canonical +# path. +# +# In a Unix-style file system: +# +# - A period '.' refers to the current directory +# - A double period '..' refers to the directory up a level +# - Multiple consecutive slashes ('//') are treated as a single slash '/' +# 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 +# Example +# Input: "/a/" +# Output: "/a" +# +# Input: "/a/b//c/" +# Output: "/a/b/c" +# +# Input: "/a/b/c/../.." +# Output: "/a" + +use Modern::Perl; +my $path = shift || ''; +say canon($path); + +sub canon { + my($path) = @_; + my $dir = qr/[^\/\.]+/; + for ($path) { + 1 while s{/\./}{/}; + 1 while s{/\.$}{/}; + 1 while s{/$dir/\.\./}{/}; + 1 while s{/$dir/\.\.$}{/}; + s{/+}{/}g; + s{/+$}{}g; + } + return $path; +} diff --git a/challenge-112/paulo-custodio/perl/ch-2.pl b/challenge-112/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..3e866319b9 --- /dev/null +++ b/challenge-112/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +# Challenge 112 +# +# TASK #2 - Climb Stairs +# Submitted by: Mohammad S Anwar +# 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 + +use Modern::Perl; + +my $n = shift || 0; +say count($n); + +sub count { + my($n) = @_; + if ($n <= 0) { + return 0; + } + elsif ($n == 1) { + return 1; + } + elsif ($n == 2) { + return 2; + } + else { + return count($n-1)+count($n-2); + } +} diff --git a/challenge-112/paulo-custodio/t/test-1.yaml b/challenge-112/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..379880547d --- /dev/null +++ b/challenge-112/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: /a/ + input: + output: /a +- setup: + cleanup: + args: /a/b//c/ + input: + output: /a/b/c +- setup: + cleanup: + args: /a/./b/./c/ + input: + output: /a/b/c +- setup: + cleanup: + args: /a/b/c/../../ + input: + output: /a diff --git a/challenge-112/paulo-custodio/t/test-2.yaml b/challenge-112/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c1156b2fe4 --- /dev/null +++ b/challenge-112/paulo-custodio/t/test-2.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 2 + input: + output: 2 +- setup: + cleanup: + args: 3 + input: + output: 3 +- setup: + cleanup: + args: 4 + input: + output: 5 +- setup: + cleanup: + args: 5 + input: + output: 8 diff --git a/challenge-112/paulo-custodio/test.pl b/challenge-112/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-112/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'; -- cgit