aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-05-01 10:54:22 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-05-01 10:54:22 +0200
commite91360307f1b27e544ac7108aa5ea49e7b30b58d (patch)
treefee210be1e7fc78409e1d82a951c9b0cdea48d16
parentcd1cf9c3a97571dca788b4e1a0c5ce74c05927ef (diff)
downloadperlweeklychallenge-club-e91360307f1b27e544ac7108aa5ea49e7b30b58d.tar.gz
perlweeklychallenge-club-e91360307f1b27e544ac7108aa5ea49e7b30b58d.tar.bz2
perlweeklychallenge-club-e91360307f1b27e544ac7108aa5ea49e7b30b58d.zip
Challenge 215 LK Perl Python
-rw-r--r--challenge-215/lubos-kolouch/perl/ch-1.pl35
-rw-r--r--challenge-215/lubos-kolouch/perl/ch-2.pl58
-rw-r--r--challenge-215/lubos-kolouch/python/ch-1.py33
-rw-r--r--challenge-215/lubos-kolouch/python/ch-2.py59
4 files changed, 185 insertions, 0 deletions
diff --git a/challenge-215/lubos-kolouch/perl/ch-1.pl b/challenge-215/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..b1dd470440
--- /dev/null
+++ b/challenge-215/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,35 @@
+use strict;
+use warnings;
+
+sub count_unsorted_words {
+ my @words = @_;
+ my $count = 0;
+
+ for my $word (@words) {
+ $count++ if is_unsorted($word);
+ }
+
+ return $count;
+}
+
+sub is_unsorted {
+ my $word = shift;
+ my $previous_char = substr( $word, 0, 1 );
+
+ for my $i ( 1 .. length($word) - 1 ) {
+ my $current_char = substr( $word, $i, 1 );
+ return 1 if $current_char lt $previous_char;
+ $previous_char = $current_char;
+ }
+
+ return 0;
+}
+
+# Test cases
+my @words1 = ( 'abc', 'xyz', 'tsu' );
+my @words2 = ( 'rat', 'cab', 'dad' );
+my @words3 = ( 'x', 'y', 'z' );
+
+print count_unsorted_words(@words1), "\n"; # Output: 1
+print count_unsorted_words(@words2), "\n"; # Output: 3
+print count_unsorted_words(@words3), "\n"; # Output: 0
diff --git a/challenge-215/lubos-kolouch/perl/ch-2.pl b/challenge-215/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..089451f7a8
--- /dev/null
+++ b/challenge-215/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,58 @@
+use strict;
+use warnings;
+
+sub can_replace_zeroes {
+ my ( $numbers_ref, $count ) = @_;
+ my @numbers = @$numbers_ref;
+
+ return 1 if $count == 0;
+
+ my @groups;
+ my $consecutive_zeroes = 0;
+
+ foreach my $num (@numbers) {
+ if ( $num == 0 ) {
+ $consecutive_zeroes++;
+ }
+ else {
+ if ( $consecutive_zeroes > 0 ) {
+ push @groups, $consecutive_zeroes;
+ $consecutive_zeroes = 0;
+ }
+ }
+ }
+
+ if ( $consecutive_zeroes > 0 ) {
+ push @groups, $consecutive_zeroes;
+ }
+
+ if ( scalar @groups == 1 && $groups[0] == scalar @numbers ) {
+ return 1 if ( $groups[0] + 1 ) / 2 >= $count;
+ return 0;
+ }
+
+ my $replaceable_zeroes = 0;
+ for my $i ( 0 .. $#groups ) {
+ if ( $i == 0 || $i == $#groups ) {
+ $replaceable_zeroes += int( $groups[$i] / 2 );
+ }
+ else {
+ $replaceable_zeroes += int( ( $groups[$i] - 1 ) / 2 );
+ }
+ }
+
+ return 1 if $replaceable_zeroes >= $count;
+ return 0;
+}
+
+# Test cases
+my @numbers1 = ( 1, 0, 0, 0, 1 );
+my $count1 = 1;
+my @numbers2 = ( 1, 0, 0, 0, 1 );
+my $count2 = 2;
+my @numbers3 = ( 1, 0, 0, 0, 0, 0, 0, 0, 1 );
+my $count3 = 3;
+
+print can_replace_zeroes( \@numbers1, $count1 ), "\n"; # Output: 1
+print can_replace_zeroes( \@numbers2, $count2 ), "\n"; # Output: 0
+print can_replace_zeroes( \@numbers3, $count3 ), "\n"; # Output: 1
diff --git a/challenge-215/lubos-kolouch/python/ch-1.py b/challenge-215/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..d5282fb7fa
--- /dev/null
+++ b/challenge-215/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def count_unsorted_words(words):
+ count = 0
+
+ for word in words:
+ count += is_unsorted(word)
+
+ return count
+
+
+def is_unsorted(word):
+ previous_char = word[0]
+
+ for i in range(1, len(word)):
+ current_char = word[i]
+ if current_char < previous_char:
+ return 1
+ previous_char = current_char
+
+ return 0
+
+
+# Test cases
+words1 = ["abc", "xyz", "tsu"]
+words2 = ["rat", "cab", "dad"]
+words3 = ["x", "y", "z"]
+
+print(count_unsorted_words(words1)) # Output: 1
+print(count_unsorted_words(words2)) # Output: 3
+print(count_unsorted_words(words3)) # Output: 0
diff --git a/challenge-215/lubos-kolouch/python/ch-2.py b/challenge-215/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..4b34d43d54
--- /dev/null
+++ b/challenge-215/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def can_replace_zeroes(numbers: List[int], count: int) -> int:
+ """
+ Determine whether it is possible to replace the specified number of zeroes
+ in the given list of numbers containing only zeroes and ones.
+
+ Args:
+ numbers (List[int]): The list of numbers containing only zeroes and ones.
+ count (int): The number of zeroes to replace.
+
+ Returns:
+ int: 1 if it is possible to replace the specified number of zeroes, 0 otherwise.
+ """
+ if count == 0:
+ return 1
+
+ groups = []
+ consecutive_zeroes = 0
+
+ for num in numbers:
+ if num == 0:
+ consecutive_zeroes += 1
+ else:
+ if consecutive_zeroes > 0:
+ groups.append(consecutive_zeroes)
+ consecutive_zeroes = 0
+
+ if consecutive_zeroes > 0:
+ groups.append(consecutive_zeroes)
+
+ if len(groups) == 1 and groups[0] == len(numbers):
+ return 1 if (groups[0] + 1) // 2 >= count else 0
+
+ replaceable_zeroes = 0
+ for i, group in enumerate(groups):
+ if i == 0 or i == len(groups) - 1:
+ replaceable_zeroes += group // 2
+ else:
+ replaceable_zeroes += (group - 1) // 2
+
+ return 1 if replaceable_zeroes >= count else 0
+
+
+# Test cases
+numbers1 = [1, 0, 0, 0, 1]
+count1 = 1
+numbers2 = [1, 0, 0, 0, 1]
+count2 = 2
+numbers3 = [1, 0, 0, 0, 0, 0, 0, 0, 1]
+count3 = 3
+
+print(can_replace_zeroes(numbers1, count1)) # Output: 1
+print(can_replace_zeroes(numbers2, count2)) # Output: 0
+print(can_replace_zeroes(numbers3, count3)) # Output: 1