diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-21 18:58:02 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-21 18:58:02 +0000 |
| commit | f34b08b6bcaed026727296cf9be4f862984de653 (patch) | |
| tree | 8ba208e5c88475226bcc22a3b8319607df80f230 /challenge-100 | |
| parent | 7ab7e7265f053d572871f50cc117524da420f6c9 (diff) | |
| parent | 751e806cac52496594680c84ee6af27e4c730c5c (diff) | |
| download | perlweeklychallenge-club-f34b08b6bcaed026727296cf9be4f862984de653.tar.gz perlweeklychallenge-club-f34b08b6bcaed026727296cf9be4f862984de653.tar.bz2 perlweeklychallenge-club-f34b08b6bcaed026727296cf9be4f862984de653.zip | |
Merge pull request #3592 from kaicb97/branch-for-challenge-100
solution for week 100
Diffstat (limited to 'challenge-100')
| -rwxr-xr-x | challenge-100/kai-burgdorf/perl/ch-1.pl | 31 | ||||
| -rwxr-xr-x | challenge-100/kai-burgdorf/perl/ch-2.pl | 53 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-100/kai-burgdorf/perl/ch-1.pl b/challenge-100/kai-burgdorf/perl/ch-1.pl new file mode 100755 index 0000000000..5ec75daf10 --- /dev/null +++ b/challenge-100/kai-burgdorf/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $input = $ARGV[0]; #8:30 pm + +die "usage: ch-1.pl [timestring]" if ( !$input ); + +my ( $hours, $minutes ) = split( ":", $input, 2 ); + +my $result; + +if ( $minutes =~ /.*am*|.*pm*/ ) { + my $is_am = ( $minutes =~ /.*am/ ) ? 1 : 0; + $minutes =~ s/am|pm//; #optionales space in regex + $result = ($is_am) ? "$hours:$minutes\n" : "" . ( $hours + 12 ) . ":$minutes\n"; +} +else { + my $is_early = ( $hours < 12 || $hours == 24 ) ? 1 : 0; + if ( $hours == 24 || $hours == 0 ) { + $hours = 12; + } + elsif ( !$is_early && $hours > 12 ) { + $hours -= 12; + } + $result = "$hours:$minutes"; + $result .= ($is_early) ? "am\n" : "pm\n"; +} + +print "$result"; diff --git a/challenge-100/kai-burgdorf/perl/ch-2.pl b/challenge-100/kai-burgdorf/perl/ch-2.pl new file mode 100755 index 0000000000..39ff67dc79 --- /dev/null +++ b/challenge-100/kai-burgdorf/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +#Example 1: +#my @triangle = ( [1], [ 2, 4 ], [ 6, 4, 9 ], [ 5, 1, 7, 2 ] ); #output 8 +#Example 2: +my @triangle = ( [3], [ 3, 1 ], [ 5, 2, 3 ], [ 4, 3, 1, 3 ] ); #output 7 + +my $lowest_sum; +get_sum_for_path(); +print "lowest: $lowest_sum\n"; + +sub get_right_child { + my ( $i, $j ) = @_; + if ( defined $triangle[ $i + 1 ][ $j + 1 ] ) { + return { is_child => 1, row => $i + 1, col => $j + 1, value => $triangle[ $i + 1 ][ $j + 1 ] }; + } + return { is_child => 0 }; +} + +sub get_left_child { + my ( $i, $j ) = @_; + if ( defined $triangle[ $i + 1 ][$j] ) { + return { is_child => 1, row => $i + 1, col => $j }; + } + return { is_child => 0 }; +} + +sub get_sum_for_path { + my ( $i, $j, $accu ) = @_; + + if ( !$accu ) { $accu = 0; } + if ( !$i ) { $i = 0; } + if ( !$j ) { $j = 0; } + + $accu += $triangle[$i][$j]; + + my $left_child = get_left_child( $i, $j ); + my $right_child = get_right_child( $i, $j ); + + if ( $left_child->{is_child} == 0 && $right_child->{is_child} == 0 ) { #found a leave + if ( !$lowest_sum || $accu < $lowest_sum ) { + $lowest_sum = $accu; + return; + } + } + else { + get_sum_for_path( $left_child->{row}, $left_child->{col}, $accu ); + get_sum_for_path( $right_child->{row}, $right_child->{col}, $accu ); + } +} |
