diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-28 00:57:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-28 00:57:54 +0100 |
| commit | 84aac3d124067c0bc8e313355449d86ab588c2fd (patch) | |
| tree | 295be3625b458b65454a4453847c6045436fe459 | |
| parent | 0ea82e2833f6b6643df0e51dba7ff791e289eb0d (diff) | |
| parent | 8930a15bf6e2d0d251e0993d62d8460342e52516 (diff) | |
| download | perlweeklychallenge-club-84aac3d124067c0bc8e313355449d86ab588c2fd.tar.gz perlweeklychallenge-club-84aac3d124067c0bc8e313355449d86ab588c2fd.tar.bz2 perlweeklychallenge-club-84aac3d124067c0bc8e313355449d86ab588c2fd.zip | |
Merge pull request #2157 from nunovieira220/challenge-075
Add solution to challenge 075
| -rw-r--r-- | challenge-075/nunovieira220/perl/ch-1.pl | 39 | ||||
| -rw-r--r-- | challenge-075/nunovieira220/perl/ch-2.pl | 97 |
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-075/nunovieira220/perl/ch-1.pl b/challenge-075/nunovieira220/perl/ch-1.pl new file mode 100644 index 0000000000..4074a86e7d --- /dev/null +++ b/challenge-075/nunovieira220/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# Get coin sum +sub get_coin_sum { + my ($limit, @sets) = @_; + my $len = scalar @sets; + + return 0 if($len == 0); + + my %vals = (); + $vals{0} = 1; + $vals{$limit} = 0; + + foreach my $set (@sets) { + last if($set > $limit); + + for(my $i = $set; $i <= $limit; $i++) { + my $diff = $vals{$i - $set}; + if($diff) { + $vals{$i} = $vals{$i} ? $vals{$i} + $diff : $diff; + } + } + } + + return $vals{$limit}; +} + +# Input/Output +if(scalar @ARGV > 0) { + my $limit = shift @ARGV; + my @sets = grep {$_} (sort @ARGV); + print get_coin_sum($limit, @sets)."\n"; +} else { + my @arr = (1,2,4); + print get_coin_sum(6, @arr)."\n"; +}
\ No newline at end of file diff --git a/challenge-075/nunovieira220/perl/ch-2.pl b/challenge-075/nunovieira220/perl/ch-2.pl new file mode 100644 index 0000000000..6949156df7 --- /dev/null +++ b/challenge-075/nunovieira220/perl/ch-2.pl @@ -0,0 +1,97 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use List::Util qw[max]; + +# Print histogram +sub print_histogram { + my @lines = @_; + my $len = scalar @lines; + my $max = scalar @{$lines[0]}; + my @nums = (); + + print "\n"; + + for (my $i = $max - 1; $i >= 0; $i--) { + print ($i+1); + print " | "; + my $counter = 0; + + foreach my $l (@lines) { + my @line = @{$l}; + print $line[$i]." "; + + $counter++ if($line[$i] eq "#"); + } + + push @nums, $counter; + print "\n"; + } + + print "- | "; + print "- " for (@lines); + print "\n"; + + print "- | "; + for my $l (@lines) { + my $count = grep { $_ eq '#' } @{$l}; + print $count." "; + }; + + print "\n\n"; +} + +# Handle histogram +sub histogram { + my $out = shift @_; + my $len = scalar @_; + + return 0 if($len == 0); + + my $max = max(@_) + 1; + my @bonus = (); + my @vals = (0) x $max; + my $area = 0; + + foreach my $val (@_) { + my @line = (); + + if($val != 0) { + for(my $i = 1; $i <= $val; $i++) { + $vals[$i] += $i; + $area = max($vals[$i], $area); + push @line, '#'; + } + + for(my $i = $val + 1; $i < $max; $i++) { + $vals[$i] = 0; + push @line, ' '; + } + } else { + @vals = (0) x $max; + } + + push @bonus, \@line; + } + + if($out) { + print_histogram(@bonus); + } + + return $area; +} + +# Flag for printing +my $out = 1; + +# Input/Output +if(scalar @ARGV > 0) { + print "Output: ".histogram($out , @ARGV)."\n"; +} else { + my @arr1 = (2, 1, 4, 5, 3, 7); + print "Output: ".histogram($out , @arr1)."\n"; + + my @arr2 = (3, 2, 3, 5, 7, 5); + print "Output: ".histogram($out, @arr2)."\n"; +}
\ No newline at end of file |
