diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-05-26 21:10:36 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-05-26 21:10:36 +0200 |
| commit | a838cde65d75d5bfd325ff4faae83ecce14db0b8 (patch) | |
| tree | d6753e6e904bda5c6fe045c3718a86827e7be123 | |
| parent | 430f300fd7abb93023633e53f7d3d0768d0a8723 (diff) | |
| download | perlweeklychallenge-club-a838cde65d75d5bfd325ff4faae83ecce14db0b8.tar.gz perlweeklychallenge-club-a838cde65d75d5bfd325ff4faae83ecce14db0b8.tar.bz2 perlweeklychallenge-club-a838cde65d75d5bfd325ff4faae83ecce14db0b8.zip | |
feat(challenge-112/lubos-kolouch/perl,python/): Challenge 112 LK Perl Python
| -rw-r--r-- | challenge-112/lubos-kolouch/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-112/lubos-kolouch/perl/ch-2.pl | 15 | ||||
| -rw-r--r-- | challenge-112/lubos-kolouch/python/ch-1.py | 18 | ||||
| -rw-r--r-- | challenge-112/lubos-kolouch/python/ch-2.py | 15 |
4 files changed, 70 insertions, 0 deletions
diff --git a/challenge-112/lubos-kolouch/perl/ch-1.pl b/challenge-112/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..60a2f3be2f --- /dev/null +++ b/challenge-112/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; + +sub canonical_path { + my ($path) = @_; + my @path_parts = split( '/', $path ); + my @canonical_parts; + for my $part (@path_parts) { + if ( $part eq '..' ) { + if (@canonical_parts) { + pop @canonical_parts; + } + } + elsif ( $part && $part ne '.' ) { + push @canonical_parts, $part; + } + } + return '/' . join( '/', @canonical_parts ); +} + +# Testing +print canonical_path("/a/./b/../../c/"), "\n"; # Outputs: "/c" diff --git a/challenge-112/lubos-kolouch/perl/ch-2.pl b/challenge-112/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..06fa1805da --- /dev/null +++ b/challenge-112/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; + +sub climb_stairs { + my ($n) = @_; + return $n if $n <= 2; + my @ways = ( 0, 1, 2 ); + for my $i ( 3 .. $n ) { + push @ways, $ways[ $i - 1 ] + $ways[ $i - 2 ]; + } + return $ways[$n]; +} + +# Testing +print climb_stairs(5), "\n"; # Outputs: 8 diff --git a/challenge-112/lubos-kolouch/python/ch-1.py b/challenge-112/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..6a8d81b553 --- /dev/null +++ b/challenge-112/lubos-kolouch/python/ch-1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def canonical_path(path): + path_parts = path.split("/") + canonical_parts = [] + for part in path_parts: + if part == "..": + if canonical_parts: + canonical_parts.pop() + elif part and part != ".": + canonical_parts.append(part) + return "/" + "/".join(canonical_parts) + + +# Testing +print(canonical_path("/a/./b/../../c/")) # Outputs: "/c" diff --git a/challenge-112/lubos-kolouch/python/ch-2.py b/challenge-112/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..1ee087eb13 --- /dev/null +++ b/challenge-112/lubos-kolouch/python/ch-2.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def climb_stairs(n): + if n <= 2: + return n + ways = [0, 1, 2] + for i in range(3, n + 1): + ways.append(ways[i - 1] + ways[i - 2]) + return ways[n] + + +# Testing +print(climb_stairs(5)) # Outputs: 8 |
