diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-04-05 20:41:59 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-04-05 20:41:59 +0100 |
| commit | 309009c3472d4b0c89823c082e48d7669cf6cff0 (patch) | |
| tree | e5044df0511fd085192b4245fc6261469e31dc85 | |
| parent | a3ab0fe0ce5111dadb2f81b52c663bb2305b0b10 (diff) | |
| parent | 5e9919b8dc85a54181834303857199ba253ad2fd (diff) | |
| download | perlweeklychallenge-club-309009c3472d4b0c89823c082e48d7669cf6cff0.tar.gz perlweeklychallenge-club-309009c3472d4b0c89823c082e48d7669cf6cff0.tar.bz2 perlweeklychallenge-club-309009c3472d4b0c89823c082e48d7669cf6cff0.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-039/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-039/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-039/paulo-custodio/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-039/paulo-custodio/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-039/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-039/paulo-custodio/t/test-2.yaml | 20 | ||||
| -rwxr-xr-x | challenge-159/e-choroba/perl/ch-1.pl | 40 | ||||
| -rwxr-xr-x | challenge-159/e-choroba/perl/ch-2.pl | 23 |
8 files changed, 160 insertions, 0 deletions
diff --git a/challenge-039/paulo-custodio/Makefile b/challenge-039/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-039/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-039/paulo-custodio/README b/challenge-039/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-039/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-039/paulo-custodio/perl/ch-1.pl b/challenge-039/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..a402e847ef --- /dev/null +++ b/challenge-039/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# Challenge 039 +# +# TASK #1 +# A guest house had a policy that the light remain ON as long as the at least +# one guest is in the house. There is guest book which tracks all guest in/out +# time. Write a script to find out how long in minutes the light were ON. +# Guest Book +# 1) Alex IN: 09:10 OUT: 09:45 +# 2) Arnold IN: 09:15 OUT: 09:33 +# 3) Bob IN: 09:22 OUT: 09:55 +# 4) Charlie IN: 09:25 OUT: 10:05 +# 5) Steve IN: 09:33 OUT: 10:01 +# 6) Roger IN: 09:44 OUT: 10:12 +# 7) David IN: 09:57 OUT: 10:23 +# 8) Neil IN: 10:01 OUT: 10:19 +# 9) Chris IN: 10:10 OUT: 11:00 + +use Modern::Perl; +use Time::Interval; +use Date::Parse; + +my $min_intervals = coalesce([ + ['09:10', '09:45'], + ['09:15', '09:33'], + ['09:22', '09:55'], + ['09:25', '10:05'], + ['09:33', '10:01'], + ['09:44', '10:12'], + ['09:57', '10:23'], + ['10:01', '10:19'], + ['10:10', '11:00'], +]); + +my $minutes = 0; +for (@$min_intervals) { + my $s = str2time($_->[0])/60; + my $e = str2time($_->[1])/60; + $minutes += $e-$s; +} +say $minutes; diff --git a/challenge-039/paulo-custodio/perl/ch-2.pl b/challenge-039/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..86f2877b47 --- /dev/null +++ b/challenge-039/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +# Challenge 039 +# +# TASK #2 +# Contributed by Andrezgz +# Write a script to demonstrate Reverse Polish notation(RPN). Checkout the wiki +# page for more information about RPN. + +use Modern::Perl; + +# simple rpn calculator +my @stack; +my %dispatch = ( + '+' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a+$b; }, + '-' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a-$b; }, + '*' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a*$b; }, + '/' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a/$b; }, + '.' => sub { say pop @stack; }, +); + +for (split //, "@ARGV") { + if (/\s/) {} + elsif (/\d/) { push @stack, $_; } + elsif (exists $dispatch{$_}) { $dispatch{$_}->(); } + else { die "invalid operation: $_"; } +} diff --git a/challenge-039/paulo-custodio/t/test-1.yaml b/challenge-039/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f3c968762c --- /dev/null +++ b/challenge-039/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 110 diff --git a/challenge-039/paulo-custodio/t/test-2.yaml b/challenge-039/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..746bd8389b --- /dev/null +++ b/challenge-039/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 3 + . + input: + output: 4 +- setup: + cleanup: + args: 1 3 - . + input: + output: -2 +- setup: + cleanup: + args: '"3 2 * ."' + input: + output: 6 +- setup: + cleanup: + args: 3 2 / . + input: + output: 1.5 diff --git a/challenge-159/e-choroba/perl/ch-1.pl b/challenge-159/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..24e047cd84 --- /dev/null +++ b/challenge-159/e-choroba/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +use Math::BigRat; + +sub mediant ($p, $q, $depth) { + my $m = Math::BigRat->new($p->numerator + $q->numerator) + / ($p->denominator + $q->denominator); + return $m->denominator <= $depth ? $m : undef +} + +sub farey_sequence ($n) { + my @farey = map 'Math::BigRat'->new($_), '0/1', '1/1'; + for my $depth (2 .. $n) { + for (my $i = 0; $i < $#farey; ++$i) { + if (my $m = mediant($farey[$i], $farey[1 + $i], $depth)) { + splice @farey, ++$i, 0, $m; + } + } + } + @farey[0, -1] = ('0/1', '1/1'); + return \@farey +} + +use Test::More tests => 3; + +is_deeply farey_sequence(5), + [qw[ 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 ]], + 'Example 1'; + +is_deeply farey_sequence(7), + [qw[ 0/1 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 + 1/2 4/7 3/5 2/3 5/7 3/4 4/5 5/6 6/7 1/1 ]], + 'Example 2'; + +is_deeply farey_sequence(4), + [qw[ 0/1 1/4 1/3 1/2 2/3 3/4 1/1 ]], + 'Example 3'; diff --git a/challenge-159/e-choroba/perl/ch-2.pl b/challenge-159/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..500351616d --- /dev/null +++ b/challenge-159/e-choroba/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +use List::Util qw{ uniq }; +use Math::Prime::Util qw{ factor }; + +sub moebius_number ($n) { + my @f = factor($n); + + return 0 if @f != uniq(@f); + return -1 if @f % 2; + return 1 +} + +use Test::More tests => 4; + +is moebius_number(5), -1, 'Example 1'; +is moebius_number(10), 1, 'Example 2'; +is moebius_number(20), 0, 'Example 3'; + +is moebius_number(100), 0, 'Even number of prime factors but square'; |
