diff options
| -rw-r--r-- | challenge-211/lubos-kolouch/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-211/lubos-kolouch/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-211/lubos-kolouch/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-211/lubos-kolouch/python/ch-2.py | 34 |
4 files changed, 134 insertions, 0 deletions
diff --git a/challenge-211/lubos-kolouch/perl/ch-1.pl b/challenge-211/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..3326f8686b --- /dev/null +++ b/challenge-211/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub is_toeplitz { + my ($matrix) = @_; + my $rows = @$matrix; + my $cols = @{ $matrix->[0] }; + + for ( my $i = 0 ; $i < $rows - 1 ; $i++ ) { + for ( my $j = 0 ; $j < $cols - 1 ; $j++ ) { + if ( $matrix->[$i]->[$j] != $matrix->[ $i + 1 ]->[ $j + 1 ] ) { + return 0; + } + } + } + return 1; +} + +# Test with example matrices +my @matrix1 = ( [ 4, 3, 2, 1 ], [ 5, 4, 3, 2 ], [ 6, 5, 4, 3 ], ); + +my @matrix2 = ( [ 1, 2, 3 ], [ 3, 2, 1 ], ); + +print is_toeplitz( \@matrix1 ) ? "true\n" : "false\n"; +print is_toeplitz( \@matrix2 ) ? "true\n" : "false\n"; diff --git a/challenge-211/lubos-kolouch/perl/ch-2.pl b/challenge-211/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1faa6b62ed --- /dev/null +++ b/challenge-211/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Algorithm::Combinatorics qw(combinations); + +sub can_split_with_same_average { + my ($nums) = @_; + my $total = 0; + $total += $_ for @$nums; + my $n = scalar @$nums; + + for my $i ( 1 .. $n - 1 ) { + my $combinations = combinations( $nums, $i ); + while ( my $comb = $combinations->next ) { + my $sum_comb = 0; + $sum_comb += $_ for @$comb; + if ( $sum_comb * ( $n - $i ) == ( $total - $sum_comb ) * $i ) { + return 1; + } + } + } + return 0; +} + +# Test cases +my @nums1 = ( 1, 2, 3, 4, 5, 6, 7, 8 ); +my @nums2 = ( 1, 3 ); + +# Tests +print can_split_with_same_average( \@nums1 ) ? "true\n" : "false\n"; +print can_split_with_same_average( \@nums2 ) ? "true\n" : "false\n"; diff --git a/challenge-211/lubos-kolouch/python/ch-1.py b/challenge-211/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..b73f247e94 --- /dev/null +++ b/challenge-211/lubos-kolouch/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def is_toeplitz(matrix: List[List[int]]) -> bool: + """ + Check if the given matrix is a Toeplitz Matrix. + + A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements. + + :param matrix: A list of lists containing integers. + :return: True if the matrix is Toeplitz, False otherwise. + """ + rows = len(matrix) + cols = len(matrix[0]) + + for i in range(rows - 1): + for j in range(cols - 1): + if matrix[i][j] != matrix[i + 1][j + 1]: + return False + + return True + + +# Test cases +matrix1 = [ + [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3], +] + +matrix2 = [ + [1, 2, 3], + [3, 2, 1], +] + +# Tests +assert is_toeplitz(matrix1) == True +assert is_toeplitz(matrix2) == False + +print("All tests passed!") diff --git a/challenge-211/lubos-kolouch/python/ch-2.py b/challenge-211/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..3342c43abe --- /dev/null +++ b/challenge-211/lubos-kolouch/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from itertools import combinations +from typing import List + + +def can_split_with_same_average(nums: List[int]) -> bool: + """ + Check if the given list of integers can be split into two separate lists with the same average. + + :param nums: A list of integers. + :return: True if the list can be split with the same average, False otherwise. + """ + total = sum(nums) + n = len(nums) + + for i in range(1, n): + for comb in combinations(nums, i): + sum_comb = sum(comb) + if sum_comb * (n - i) == (total - sum_comb) * i: + return True + return False + + +# Test cases +nums1 = [1, 2, 3, 4, 5, 6, 7, 8] +nums2 = [1, 3] + +# Tests +assert can_split_with_same_average(nums1) == True +assert can_split_with_same_average(nums2) == False + +print("All tests passed!") |
