diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-08-22 14:59:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-22 14:59:36 +0100 |
| commit | 5f06c52272d7da2c755a4dfe0f85ba1d5733d56c (patch) | |
| tree | 1a103f0111ca0f34cbc9d79717885c1eea24e547 | |
| parent | dbb96034773a87fc1283697c57b32447850ee644 (diff) | |
| parent | 4d2123b57d0adb3e71526802aa840f54f96a9a7d (diff) | |
| download | perlweeklychallenge-club-5f06c52272d7da2c755a4dfe0f85ba1d5733d56c.tar.gz perlweeklychallenge-club-5f06c52272d7da2c755a4dfe0f85ba1d5733d56c.tar.bz2 perlweeklychallenge-club-5f06c52272d7da2c755a4dfe0f85ba1d5733d56c.zip | |
Merge pull request #6640 from jaldhar/challenge-178
Challenge 178 by Jaldhar H. Vyas
| -rw-r--r-- | challenge-178/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-178/jaldhar-h-vyas/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-178/jaldhar-h-vyas/perl/ch-2.pl | 60 | ||||
| -rwxr-xr-x | challenge-178/jaldhar-h-vyas/raku/ch-1.sh | 3 | ||||
| -rwxr-xr-x | challenge-178/jaldhar-h-vyas/raku/ch-2.raku | 56 |
5 files changed, 148 insertions, 0 deletions
diff --git a/challenge-178/jaldhar-h-vyas/blog.txt b/challenge-178/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..b71a1d2383 --- /dev/null +++ b/challenge-178/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2022/08/perl_weekly_challenge_week_178.html
\ No newline at end of file diff --git a/challenge-178/jaldhar-h-vyas/perl/ch-1.pl b/challenge-178/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..ef0f386183 --- /dev/null +++ b/challenge-178/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use 5.030; +use warnings; +use Math::Prime::Util qw/ todigits /; +use POSIX qw/ floor /; + +# based on http://www.rosettacode.org/wiki/Negative_base_numbers#Perl +sub quaterImaginaryBase { + my($n) = @_; + my @result; + my $r = 0; + + while ($n) { + $r = $n % -4; + $n = floor($n / -4); + if ($r < 0) { + $n++; + $r += 4; + } + push @result, todigits($r, 4) || 0; + } + + return join '0', reverse @result; +} + +my ($n) = shift // die "Need an integer\n"; + +say quaterImaginaryBase($n);
\ No newline at end of file diff --git a/challenge-178/jaldhar-h-vyas/perl/ch-2.pl b/challenge-178/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..1007e7e389 --- /dev/null +++ b/challenge-178/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use 5.030; +use warnings; +use DateTime; +use English; + +sub nextBusinessDay { + my ($dt) = @_; + my $next = $dt->clone; + if ($dt->day_of_week == 5) { + $next->add(days => 3); + } elsif ($dt->day_of_week == 6) { + $next->add(days => 2); + } else { + $next->add(days => 1); + } + + return $next->set(hour => 9, minute => 0) +} + +sub usage() { + print<<"-USAGE-"; +Usage: + $PROGRAM_NAME <timestamp> <duration> + + <timestamp> a datetime string in the format YYYY-MM-DD HH:MM + <duration> a duration as a decimal number of hours +-USAGE- + exit 0; +} + +if (scalar @ARGV != 2) { + usage; +} + +my ($timestamp, $duration) = @ARGV; + +my ($year, $month, $day, $hour, $minute); +if ($timestamp =~ / ^ (\d{4}) [-] (\d{2}) \- (\d{2}) [ ] (\d{2}) [:] (\d{2}) $/msx) { + ($year, $month, $day, $hour, $minute) = @{^CAPTURE}; +} else { + die "Bad timestamp format\n"; +} + +my $start = DateTime->new( + year => $year, + month => $month, + day => $day, + hour => $hour, + minute => $minute +); + +my $endOfDay = $start->clone->set(hour => 18, minute => 0); +my $endOfDuration = $start->clone->add(hours => $duration); +if ($endOfDuration <= $endOfDay) { + say $endOfDuration->strftime('%F %H:%M'); +} else { + my $difference = $endOfDuration - $endOfDay; + say nextBusinessDay($start)->add($difference)->strftime('%F %H:%M'); +} diff --git a/challenge-178/jaldhar-h-vyas/raku/ch-1.sh b/challenge-178/jaldhar-h-vyas/raku/ch-1.sh new file mode 100755 index 0000000000..101ac3837b --- /dev/null +++ b/challenge-178/jaldhar-h-vyas/raku/ch-1.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +raku -MBase::Any -e 'to-base(@*ARGS[0].Int, 2i).say;' $@
\ No newline at end of file diff --git a/challenge-178/jaldhar-h-vyas/raku/ch-2.raku b/challenge-178/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..d5a9bf5e8c --- /dev/null +++ b/challenge-178/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,56 @@ +#!/usr/bin/raku + +sub format($self) { + sprintf "%04d-%02d-%02d %02d:%02d", .year, .month, .day, .hour, .minute + given $self; +} + +sub nextBusinessDay(DateTime $dt) { + my $next = DateTime.new( + date => $dt.Date, + hour => 9, + minute => 0, + formatter => &format + ); + + if ($dt.day-of-week == 5) { + $next = $next.later(days => 3); + } elsif ($dt.day-of-week == 6) { + $next = $next.later(days => 2); + } else { + $next = $next.later(days => 1); + } + + return $next; +} + +sub MAIN( + Str $timestamp, #= a datetime string in the format YYYY-MM-DD HH:MM + Real $duration #= a duration as a decimal number of hours +) { + my ($year, $month, $day, $hour, $minute); + + if $timestamp.match(/ ^ (\d ** 4) '-' (\d ** 2) '-' (\d ** 2) ' ' (\d ** 2) ':' (\d ** 2) $/) { + ($year, $month, $day, $hour, $minute) = $/.List; + } else { + die "Bad timestamp format"; + } + + my $start = DateTime.new( + year => $year, + month => $month, + day => $day, + hour => $hour, + minute => $minute, + formatter => &format + ); + + my $endOfDay = DateTime.new(date => $start.Date, hour => 18, minute => 0); + my $endOfDuration = $start.clone.later(seconds => 3_600 * $duration); + if $endOfDuration <= $endOfDay { + say $endOfDuration; + } else { + my $difference = $endOfDuration - $endOfDay; + say nextBusinessDay($start).later(seconds => $difference); + } +}
\ No newline at end of file |
