aboutsummaryrefslogtreecommitdiff
path: root/challenge-057
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-04-16 15:24:13 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-04-16 15:24:13 +0200
commit8091d154c159948b012ce9728cbd8942ea3d148c (patch)
tree11224395d1559dd0e7df4e865fbf0c132fd16a10 /challenge-057
parentfc7f30eaf4f1b81354ea7a10b3240efccf28a084 (diff)
downloadperlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.tar.gz
perlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.tar.bz2
perlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.zip
Challenge 057 059 LK Perl Python
Diffstat (limited to 'challenge-057')
-rw-r--r--challenge-057/lubos-kolouch/perl/ch-1.pl58
-rw-r--r--challenge-057/lubos-kolouch/perl/ch-2.pl26
-rw-r--r--challenge-057/lubos-kolouch/python/ch-1.py51
-rw-r--r--challenge-057/lubos-kolouch/python/ch-2.py24
4 files changed, 159 insertions, 0 deletions
diff --git a/challenge-057/lubos-kolouch/perl/ch-1.pl b/challenge-057/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..8589164d70
--- /dev/null
+++ b/challenge-057/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+package BinaryTree;
+
+sub new {
+ my ( $class, $value, $left, $right ) = @_;
+ my $self = {
+ value => $value,
+ left => $left,
+ right => $right,
+ };
+ bless $self, $class;
+ return $self;
+}
+
+sub invert {
+ my $self = shift;
+
+ ( $self->{left}, $self->{right} ) = ( $self->{right}, $self->{left} );
+ $self->{left}->invert() if defined $self->{left};
+ $self->{right}->invert() if defined $self->{right};
+}
+
+sub pretty_print {
+ my $self = shift;
+ my $padding = shift || '';
+ my $branch_padding = shift || '';
+
+ if ( defined $self->{right} ) {
+ $self->{right}->pretty_print( "$padding ", "$padding|" );
+ }
+
+ print $branch_padding;
+ print "--" if $branch_padding;
+ print $self->{value}, "\n";
+
+ if ( defined $self->{left} ) {
+ $self->{left}->pretty_print( "$padding ", "$padding|" );
+ }
+}
+
+package main;
+
+my $tree = BinaryTree->new(
+ 1,
+ BinaryTree->new( 2, BinaryTree->new(4), BinaryTree->new(5) ),
+ BinaryTree->new( 3, BinaryTree->new(6), BinaryTree->new(7) )
+);
+
+print "Original tree:\n";
+$tree->pretty_print();
+
+$tree->invert();
+
+print "\nInverted tree:\n";
+$tree->pretty_print();
diff --git a/challenge-057/lubos-kolouch/perl/ch-2.pl b/challenge-057/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..b8edac5a5a
--- /dev/null
+++ b/challenge-057/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub shortest_unique_prefixes {
+ my @words = @_;
+ my @result;
+
+ for my $word (@words) {
+ my $prefix = "";
+ for my $char ( split //, $word ) {
+ $prefix .= $char;
+ if ( !grep { $_ =~ /^$prefix/ } @result ) {
+ push @result, $prefix;
+ last;
+ }
+ }
+ }
+
+ return \@result;
+}
+
+my @input = ( "alphabet", "book", "carpet", "cadmium", "cadeau", "alpine" );
+my $result = shortest_unique_prefixes(@input);
+
+print "[", join( ", ", @{$result} ), "]\n";
diff --git a/challenge-057/lubos-kolouch/python/ch-1.py b/challenge-057/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..e7dd3485db
--- /dev/null
+++ b/challenge-057/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class BinaryTree:
+ def __init__(self, value, left=None, right=None):
+ self.value = value
+ self.left = left
+ self.right = right
+
+ def invert(self):
+ self.left, self.right = self.right, self.left
+ if self.left:
+ self.left.invert()
+ if self.right:
+ self.right.invert()
+
+ def pretty_print(self, padding="", branch_padding=""):
+ if self.right:
+ self.right.pretty_print(padding + " ", padding + "|")
+
+ print(branch_padding, end="")
+ if branch_padding:
+ print("--", end="")
+ print(self.value)
+
+ if self.left:
+ self.left.pretty_print(padding + " ", padding + "|")
+
+
+if __name__ == "__main__":
+ tree = BinaryTree(
+ 1,
+ BinaryTree(
+ 2,
+ BinaryTree(4),
+ BinaryTree(5)
+ ),
+ BinaryTree(
+ 3,
+ BinaryTree(6),
+ BinaryTree(7)
+ )
+ )
+
+ print("Original tree:")
+ tree.pretty_print()
+
+ tree.invert()
+
+ print("\nInverted tree:")
+ tree.pretty_print()
diff --git a/challenge-057/lubos-kolouch/python/ch-2.py b/challenge-057/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..d8dd139311
--- /dev/null
+++ b/challenge-057/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def shortest_unique_prefixes(words: List[str]) -> List[str]:
+ result = []
+
+ for word in words:
+ prefix = ""
+ for char in word:
+ prefix += char
+ if not any(res.startswith(prefix) for res in result):
+ result.append(prefix)
+ break
+
+ return result
+
+if __name__ == "__main__":
+ input_list = ["alphabet", "book", "carpet", "cadmium", "cadeau", "alpine"]
+ result = shortest_unique_prefixes(input_list)
+
+ print(result)