aboutsummaryrefslogtreecommitdiff
path: root/challenge-125
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-06-17 10:32:17 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-06-17 10:32:17 +0200
commit130de4f52d9f775c453eb26da01554baa69c3c75 (patch)
tree41c12bfdc0abcd0278d0a769b7a6d6b39f917db7 /challenge-125
parent8c29879276597f2e477edefaf4b9d8b77630a797 (diff)
downloadperlweeklychallenge-club-130de4f52d9f775c453eb26da01554baa69c3c75.tar.gz
perlweeklychallenge-club-130de4f52d9f775c453eb26da01554baa69c3c75.tar.bz2
perlweeklychallenge-club-130de4f52d9f775c453eb26da01554baa69c3c75.zip
feat(challenge-124,125,128/lubos-kolouch/perl,python/): Challenge 124 125 128 LK Perl Python
Diffstat (limited to 'challenge-125')
-rw-r--r--challenge-125/lubos-kolouch/perl/ch-1.pl24
-rw-r--r--challenge-125/lubos-kolouch/perl/ch-2.pl58
-rw-r--r--challenge-125/lubos-kolouch/python/ch-1.py21
-rw-r--r--challenge-125/lubos-kolouch/python/ch-2.py40
4 files changed, 143 insertions, 0 deletions
diff --git a/challenge-125/lubos-kolouch/perl/ch-1.pl b/challenge-125/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..0a68352207
--- /dev/null
+++ b/challenge-125/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub pythagorean_triples {
+ my $N = shift;
+ my $found = 0;
+ for my $a ( 1 .. $N ) {
+ for my $b ( $a .. $N ) {
+ my $c = sqrt( $a**2 + $b**2 );
+ if ( $c == int($c) ) {
+ if ( $a == $N || $b == $N || $c == $N ) {
+ print "($a, $b, $c)\n";
+ $found = 1;
+ }
+ }
+ }
+ }
+ print "-1\n" if not $found;
+}
+
+pythagorean_triples(5);
+pythagorean_triples(13);
+pythagorean_triples(1);
diff --git a/challenge-125/lubos-kolouch/perl/ch-2.pl b/challenge-125/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..a19c6e38f4
--- /dev/null
+++ b/challenge-125/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,58 @@
+use strict;
+use warnings;
+
+package Node {
+ use Moose;
+ has 'value' => ( is => 'ro' );
+ has 'left' => ( is => 'rw' );
+ has 'right' => ( is => 'rw' );
+}
+
+package BinaryTree {
+ use Moose;
+ has 'root' => ( is => 'rw' );
+
+ sub diameter {
+ my ($self) = @_;
+ my $max = 0;
+ _depth( $self->root, \$max );
+ return $max;
+ }
+
+ sub _depth {
+ my ( $node, $max ) = @_;
+ return 0 unless $node;
+
+ my $left = _depth( $node->left, $max );
+ my $right = _depth( $node->right, $max );
+ $$max = $left + $right if $left + $right > $$max;
+
+ return [ $left, $right ]->[ $left < $right ] + 1;
+ }
+}
+
+# Test
+my $tree = BinaryTree->new(
+ root => Node->new(
+ value => 1,
+ left => Node->new(
+ value => 2,
+ left => Node->new( value => 3 ),
+ right => Node->new( value => 4 )
+ ),
+ right => Node->new(
+ value => 5,
+ left => Node->new( value => 6 ),
+ right => Node->new(
+ value => 7,
+ left => Node->new(
+ value => 8,
+ left => Node->new( value => 9 )
+ ),
+ right => Node->new( value => 10 )
+ )
+ )
+ )
+);
+
+print $tree->diameter(); # prints: 6
diff --git a/challenge-125/lubos-kolouch/python/ch-1.py b/challenge-125/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..f78a53a143
--- /dev/null
+++ b/challenge-125/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def pythagorean_triples(N):
+ found = False
+ for a in range(1, N + 1):
+ for b in range(a, N + 1):
+ c = (a**2 + b**2) ** 0.5
+ if c.is_integer():
+ c = int(c)
+ if a == N or b == N or c == N:
+ print((a, b, c))
+ found = True
+ if not found:
+ print(-1)
+
+
+pythagorean_triples(5)
+pythagorean_triples(13)
+pythagorean_triples(1)
diff --git a/challenge-125/lubos-kolouch/python/ch-2.py b/challenge-125/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..b02ef4a3ac
--- /dev/null
+++ b/challenge-125/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+class Node:
+ def __init__(self, value, left=None, right=None):
+ self.value = value
+ self.left = left
+ self.right = right
+
+
+class BinaryTree:
+ def __init__(self, root):
+ self.root = root
+
+ def diameter(self):
+ _, diameter = self._depth(self.root)
+ return diameter
+
+ def _depth(self, node):
+ if not node:
+ return 0, 0
+
+ left_depth, left_diameter = self._depth(node.left)
+ right_depth, right_diameter = self._depth(node.right)
+ max_diameter = max(left_diameter, right_diameter, left_depth + right_depth)
+
+ return max(left_depth, right_depth) + 1, max_diameter
+
+
+# Test
+tree = BinaryTree(
+ Node(
+ 1,
+ Node(2, Node(3), Node(4)),
+ Node(5, Node(6), Node(7, Node(8, Node(9)), Node(10))),
+ )
+)
+
+print(tree.diameter()) # prints: 6