aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-26 11:11:39 +0100
committerGitHub <noreply@github.com>2023-03-26 11:11:39 +0100
commit35fd050c4f46acb50f812b30b8902185e3ed1e2e (patch)
tree5aef239adaa0905aac4f64c35347c39020cd10dd
parenta286ede7a217e0f8344668a4bf7625d9558d84fb (diff)
parentd2953f30129880a59974c77ab4d00df21f4f6f71 (diff)
downloadperlweeklychallenge-club-35fd050c4f46acb50f812b30b8902185e3ed1e2e.tar.gz
perlweeklychallenge-club-35fd050c4f46acb50f812b30b8902185e3ed1e2e.tar.bz2
perlweeklychallenge-club-35fd050c4f46acb50f812b30b8902185e3ed1e2e.zip
Merge pull request #7793 from LubosKolouch/master
Challenge 034 039 LK Perl Python
-rw-r--r--challenge-034/lubos-kolouch/perl/ch-1.pl21
-rw-r--r--challenge-034/lubos-kolouch/perl/ch-2.pl50
-rw-r--r--challenge-034/lubos-kolouch/python/ch-1.py20
-rw-r--r--challenge-034/lubos-kolouch/python/ch-2.py46
-rw-r--r--challenge-039/lubos-kolouch/perl/ch-1.pl62
-rw-r--r--challenge-039/lubos-kolouch/perl/ch-2.pl39
-rw-r--r--challenge-039/lubos-kolouch/python/ch-1.py47
-rw-r--r--challenge-039/lubos-kolouch/python/ch-2.py31
8 files changed, 316 insertions, 0 deletions
diff --git a/challenge-034/lubos-kolouch/perl/ch-1.pl b/challenge-034/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..9d77d24725
--- /dev/null
+++ b/challenge-034/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,21 @@
+# Define an array of numbers
+my @numbers = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
+
+# Print the first three elements of the array using a slice
+print "First three elements: ", join( ", ", @numbers[ 0 .. 2 ] ), "\n";
+
+# Print the last five elements of the array using a slice
+print "Last five elements: ", join( ", ", @numbers[ -5 .. -1 ] ), "\n";
+
+# Define an array of names
+my @names = ( "Alice", "Bob", "Charlie", "Dave", "Emily" );
+
+# Define a hash of ages
+my %ages = (
+ "Alice" => 27,
+ "Charlie" => 32,
+ "Emily" => 24
+);
+
+# Use a hash slice to print the ages of Alice, Charlie, and Emily
+print "Ages: ", join( ", ", @ages{ @names[ 0, 2, 4 ] } ), "\n";
diff --git a/challenge-034/lubos-kolouch/perl/ch-2.pl b/challenge-034/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..a979252617
--- /dev/null
+++ b/challenge-034/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+
+# Define a function to add two numbers
+sub add {
+ my ( $a, $b ) = @_;
+ return $a + $b;
+}
+
+# Define a function to subtract two numbers
+sub subtract {
+ my ( $a, $b ) = @_;
+ return $a - $b;
+}
+
+# Define a function to multiply two numbers
+sub multiply {
+ my ( $a, $b ) = @_;
+ return $a * $b;
+}
+
+# Define a function to divide two numbers
+sub divide {
+ my ( $a, $b ) = @_;
+ return $a / $b;
+}
+
+# Define a dispatch table that maps operation names to functions
+my %operation_table = (
+ "add" => \&add,
+ "subtract" => \&subtract,
+ "multiply" => \&multiply,
+ "divide" => \&divide
+);
+
+# Define a function that takes an operation name and two numbers, and performs the operation
+sub perform_operation {
+ my ( $operation, $a, $b ) = @_;
+ if ( exists $operation_table{$operation} ) {
+ return $operation_table{$operation}->( $a, $b );
+ }
+ else {
+ die "Unknown operation '$operation'";
+ }
+}
+
+# Test the dispatch table by performing some operations
+print perform_operation( "add", 2, 3 ), "\n"; # Output: 5
+print perform_operation( "subtract", 5, 1 ), "\n"; # Output: 4
+print perform_operation( "multiply", 4, 6 ), "\n"; # Output: 24
+print perform_operation( "divide", 10, 2 ), "\n"; # Output: 5
diff --git a/challenge-034/lubos-kolouch/python/ch-1.py b/challenge-034/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..1251ea49a3
--- /dev/null
+++ b/challenge-034/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Define an array of numbers
+numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+
+# Print the first three elements of the array using a slice
+print(f"First three elements: {numbers[:3]}")
+
+# Print the last five elements of the array using a slice
+print(f"Last five elements: {numbers[-5:]}")
+
+# Define a dictionary of ages
+ages = {"Alice": 27, "Bob": 31, "Charlie": 32, "Dave": 29, "Emily": 24}
+
+# Define a list of names
+names = ["Alice", "Charlie", "Emily"]
+
+# Use a hash slice to print the ages of Alice, Charlie, and Emily
+print(f"Ages: {[ages[name] for name in names]}")
diff --git a/challenge-034/lubos-kolouch/python/ch-2.py b/challenge-034/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..62e4bf37b4
--- /dev/null
+++ b/challenge-034/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+# Define a function to add two numbers
+def add(a, b):
+ return a + b
+
+
+# Define a function to subtract two numbers
+def subtract(a, b):
+ return a - b
+
+
+# Define a function to multiply two numbers
+def multiply(a, b):
+ return a * b
+
+
+# Define a function to divide two numbers
+def divide(a, b):
+ return a / b
+
+
+# Define a dispatch table that maps operation names to functions
+operation_table = {
+ "add": add,
+ "subtract": subtract,
+ "multiply": multiply,
+ "divide": divide,
+}
+
+
+# Define a function that takes an operation name and two numbers, and performs the operation
+def perform_operation(operation, a, b):
+ if operation in operation_table:
+ return operation_table[operation](a, b)
+ else:
+ raise ValueError(f"Unknown operation '{operation}'")
+
+
+# Test the dispatch table by performing some operations
+print(perform_operation("add", 2, 3)) # Output: 5
+print(perform_operation("subtract", 5, 1)) # Output: 4
+print(perform_operation("multiply", 4, 6)) # Output: 24
+print(perform_operation("divide", 10, 2)) # Output: 5
diff --git a/challenge-039/lubos-kolouch/perl/ch-1.pl b/challenge-039/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..89aff4857c
--- /dev/null
+++ b/challenge-039/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+sub minutes_light_on {
+ my @guest_book = @_;
+ my @time_ranges;
+
+ for my $entry (@guest_book) {
+ my ( $in_time, $out_time ) = @{$entry}[ 1, 2 ];
+ my $in_time_minutes = int( substr( $in_time, 0, 2 ) ) * 60 +
+ int( substr( $in_time, 3, 2 ) );
+ my $out_time_minutes = int( substr( $out_time, 0, 2 ) ) * 60 +
+ int( substr( $out_time, 3, 2 ) );
+ push @time_ranges, [ $in_time_minutes, $out_time_minutes ];
+ }
+
+ @time_ranges = sort { $a->[0] <=> $b->[0] } @time_ranges;
+
+ my @merged_ranges = ( $time_ranges[0] );
+
+ for my $i ( 1 .. $#time_ranges ) {
+ my ( $current_start, $current_end ) = @{ $time_ranges[$i] };
+ my ( $last_start, $last_end ) = @{ $merged_ranges[-1] };
+
+ if ( $current_start > $last_end ) {
+ push @merged_ranges, [ $current_start, $current_end ];
+ }
+ else {
+ $merged_ranges[-1] =
+ [ $last_start, max( $current_end, $last_end ) ];
+ }
+ }
+
+ my $total_minutes = 0;
+ for my $range (@merged_ranges) {
+ $total_minutes += $range->[1] - $range->[0];
+ }
+
+ return $total_minutes;
+}
+
+sub max {
+ return $_[0] > $_[1] ? $_[0] : $_[1];
+}
+
+my @guest_book = (
+ [ 'Alex', '09:10', '09:45' ],
+ [ 'Arnold', '09:15', '09:33' ],
+ [ 'Bob', '09:22', '09:55' ],
+ [ 'Charlie', '09:25', '10:05' ],
+ [ 'Steve', '09:33', '10:01' ],
+ [ 'Roger', '09:44', '10:12' ],
+ [ 'David', '09:57', '10:23' ],
+ [ 'Neil', '10:01', '10:19' ],
+ [ 'Chris', '10:10', '11:00' ],
+);
+
+is( minutes_light_on(@guest_book), 110, "Test minutes_light_on" );
diff --git a/challenge-039/lubos-kolouch/perl/ch-2.pl b/challenge-039/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..a5597a1d56
--- /dev/null
+++ b/challenge-039/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+sub evaluate_rpn {
+ my ($expression) = @_;
+ my @stack;
+
+ for my $token ( split / /, $expression ) {
+ if ( $token =~ /^[+\-*\/]$/ ) {
+ my $b = pop @stack;
+ my $a = pop @stack;
+
+ if ( $token eq '+' ) {
+ push @stack, $a + $b;
+ }
+ elsif ( $token eq '-' ) {
+ push @stack, $a - $b;
+ }
+ elsif ( $token eq '*' ) {
+ push @stack, $a * $b;
+ }
+ else { # $token eq '/'
+ push @stack, $a / $b;
+ }
+ }
+ else {
+ push @stack, $token;
+ }
+ }
+
+ return $stack[0];
+}
+
+my $rpn_expression = '3 4 + 2 * 1 +';
+my $result = evaluate_rpn($rpn_expression);
+print "RPN expression: $rpn_expression\n";
+print "Result: $result\n";
diff --git a/challenge-039/lubos-kolouch/python/ch-1.py b/challenge-039/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..68f5033a99
--- /dev/null
+++ b/challenge-039/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,47 @@
+from typing import List, Tuple
+
+
+def minutes_light_on(guest_book: List[Tuple[str, str, str]]) -> int:
+ """
+ Calculate the total minutes the light was on based on the guest book entries.
+
+ :param guest_book: A list of tuples containing guest names, IN time, and OUT time.
+ :return: The total minutes the light was on.
+ """
+ time_ranges = []
+ for _, in_time, out_time in guest_book:
+ in_time_minutes = int(in_time.split(":")[0]) * 60 + int(in_time.split(":")[1])
+ out_time_minutes = int(out_time.split(":")[0]) * 60 + int(
+ out_time.split(":")[1]
+ )
+ time_ranges.append((in_time_minutes, out_time_minutes))
+
+ time_ranges.sort()
+
+ merged_ranges = [time_ranges[0]]
+ for current_start, current_end in time_ranges[1:]:
+ last_start, last_end = merged_ranges[-1]
+
+ if current_start > last_end:
+ merged_ranges.append((current_start, current_end))
+ else:
+ merged_ranges[-1] = (last_start, max(current_end, last_end))
+
+ total_minutes = sum(end - start for start, end in merged_ranges)
+
+ return total_minutes
+
+
+def test_minutes_light_on():
+ guest_book = [
+ ("Alex", "09:10", "09:45"),
+ ("Arnold", "09:15", "09:33"),
+ ("Bob", "09:22", "09:55"),
+ ("Charlie", "09:25", "10:05"),
+ ("Steve", "09:33", "10:01"),
+ ("Roger", "09:44", "10:12"),
+ ("David", "09:57", "10:23"),
+ ("Neil", "10:01", "10:19"),
+ ("Chris", "10:10", "11:00"),
+ ]
+ assert minutes_light_on(guest_book) == 110
diff --git a/challenge-039/lubos-kolouch/python/ch-2.py b/challenge-039/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..fef9738bb9
--- /dev/null
+++ b/challenge-039/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def evaluate_rpn(expression: str) -> float:
+ stack = []
+
+ for token in expression.split():
+ if token in "+-*/":
+ b = stack.pop()
+ a = stack.pop()
+
+ if token == "+":
+ stack.append(a + b)
+ elif token == "-":
+ stack.append(a - b)
+ elif token == "*":
+ stack.append(a * b)
+ else: # token == "/"
+ stack.append(a / b)
+ else:
+ stack.append(float(token))
+
+ return stack[0]
+
+
+if __name__ == "__main__":
+ rpn_expression = "3 4 + 2 * 1 +"
+ result = evaluate_rpn(rpn_expression)
+ print(f"RPN expression: {rpn_expression}")
+ print(f"Result: {result}")