Skip to content

Instantly share code, notes, and snippets.

@kevincolyer
Created December 21, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincolyer/907f8de602db6e92e1a3d19b48e58265 to your computer and use it in GitHub Desktop.
Save kevincolyer/907f8de602db6e92e1a3d19b48e58265 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl6
use v6;
use Test;
=begin pod
Task 39.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
=end pod
# this seems obvious - 9.10-11:00 there was continuous presence in the house...
# but here's a script anyway
my $housemates=q:to/timings/;
Alex IN: 09:10 OUT: 09:45
Arnold IN: 09:15 OUT: 09:33
Bob IN: 09:22 OUT: 09:55
Charlie IN: 09:25 OUT: 10:05
Steve IN: 09:33 OUT: 10:01
Roger IN: 09:44 OUT: 10:12
David IN: 09:57 OUT: 10:23
Neil IN: 10:01 OUT: 10:19
Chris IN: 10:10 OUT: 11:00
timings
# make set of all entry and exit times and populate with the interval minutes
# need SetHash because I want to add members one at a time - otherwise a Set is immutable
my %minutes is SetHash;
for $housemates.lines -> $l {
# parse list to get times
$l ~~ / (\d\d) \: (\d\d) .+ (\d\d) \: (\d\d) /;
my ($ih,$im,$oh,$om) = |$/;
# add the time range to the set
%minutes{$_}++ for ($ih*60+$im)..($oh*60+$om-1);
}
# count the elements
say %minutes.elems ~ " minutes the lights were on";
#!/usr/bin/perl6
use v6;
use Test;
=begin pod
Write a script to demonstrate Reverse Polish notation(RPN).
https://en.wikipedia.org/wiki/Reverse_Polish_notation
example given
The infix expression ((15 ÷ (7 − (1 + 1))) × 3) − (2 + (1 + 1)) can be written like this in reverse Polish notation:
15 7 1 1 + − ÷ 3 × 2 1 1 + + −
gives 5
=end pod
is evaluateRPN("15 7 1 1 + − ÷ 3 × 2 1 1 + + −"),5,"Wikipedia example";
# parses and evaluates a space separated postfix expression in RPN (add,sub,div,mult and pos. integers supported)
sub evaluateRPN($input) {
say "Postfix expression to evaluate: [$input]";
my @stack;
my @in=$input.split: /\s+/;
for @in -> $token {
# for each token in the postfix expression:
given $token {
# if token is an operator:
# operand_2 ← pop from the stack
# operand_1 ← pop from the stack
# result ← evaluate token with operand_1 and operand_2
# push result back onto the stack
when / (<[+−÷×]>) / {
say "! found token operator";
my $op2 = @stack.pop;
say " popped $op2";
my $op1 = @stack.pop;
say " popped $op1";
print " evaluating $op1 and $op2 with $/";
my $result = 0;
$result = $op1 + $op2 if $/ eq '+';
$result = $op1 - $op2 if $/ eq '−';
$result = $op1 / $op2 if $/ eq '÷';
$result = $op1 * $op2 if $/ eq '×';
@stack.push: $result;
say " result = $result";
say " pushed $result into stack [{@stack}]";
}
# else if token is an operand:
# push token onto the stack
when /\d+/ {
say "! found token operand";
@stack.push: $token;
say " pushed $token into stack [{@stack}]";
};
}
}
# result ← pop from the stack
say "! end of input";
say " popping {@stack[0]} and returning...\n";
@stack.pop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment