From 2a02ca9b0fbde27d1958c6deebee1bbd4b2d0817 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 24 Jul 2023 02:41:11 +0100 Subject: - Added solutions by Roger Bell_West. - Added solutions by Simon Green. - Added solutions by Jan Krnavek. - Added solutions by Adam Russell. - Added solutions by Avery Adams. - Added solutions by Steven Wilson. - Added solutions by Cheok-Yin Fung. - Added solutions by BarrOff. - Added solutions by Bruce Gray. --- challenge-226/steven-wilson/perl/ch-02.pl | 36 ------------------------------- challenge-226/steven-wilson/perl/ch-2.pl | 36 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 challenge-226/steven-wilson/perl/ch-02.pl create mode 100644 challenge-226/steven-wilson/perl/ch-2.pl (limited to 'challenge-226') diff --git a/challenge-226/steven-wilson/perl/ch-02.pl b/challenge-226/steven-wilson/perl/ch-02.pl deleted file mode 100644 index 82bed226d8..0000000000 --- a/challenge-226/steven-wilson/perl/ch-02.pl +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -use List::Util qw/ min /; -use Test::More; - -my @ints_1 = (1, 5, 0, 3, 5); -my @ints_2 = (0); -my @ints_3 = (2, 1, 4, 0, 3); -cmp_ok( min_zero_ops( @ints_1 ), '==', 3, "Example 1" ); -cmp_ok( min_zero_ops( @ints_2 ), '==', 0, "Example 2" ); -cmp_ok( min_zero_ops( @ints_3 ), '==', 4, "Example 3" ); -done_testing(); - -sub min_zero_ops { - my @i = @_; - my $smallest; - my @non_zero; - my $operations = 0; - for (0 .. (scalar @i -1 )) { - if ( $i[$_] != 0 ) { - push @non_zero, $_; - } - } - while ( @non_zero != 0 ) { - $smallest = min( @i[ @non_zero ]); - for (@non_zero) { - $i[$_] -= $smallest; - } - @non_zero = grep { $i[$_] > 0 } @non_zero; - $operations++; - } - return $operations; -} - diff --git a/challenge-226/steven-wilson/perl/ch-2.pl b/challenge-226/steven-wilson/perl/ch-2.pl new file mode 100644 index 0000000000..82bed226d8 --- /dev/null +++ b/challenge-226/steven-wilson/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use List::Util qw/ min /; +use Test::More; + +my @ints_1 = (1, 5, 0, 3, 5); +my @ints_2 = (0); +my @ints_3 = (2, 1, 4, 0, 3); +cmp_ok( min_zero_ops( @ints_1 ), '==', 3, "Example 1" ); +cmp_ok( min_zero_ops( @ints_2 ), '==', 0, "Example 2" ); +cmp_ok( min_zero_ops( @ints_3 ), '==', 4, "Example 3" ); +done_testing(); + +sub min_zero_ops { + my @i = @_; + my $smallest; + my @non_zero; + my $operations = 0; + for (0 .. (scalar @i -1 )) { + if ( $i[$_] != 0 ) { + push @non_zero, $_; + } + } + while ( @non_zero != 0 ) { + $smallest = min( @i[ @non_zero ]); + for (@non_zero) { + $i[$_] -= $smallest; + } + @non_zero = grep { $i[$_] > 0 } @non_zero; + $operations++; + } + return $operations; +} + -- cgit