diff options
| -rw-r--r-- | challenge-152/lubos-kolouch/java/ch-1.java | 36 | ||||
| -rw-r--r-- | challenge-152/lubos-kolouch/java/ch-2.java | 58 | ||||
| -rw-r--r-- | challenge-152/lubos-kolouch/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-152/lubos-kolouch/perl/ch-2.pl | 51 | ||||
| -rw-r--r-- | challenge-152/lubos-kolouch/php/ch-1.php | 20 | ||||
| -rw-r--r-- | challenge-152/lubos-kolouch/php/ch-2.php | 56 | ||||
| -rw-r--r-- | challenge-152/lubos-kolouch/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-152/lubos-kolouch/python/ch-2.py | 43 |
8 files changed, 312 insertions, 0 deletions
diff --git a/challenge-152/lubos-kolouch/java/ch-1.java b/challenge-152/lubos-kolouch/java/ch-1.java new file mode 100644 index 0000000000..46bda99f11 --- /dev/null +++ b/challenge-152/lubos-kolouch/java/ch-1.java @@ -0,0 +1,36 @@ +class MinPathFinder { + + static int get_min_count(int[][] in_arr) { + int min_sum = 0; + + int i; + for (i = 0; i < in_arr.length; i++) { + int min = in_arr[i][0]; + + int j; + for (j = 0; j < in_arr[i].length; j++) { + if (in_arr[i][j] < min) { + min = in_arr[i][j]; + } + } + + min_sum += min; + } + return min_sum; + } + + public static void main(String[] args) { + + int[][] my_list = {{1}, {5, 3}, {2, 3, 4}, {7, 1, 0, 2}, {6, 4, 5, 2, 8}}; + + if (get_min_count(my_list) != 8) { + System.out.println("Failed test 1"); + } + + int[][] my_list2 = {{5}, {2, 3}, {4, 1, 5}, {0, 1, 2, 3}, {7, 2, 4, 1, 9}}; + + if (get_min_count(my_list2) != 9) { + System.out.println("Failed test 2"); + } + } +} diff --git a/challenge-152/lubos-kolouch/java/ch-2.java b/challenge-152/lubos-kolouch/java/ch-2.java new file mode 100644 index 0000000000..85e10266cb --- /dev/null +++ b/challenge-152/lubos-kolouch/java/ch-2.java @@ -0,0 +1,58 @@ +class Point { + int x, y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } +}; + +class Rectangle { + Point left, right; + + public Rectangle(Point left, Point right) { + this.left = left; + this.right = right; + } + + public int get_area() { + return Math.abs(this.left.x - this.right.x) * + Math.abs(this.left.y - this.right.y); + } +}; + +class OverlapArea { + + static int get_total_area(Rectangle first, Rectangle second) { + int area1 = first.get_area(); + int area2 = second.get_area(); + + // calculate the overlapping area + int x_dist = Math.min(first.right.x, second.right.x) - + Math.max(first.left.x, second.left.x); + int y_dist = Math.min(first.right.y, second.right.y) - + Math.max(first.left.y, second.left.y); + int areaI = 0; + if (x_dist > 0 && y_dist > 0) { + areaI = x_dist * y_dist; + } + + return (area1 + area2 - areaI); + } + + public static void main(String[] args) { + Rectangle first = new Rectangle(new Point(-1, 0), new Point(2, 2)); + Rectangle second = new Rectangle(new Point(0, -1), new Point(4, 4)); + + if (get_total_area(first, second) != 22) { + System.out.println("Failed test 1"); + } + + first = new Rectangle(new Point(-3, -1), new Point(1, 3)); + second = new Rectangle(new Point(-1, -3), new Point(2, 2)); + + if (get_total_area(first, second) != 25) { + System.out.println("Failed test 2"); + } + } +} diff --git a/challenge-152/lubos-kolouch/perl/ch-1.pl b/challenge-152/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..be4e9ea03b --- /dev/null +++ b/challenge-152/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; +use List::Util qw/min/; + +sub get_min_count { + my @in_arr = @_; + + my $min_sum = 0; + + for my $sub_arr (@in_arr) { + + # so obviously it is not a tree, just independent arrays + # so I can just take a min from each row + + $min_sum += min(@$sub_arr); + } + + return $min_sum; +} + +use Test::More; + +is( get_min_count( [1], [ 5, 3 ], [ 2, 3, 4 ], [ 7, 1, 0, 2 ], [ 6, 4, 5, 2, 8 ] ), 8 ); +is( get_min_count( [5], [ 2, 3 ], [ 4, 1, 5 ], [ 0, 1, 2, 3 ], [ 7, 2, 4, 1, 9 ] ), 9 ); + +done_testing; diff --git a/challenge-152/lubos-kolouch/perl/ch-2.pl b/challenge-152/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ac1b5aea31 --- /dev/null +++ b/challenge-152/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,51 @@ +use strict; +use warnings; +use Moose; + +package Point; +use Moose; + +has 'x' => ( is => 'rw', isa => 'Int' ); +has 'y' => ( is => 'rw', isa => 'Int' ); + +package Rectangle; +use Moose; +use List::Util qw/min max/; + +has 'left' => ( is => 'rw', isa => 'Point' ); +has 'right' => ( is => 'rw', isa => 'Point' ); + +sub get_area { + my $self = shift; + return abs( $self->left->x - $self->right->x ) * abs( $self->left->y - $self->right->y ); +} + +sub get_total_area { + + my ( $first, $second ) = @_; + + my $area1 = $first->get_area; + my $area2 = $second->get_area; + + # calculate the overlapping area + my $x_dist = min( $first->right->x, $second->right->x ) - max( $first->left->x, $second->left->x ); + my $y_dist = min( $first->right->y, $second->right->y ) - max( $first->left->y, $second->left->y ); + my $area_i = 0; + $area_i = $x_dist * $y_dist if ( $x_dist > 0 ) and ( $y_dist > 0 ); + + return $area1 + $area2 - $area_i; +} + +use Test::More; + +my $first = Rectangle->new( left => Point->new( x => -1, y => 0 ), right => Point->new( x => 2, y => 2 ) ); +my $second = Rectangle->new( left => Point->new( x => 0, y => -1 ), right => Point->new( x => 4, y => 4 ) ); + +is( get_total_area( $first, $second ), 22 ); + +$first = Rectangle->new( left => Point->new( x => -3, y => -1 ), right => Point->new( x => 1, y => 3 ) ); +$second = Rectangle->new( left => Point->new( x => -1, y => -3 ), right => Point->new( x => 2, y => 2 ) ); + +is( get_total_area( $first, $second ), 25 ); + +done_testing; diff --git a/challenge-152/lubos-kolouch/php/ch-1.php b/challenge-152/lubos-kolouch/php/ch-1.php new file mode 100644 index 0000000000..f8e66e2224 --- /dev/null +++ b/challenge-152/lubos-kolouch/php/ch-1.php @@ -0,0 +1,20 @@ +<?php + +function get_min_count($in_arr) { + + $min_sum = 0; + + foreach ($in_arr as $sub_arr) { + + # so obviously it is not a tree, just independent arrays + # so I can just take a min from each row + + $min_sum += min($sub_arr); + } + + return $min_sum; +} + +get_min_count([ [1], [ 5, 3 ], [ 2, 3, 4 ], [ 7, 1, 0, 2 ], [ 6, 4, 5, 2, 8 ] ]) == 8 or throw new Exception("Test1 failed"); +get_min_count([ [5], [ 2, 3 ], [ 4, 1, 5 ], [ 0, 1, 2, 3 ], [ 7, 2, 4, 1, 9 ] ]) == 9 or throw new Exception("Test2 failed"); +?> diff --git a/challenge-152/lubos-kolouch/php/ch-2.php b/challenge-152/lubos-kolouch/php/ch-2.php new file mode 100644 index 0000000000..d115137e0b --- /dev/null +++ b/challenge-152/lubos-kolouch/php/ch-2.php @@ -0,0 +1,56 @@ +<?php + +class Point { + + public int $x; + public int $y; + + public function __construct(int $x, int $y = 0) { + $this->x = $x; + $this->y = $y; + } + +} + +class Rectangle { + + public Point $left; + public Point $right; + + public function __construct(Point $left, Point $right) { + $this->left = $left; + $this->right = $right; + } + public function get_area() { + return abs( $this->left->x - $this->right->x ) * abs( $this->left->y - $this->right->y ); + } +} + + + +function get_total_area(Rectangle $first, Rectangle $second) { + + $area1 = $first->get_area(); + $area2 = $second->get_area(); + + # calculate the overlapping area + $x_dist = min( $first->right->x, $second->right->x ) - max( $first->left->x, $second->left->x ); + $y_dist = min( $first->right->y, $second->right->y ) - max( $first->left->y, $second->left->y ); + $area_i = 0; + if (( $x_dist > 0 ) and ( $y_dist > 0 )) { + $area_i = $x_dist * $y_dist; + } + + return $area1 + $area2 - $area_i; +} + +$first = new Rectangle(new Point(-1, 0), new Point(2, 2) ); +$second = new Rectangle(new Point(0, -1), new Point(4, 4) ); + +get_total_area( $first, $second ) == 22 or throw new Exception("Test 1 failed"); + +$first = new Rectangle(new Point(-3, -1), new Point(1, 3) ); +$second = new Rectangle(new Point(-1, -3), new Point(2, 2) ); + +get_total_area( $first, $second ) == 25 or throw new Exception("Test 2 failed"); +?> diff --git a/challenge-152/lubos-kolouch/python/ch-1.py b/challenge-152/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..6e170349ca --- /dev/null +++ b/challenge-152/lubos-kolouch/python/ch-1.py @@ -0,0 +1,22 @@ +""" Challenge 152 Task 1 """ + + +def get_min_count(in_arr: list): + """ Find out the lists minimum """ + + min_sum = 0 + + for sub_arr in in_arr: + + # so obviously it is not a tree, just independent arrays + # so I can just take a min from each row + + min_sum += min(sub_arr) + + return min_sum + + +assert get_min_count([[1], [5, 3], [2, 3, 4], [7, 1, 0, 2], [6, 4, 5, 2, + 8]]) == 8 +assert get_min_count([[5], [2, 3], [4, 1, 5], [0, 1, 2, 3], [7, 2, 4, 1, + 9]]) == 9 diff --git a/challenge-152/lubos-kolouch/python/ch-2.py b/challenge-152/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..a7fe673bd6 --- /dev/null +++ b/challenge-152/lubos-kolouch/python/ch-2.py @@ -0,0 +1,43 @@ +class Point: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + +class Rectangle: + def __init__(self, left: Point, right: Point): + self.left = left + self.right = right + + def get_area(self) -> int: + return abs(self.left.x - self.right.x) * abs(self.left.y - + self.right.y) + + +def get_total_area(first: Rectangle, second: Rectangle) -> int: + area1 = first.get_area() + area2 = second.get_area() + + # calculate the overlapping area + x_dist = min(first.right.x, second.right.x) - \ + max(first.left.x, second.left.x) + y_dist = min(first.right.y, second.right.y) - \ + max(first.left.y, second.left.y) + area_i = 0 + if x_dist > 0 and y_dist > 0: + area_i = x_dist * y_dist + + return area1 + area2 - area_i + + +first = Rectangle(Point(-1, 0), Point(2, 2)) +second = Rectangle(Point(0, -1), Point(4, 4)) + +if get_total_area(first, second) != 22: + raise Exception("Failed test 1") + +first = Rectangle(Point(-3, -1), Point(1, 3)) +second = Rectangle(Point(-1, -3), Point(2, 2)) + +if get_total_area(first, second) != 25: + raise Exception("Failed test 2") |
