diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-07-18 20:38:42 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-07-18 20:38:42 +0200 |
| commit | d8021f9fbf19a8e254eb219800a92ac3341fe512 (patch) | |
| tree | 1c91810a72fc07d9c313922caec5c02219f9c52d | |
| parent | d5658ad8a3dfc590219ba295cb1824ec50540afe (diff) | |
| download | perlweeklychallenge-club-d8021f9fbf19a8e254eb219800a92ac3341fe512.tar.gz perlweeklychallenge-club-d8021f9fbf19a8e254eb219800a92ac3341fe512.tar.bz2 perlweeklychallenge-club-d8021f9fbf19a8e254eb219800a92ac3341fe512.zip | |
feat(challenge-226/lubos-kolouch/perl,python/): Challenge 226 LK Perl Python
| -rw-r--r-- | challenge-226/lubos-kolouch/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-226/lubos-kolouch/perl/ch-2.pl | 36 | ||||
| -rw-r--r-- | challenge-226/lubos-kolouch/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-226/lubos-kolouch/python/ch-2.py | 29 |
4 files changed, 119 insertions, 0 deletions
diff --git a/challenge-226/lubos-kolouch/perl/ch-1.pl b/challenge-226/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..e366d8dd7d --- /dev/null +++ b/challenge-226/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +sub shuffle_string { + my ( $string, @indices ) = @_; + + my @chars = split //, $string; + my @shuffled; + + for my $i ( 0 .. $#indices ) { + $shuffled[ $indices[$i] ] = $chars[$i]; + } + + return join '', @shuffled; +} + +my $string = "lacelengh"; +my @indices = ( 3, 2, 0, 5, 4, 8, 6, 7, 1 ); +is( shuffle_string( $string, @indices ), "challenge" ); + +$string = "rulepark"; +@indices = ( 4, 7, 3, 1, 0, 5, 2, 6 ); +is( shuffle_string( $string, @indices ), "perlraku" ); + +done_testing(); diff --git a/challenge-226/lubos-kolouch/perl/ch-2.pl b/challenge-226/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..94b51b53cb --- /dev/null +++ b/challenge-226/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use List::Util qw/min/; + +sub min_zero_operations { + my (@ints) = @_; + + my @positives = grep { $_ > 0 } @ints; + + return 0 if @positives == 0; + + my $smallest = min @positives; + + my $operations = 0; + + while ( $smallest > 0 ) { + for my $i ( 0 .. $#ints ) { + if ( $ints[$i] > 0 ) { + $ints[$i] -= $smallest; + } + } + $operations++; + + @positives = grep { $_ > 0 } @ints; + $smallest = @positives > 0 ? min(@positives) : 0; + } + + return $operations; +} + +print min_zero_operations( 0, 0, 0 ), "\n"; # 0 +print min_zero_operations( 1, 5, 0, 3, 5 ), "\n"; # 3 +print min_zero_operations(0), "\n"; # 0 +print min_zero_operations( 2, 1, 4, 0, 3 ), "\n"; # 4 diff --git a/challenge-226/lubos-kolouch/python/ch-1.py b/challenge-226/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..249c379d68 --- /dev/null +++ b/challenge-226/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +import unittest + + +def shuffle_string(string, indices): + chars = list(string) + shuffled = [None] * len(string) + + for i, idx in enumerate(indices): + shuffled[idx] = chars[i] + + return "".join(shuffled) + + +class TestShuffle(unittest.TestCase): + def test_shuffle(self): + string = "lacelengh" + indices = [3, 2, 0, 5, 4, 8, 6, 7, 1] + self.assertEqual(shuffle_string(string, indices), "challenge") + + string = "rulepark" + indices = [4, 7, 3, 1, 0, 5, 2, 6] + self.assertEqual(shuffle_string(string, indices), "perlraku") + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-226/lubos-kolouch/python/ch-2.py b/challenge-226/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..558c2b633a --- /dev/null +++ b/challenge-226/lubos-kolouch/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def min_zero_operations(ints): + positives = [x for x in ints if x > 0] + + if not positives: + return 0 + + smallest = min(positives) + + operations = 0 + + while smallest > 0: + for i in range(len(ints)): + if ints[i] > 0: + ints[i] -= smallest + operations += 1 + + positives = [x for x in ints if x > 0] + smallest = min(positives) if positives else 0 + + return operations + + +print(min_zero_operations([0, 0, 0])) # 0 +print(min_zero_operations([1, 5, 0, 3, 5])) # 3 +print(min_zero_operations([2, 1, 4, 0, 3])) # 4 |
