From 8930a15bf6e2d0d251e0993d62d8460342e52516 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Fri, 28 Aug 2020 00:32:43 +0100 Subject: Add nunovieira220 solution to challenge 075 --- challenge-075/nunovieira220/perl/ch-1.pl | 39 +++++++++++++ challenge-075/nunovieira220/perl/ch-2.pl | 97 ++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 challenge-075/nunovieira220/perl/ch-1.pl create mode 100644 challenge-075/nunovieira220/perl/ch-2.pl 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 -- cgit