diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-05 07:27:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-05 07:27:36 +0000 |
| commit | bece936a46afbfa066a96aef5f65b0e6a9c4bd32 (patch) | |
| tree | 5a29f0028bd352ab3e74235cddfb29556385a7b4 | |
| parent | d6adedc738b5d22b6c6e315197b5e0716ef5a7c3 (diff) | |
| parent | 1608da594dbaaa0999a4d7ddf0ee52c2c62c3622 (diff) | |
| download | perlweeklychallenge-club-bece936a46afbfa066a96aef5f65b0e6a9c4bd32.tar.gz perlweeklychallenge-club-bece936a46afbfa066a96aef5f65b0e6a9c4bd32.tar.bz2 perlweeklychallenge-club-bece936a46afbfa066a96aef5f65b0e6a9c4bd32.zip | |
Merge pull request #7661 from kjetillll/challenge-206-kjetillll
https://theweeklychallenge.org/blog/perl-weekly-challenge-206/
| -rw-r--r-- | challenge-206/kjetillll/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-206/kjetillll/perl/ch-2.pl | 37 |
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-206/kjetillll/perl/ch-1.pl b/challenge-206/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..90c9e4e757 --- /dev/null +++ b/challenge-206/kjetillll/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; use strict; use List::Util 'reduce'; + +my @time = @ARGV; #input from command line args +@time = ("10:10", "09:30", "09:00", "09:55") if not @time; #...or use test case + +print "Input: @time\n"; +print "Interval in minutes: ", shortest_time(@time), "\n"; + +sub shortest_time { + ( + reduce { + my($last,$min) = ref$a ? @$a : ($a,undef); + my $diff = $b - $last; + [$b, !defined$min || $diff < $min ? $diff : $min] + } + map{ my($hh,$mm) = split/:/; $hh*60 + $mm } + sort @_ + )[0][1] +} diff --git a/challenge-206/kjetillll/perl/ch-2.pl b/challenge-206/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..0ec1e5e59e --- /dev/null +++ b/challenge-206/kjetillll/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl +use warnings; use strict; +#use Acme::Tools; + +my @array = @ARGV; #input from command line args +@array = (1,2,3,4) if not @array; #...or use test case +#@array = (0,2,1,3) if not @array; #...or use test case + +print "Input: @array\n"; +print "Max sum: ", maxsum(@array), "\n"; + + +sub maxsum { + my $max; + for my $perm_array ( perm(@_) ){ + my $sum; + while(@$perm_array){ #loop the pairs ($a,$b) of current permutation + my($a,$b) = splice@$perm_array,0,2; + $sum += $a<$b ? $a : $b; + } + $max = $sum if !defined$max or $sum>$max; + } + $max +} + +sub perm { + my(@i,@r) = 0..$#_; + @_ || return; + while ( push @r, [@_[@i]] ) { + my $p = $#i || last; + --$p || last while $i[$p-1] > $i[$p]; + push @i, reverse splice @i, my$q=$p; + ++$q while $i[$p-1] > $i[$q]; + @i[$p-1,$q] = @i[$q,$p-1]; + } + @r +} |
