diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-23 19:46:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-23 19:46:25 +0100 |
| commit | a0378812481339d92fae9464fbd9e98bcca04ff5 (patch) | |
| tree | 1a57de6f5a9b1bb1c80c4324209f47120e98a9aa | |
| parent | 1fc2447cb62d692c40a3309f57bce0c92435e14f (diff) | |
| parent | 7626fcb24af562c8f1d10c951f6fc0338a0df26b (diff) | |
| download | perlweeklychallenge-club-a0378812481339d92fae9464fbd9e98bcca04ff5.tar.gz perlweeklychallenge-club-a0378812481339d92fae9464fbd9e98bcca04ff5.tar.bz2 perlweeklychallenge-club-a0378812481339d92fae9464fbd9e98bcca04ff5.zip | |
Merge pull request #8129 from LubosKolouch/master
feat(challenge-072,218/lubos-kolouch/perl,python/): Challenges 072 and 218 LK Perl Python
| -rw-r--r-- | challenge-072/lubos-kolouch/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-072/lubos-kolouch/perl/ch-2.pl | 13 | ||||
| -rw-r--r-- | challenge-072/lubos-kolouch/python/ch-1.py | 17 | ||||
| -rw-r--r-- | challenge-072/lubos-kolouch/python/ch-2.py | 13 | ||||
| -rw-r--r-- | challenge-218/lubos-kolouch/perl/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-218/lubos-kolouch/perl/ch-2.pl | 47 | ||||
| -rw-r--r-- | challenge-218/lubos-kolouch/python/ch-1.py | 16 | ||||
| -rw-r--r-- | challenge-218/lubos-kolouch/python/ch-2.py | 34 |
8 files changed, 174 insertions, 0 deletions
diff --git a/challenge-072/lubos-kolouch/perl/ch-1.pl b/challenge-072/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..b0193d7141 --- /dev/null +++ b/challenge-072/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; + +sub trailing_zeros { + my ($n) = @_; + my $count = 0; + my $i = 5; + while ( $n / $i >= 1 ) { + $count += int( $n / $i ); + $i *= 5; + } + return $count; +} + +# Test cases: +print trailing_zeros(10); # Output: 2 +print "\n"; +print trailing_zeros(7); # Output: 1 +print "\n"; +print trailing_zeros(4); # Output: 0 diff --git a/challenge-072/lubos-kolouch/perl/ch-2.pl b/challenge-072/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ddd57cb59d --- /dev/null +++ b/challenge-072/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,13 @@ +use strict; +use warnings; + +sub display_lines { + my ( $file_name, $a, $b ) = @_; + open my $file, '<', $file_name or die "Could not open '$file_name': $!"; + my @lines = <$file>; + print @lines[ $a - 1 .. $b - 1 ]; + close $file; +} + +# Suppose 'input.txt' is a file in the same directory with content as described in the problem +display_lines( 'input.txt', 4, 12 ); diff --git a/challenge-072/lubos-kolouch/python/ch-1.py b/challenge-072/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..c61b1f14bc --- /dev/null +++ b/challenge-072/lubos-kolouch/python/ch-1.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def trailing_zeros(n: int) -> int: + count = 0 + i = 5 + while n // i >= 1: + count += n // i + i *= 5 + return count + + +# Test cases: +print(trailing_zeros(10)) # Output: 2 +print(trailing_zeros(7)) # Output: 1 +print(trailing_zeros(4)) # Output: 0 diff --git a/challenge-072/lubos-kolouch/python/ch-2.py b/challenge-072/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5525686a22 --- /dev/null +++ b/challenge-072/lubos-kolouch/python/ch-2.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def display_lines(file_name: str, a: int, b: int) -> None: + with open(file_name, "r") as file: + lines = file.readlines() + for line in lines[a - 1 : b]: + print(line.strip()) + + +# Suppose 'input.txt' is a file in the same directory with content as described in the problem +display_lines("input.txt", 4, 12) diff --git a/challenge-218/lubos-kolouch/perl/ch-1.pl b/challenge-218/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..332e9dbadc --- /dev/null +++ b/challenge-218/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,14 @@ +use strict; +use warnings; +use List::Util qw(min max); + +sub max_triplet_product { + my @nums = sort { $a <=> $b } @_; + return max($nums[0] * $nums[1] * $nums[-1], $nums[-1] * $nums[-2] * $nums[-3]); +} + +print max_triplet_product(3, 1, 2), "\n"; # Output: 6 +print max_triplet_product(4, 1, 3, 2), "\n"; # Output: 24 +print max_triplet_product(-1, 0, 1, 3, 1), "\n"; # Output: 3 +print max_triplet_product(-8, 2, -9, 0, -4, 3), "\n"; # Output: 216 + diff --git a/challenge-218/lubos-kolouch/perl/ch-2.pl b/challenge-218/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..77116a33fa --- /dev/null +++ b/challenge-218/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,47 @@ +use strict; +use warnings; + +# Function to flip a row or column +sub flip { + my ($arr_ref) = @_; + return [ map { 1 - $_ } @$arr_ref ]; +} + +# Function to convert binary to decimal +sub binary_to_decimal { + my ($binary_ref) = @_; + return oct("0b" . join('', @$binary_ref)); +} + +sub binary_matrix_score { + my ($matrix_ref) = @_; + my @matrix = @$matrix_ref; + + # First, optimize rows, make sure the first element is 1 + for my $row_ref (@matrix) { + if ($row_ref->[0] == 0) { + $row_ref = flip($row_ref); + } + } + + # Then, optimize columns excluding the first + my $cols = @{$matrix[0]}; + my $rows = scalar @matrix; + for my $col (1 .. $cols-1) { + if (scalar(grep {$matrix[$_][$col] == 0} 0 .. $rows-1) > $rows / 2) { + $_->[$col] = 1 - $_->[$col] for @matrix; + } + } + + # Calculate score + my $score = 0; + $score += binary_to_decimal($_) for @matrix; + + return $score; +} + +# Test cases: +print binary_matrix_score([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]); # Output: 39 +print "\n"; +print binary_matrix_score([[0]]); # Output: 1 + diff --git a/challenge-218/lubos-kolouch/python/ch-1.py b/challenge-218/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..fb0b261042 --- /dev/null +++ b/challenge-218/lubos-kolouch/python/ch-1.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def max_triplet_product(nums: List[int]) -> int: + nums.sort() + return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1]) + + +# Test cases: +print(max_triplet_product([3, 1, 2])) # Output: 6 +print(max_triplet_product([4, 1, 3, 2])) # Output: 24 +print(max_triplet_product([-1, 0, 1, 3, 1])) # Output: 3 +print(max_triplet_product([-8, 2, -9, 0, -4, 3])) # Output: 216 diff --git a/challenge-218/lubos-kolouch/python/ch-2.py b/challenge-218/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..c8d6620bb2 --- /dev/null +++ b/challenge-218/lubos-kolouch/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +def binary_matrix_score(matrix): + # Function to flip a row or column + def flip(arr): + return [1 - i for i in arr] + + # Function to convert binary to decimal + def binary_to_decimal(binary): + return int(''.join(map(str, binary)), 2) + + # First, optimize rows, make sure the first element is 1 + for row in matrix: + if row[0] == 0: + row[:] = flip(row) + + # Then, optimize columns excluding the first + cols = len(matrix[0]) + rows = len(matrix) + for col in range(1, cols): + if sum(matrix[row][col] for row in range(rows)) < rows / 2: + for row in range(rows): + matrix[row][col] = 1 - matrix[row][col] + + # Calculate score + score = sum(binary_to_decimal(row) for row in matrix) + + return score + + +# Test cases: +print(binary_matrix_score( + [[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]])) # Output: 39 +print(binary_matrix_score([[0]])) # Output: 1 |
