diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-02-28 18:39:31 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-02-28 18:39:31 +0100 |
| commit | fde49e5cd629f30578b4b6fdc7ddc0dadfc39345 (patch) | |
| tree | 45ab4aa753a8d6488d4c116f387566a48e43956c | |
| parent | 09eef326c170759598ee2d5d35a5aad50be4a11c (diff) | |
| download | perlweeklychallenge-club-fde49e5cd629f30578b4b6fdc7ddc0dadfc39345.tar.gz perlweeklychallenge-club-fde49e5cd629f30578b4b6fdc7ddc0dadfc39345.tar.bz2 perlweeklychallenge-club-fde49e5cd629f30578b4b6fdc7ddc0dadfc39345.zip | |
Challenge 206 LK Perl Python Bash Java PHP
| -rwxr-xr-x | challenge-206/lubos-kolouch/bash/ch-1.sh | 40 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/bash/ch-2.sh | 35 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/java/ch-1.java | 46 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/java/ch-2.java | 29 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/perl/ch-2.pl | 40 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/php/ch-1.php | 51 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/php/ch-2.php | 28 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/python/ch-1.py | 35 | ||||
| -rw-r--r-- | challenge-206/lubos-kolouch/python/ch-2.py | 28 |
10 files changed, 360 insertions, 0 deletions
diff --git a/challenge-206/lubos-kolouch/bash/ch-1.sh b/challenge-206/lubos-kolouch/bash/ch-1.sh new file mode 100755 index 0000000000..4f5ac513c3 --- /dev/null +++ b/challenge-206/lubos-kolouch/bash/ch-1.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Declare an array of time points +time=("$@") + +# Declare an array of minutes +minutes=() + +# Loop over the time points and convert them to minutes +for t in "${time[@]}"; do + # Split the time point by colon + IFS=":" read -r h m <<< "$t" + # Calculate the minutes using base 10 arithmetic + ((m = 10#$h * 60 + 10#$m)) + # Append the minutes to the array + minutes+=("$m") +done + +# Sort the minutes array numerically +sorted=($(printf '%s\n' "${minutes[@]}" | sort -n)) + +# Declare a variable for minimum difference and initialize it with a large value +min_diff=1440 + +# Loop over the sorted array and find the minimum difference between any two adjacent elements +for ((i = 0; i < ${#sorted[@]}; i++)); do + # Get the next index modulo the array length + ((j = (i + 1) % ${#sorted[@]})) + # Calculate the absolute difference between current and next element + ((diff = ${sorted[j]} - ${sorted[i]})) + # Make sure the difference is positive + ((diff = diff < 0 ? -diff : diff)) + # If the difference is larger than half a day, subtract it from a full day + ((diff = diff > 720 ? 1440 - diff : diff)) + # Update the minimum difference if needed + ((min_diff = min_diff < diff ? min_diff : diff)) +done + +# Print the output in minutes format +echo "$min_diff" diff --git a/challenge-206/lubos-kolouch/bash/ch-2.sh b/challenge-206/lubos-kolouch/bash/ch-2.sh new file mode 100644 index 0000000000..db09876458 --- /dev/null +++ b/challenge-206/lubos-kolouch/bash/ch-2.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Function to find the maximum sum of the minimum of each pairs +max_sum_min_pairs() { + # Get the array as an argument + local array=("$@") + # Sort the array in ascending order + local sorted_array=($(printf "%s\n" "${array[@]}" | sort -n)) + # Initialize the sum variable + local sum=0 + # Loop through the array with a step of 2 + for ((i=0; i<${#sorted_array[@]}; i+=2)); do + # Add the minimum of each pair to the sum + sum=$((sum + sorted_array[i])) + done + # Return the sum + echo $sum +} + +# Read the input array from standard input or command line arguments +if [ -z "$1" ]; then + read -a input_array +else + input_array=("$@") +fi + +# Check if the input array has even number of elements +if [ $((${#input_array[@]} % 2)) -ne 0 ]; then + echo "Error: The input array must have even number of elements." +else + # Call the function and print the output + output=$(max_sum_min_pairs "${input_array[@]}") + echo "Output: $output" +fi + diff --git a/challenge-206/lubos-kolouch/java/ch-1.java b/challenge-206/lubos-kolouch/java/ch-1.java new file mode 100644 index 0000000000..18d9a6fa3b --- /dev/null +++ b/challenge-206/lubos-kolouch/java/ch-1.java @@ -0,0 +1,46 @@ +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ShortestTimeTest { + + public static int shortestTimeInMinutes(String[] times) { + int minDiff = 24 * 60; // maximum possible difference in minutes + for (int i = 0; i < times.length - 1; i++) { + for (int j = i + 1; j < times.length; j++) { + String[] parts1 = times[i].split(":"); + String[] parts2 = times[j].split(":"); + int h1 = Integer.parseInt(parts1[0]); + int m1 = Integer.parseInt(parts1[1]); + int h2 = Integer.parseInt(parts2[0]); + int m2 = Integer.parseInt(parts2[1]); + int diff = Math.abs((h1 - h2) * 60 + (m1 - m2)); + if (diff > 12 * 60) { // account for circular time + diff = 24 * 60 - diff; + } + if (diff < minDiff) { + minDiff = diff; + } + } + } + return minDiff; + } + + @Test + public void testExample1() { + String[] times = {"00:00", "23:55", "20:00"}; + assertEquals(5, shortestTimeInMinutes(times)); + } + + @Test + public void testExample2() { + String[] times = {"01:01", "00:50", "00:57"}; + assertEquals(4, shortestTimeInMinutes(times)); + } + + @Test + public void testExample3() { + String[] times = {"10:10", "09:30", "09:00", "09:55"}; + assertEquals(15, shortestTimeInMinutes(times)); + } +} diff --git a/challenge-206/lubos-kolouch/java/ch-2.java b/challenge-206/lubos-kolouch/java/ch-2.java new file mode 100644 index 0000000000..f41385c38b --- /dev/null +++ b/challenge-206/lubos-kolouch/java/ch-2.java @@ -0,0 +1,29 @@ +import java.util.Arrays; + +public class MaxMinPairSum { + public static int maxMinPairSum(int[] array) { + // Sort the array in ascending order + Arrays.sort(array); + + // Use pairwise iteration to get pairs of adjacent elements + // (0,1), (2,3), (4,5), ... + int sum = 0; + for (int i = 0; i < array.length; i += 2) { + sum += Math.min(array[i], array[i + 1]); + } + + return sum; + } + + public static void main(String[] args) { + // Define test cases + int[] test1 = {1, 2, 3, 4}; + int expected1 = 4; + int[] test2 = {0, 2, 1, 3}; + int expected2 = 2; + + // Run tests + assert (maxMinPairSum(test1) == expected1); + assert (maxMinPairSum(test2) == expected2); + } +} diff --git a/challenge-206/lubos-kolouch/perl/ch-1.pl b/challenge-206/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..11bf8013df --- /dev/null +++ b/challenge-206/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +sub shortest_time_in_minutes { + my @times = @_; + my $min_diff = 24*60; # maximum possible difference in minutes + for (my $i=0; $i<@times-1; $i++) { + for (my $j=$i+1; $j<@times; $j++) { + my ($h1, $m1) = split /:/, $times[$i]; + my ($h2, $m2) = split /:/, $times[$j]; + my $diff = abs(($h1-$h2)*60 + ($m1-$m2)); + $diff = 24*60 - $diff if $diff > 12*60; # account for circular time + $min_diff = $diff if $diff < $min_diff; + } + } + return $min_diff; +} + +# Test cases +is(shortest_time_in_minutes("00:00", "23:55", "20:00"), 5, "Example 1"); +is(shortest_time_in_minutes("01:01", "00:50", "00:57"), 4, "Example 2"); +is(shortest_time_in_minutes("10:10", "09:30", "09:00", "09:55"), 15, "Example 3"); + +done_testing(); + diff --git a/challenge-206/lubos-kolouch/perl/ch-2.pl b/challenge-206/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..e05d2a96fd --- /dev/null +++ b/challenge-206/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# Use List::Util module +use List::Util qw(min max sum); + +# Function to find the maximum sum of minimum of pairs in an array +sub max_sum_min_pairs { + # Get the array as an argument + my @array = @_; + + # Sort the array in ascending order using perl's sort function + @array = sort {$a <=> $b} @array; + + # Initialize the sum variable to zero + my $sum = 0; + + # Loop through the sorted array and add the minimum of each pair to the sum + for (my $i = 0; $i < scalar(@array); $i += 2) { + # Use min function from List::Util module + $sum += min($array[$i], $array[$i+1]); + } + + # Return the sum as output + return $sum; +} + +# Define test cases +my @test1 = (1, 2, 3, 4); +my $expected1 = 4; +my @test2 = (0, 2, 1, 3); +my $expected2 = 2; + +# Run tests +use Test::More; +is(max_sum_min_pairs(@test1), $expected1, "Example 1"); +is(max_sum_min_pairs(@test2), $expected2, "Example 2"); +done_testing(); diff --git a/challenge-206/lubos-kolouch/php/ch-1.php b/challenge-206/lubos-kolouch/php/ch-1.php new file mode 100644 index 0000000000..9c23f848e9 --- /dev/null +++ b/challenge-206/lubos-kolouch/php/ch-1.php @@ -0,0 +1,51 @@ +<?php + +// Define function to convert a time in HH:MM format to minutes since midnight +function time_to_minutes($time) { + [$hours, $minutes] = explode(':', $time); + return 60 * $hours + $minutes; +} + +// Define function to find the shortest time in minutes between any two times +function shortest_time($times) { + $min_diff = 1440; // initialize with maximum possible value + $num_times = count($times); + for ($i = 0; $i < $num_times - 1; $i++) { + for ($j = $i + 1; $j < $num_times; $j++) { + $diff = abs(time_to_minutes($times[$j]) - time_to_minutes($times[$i])); + $diff = min($diff, 1440 - $diff); // handle wraparound from midnight to 00:00 + if ($diff < $min_diff) { + $min_diff = $diff; + } + } + } + return $min_diff; +} + +// Define test cases +$test1 = array("00:00", "23:55", "20:00"); +$expected1 = 5; +$test2 = array("01:01", "00:50", "00:57"); +$expected2 = 4; +$test3 = array("10:10", "09:30", "09:00", "09:55"); +$expected3 = 15; + +// Run tests +if (shortest_time($test1) === $expected1) { + echo "Test 1 passed\n"; +} else { + echo "Test 1 failed\n"; +} + +if (shortest_time($test2) === $expected2) { + echo "Test 2 passed\n"; +} else { + echo "Test 2 failed\n"; +} + +if (shortest_time($test3) === $expected3) { + echo "Test 3 passed\n"; +} else { + echo "Test 3 failed\n"; +} + diff --git a/challenge-206/lubos-kolouch/php/ch-2.php b/challenge-206/lubos-kolouch/php/ch-2.php new file mode 100644 index 0000000000..d3cf73968d --- /dev/null +++ b/challenge-206/lubos-kolouch/php/ch-2.php @@ -0,0 +1,28 @@ +<?php +function max_min_pair_sum($array) { + // Sort the array in ascending order + sort($array); + + // Use pairwise iteration to get pairs of adjacent elements + // (0,1), (2,3), (4,5), ... + $pairwise_array = array_chunk($array, 2); + + // Calculate the sum of the minimum of each pair + $sum = 0; + foreach ($pairwise_array as $pair) { + $sum += min($pair); + } + + return $sum; +} + +// Define test cases +$test1 = [1, 2, 3, 4]; +$expected1 = 4; +$test2 = [0, 2, 1, 3]; +$expected2 = 2; + +// Run tests +assert(max_min_pair_sum($test1) == $expected1); +assert(max_min_pair_sum($test2) == $expected2); + diff --git a/challenge-206/lubos-kolouch/python/ch-1.py b/challenge-206/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..ac6bd1129b --- /dev/null +++ b/challenge-206/lubos-kolouch/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + + +def shortest_time_in_minutes(*times): + min_diff = 24 * 60 # maximum possible difference in minutes + for i in range(len(times) - 1): + for j in range(i + 1, len(times)): + h1, m1 = map(int, times[i].split(':')) + h2, m2 = map(int, times[j].split(':')) + diff = abs((h1 - h2) * 60 + (m1 - m2)) + diff = 24 * 60 - diff if diff > 12 * 60 else diff # account for circular time + min_diff = min(diff, min_diff) + return min_diff + + +class TestShortestTimeInMinutes(unittest.TestCase): + + def test_example1(self): + self.assertEqual(shortest_time_in_minutes("00:00", "23:55", "20:00"), + 5) + + def test_example2(self): + self.assertEqual(shortest_time_in_minutes("01:01", "00:50", "00:57"), + 4) + + def test_example3(self): + self.assertEqual( + shortest_time_in_minutes("10:10", "09:30", "09:00", "09:55"), 15) + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-206/lubos-kolouch/python/ch-2.py b/challenge-206/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..c967a3503e --- /dev/null +++ b/challenge-206/lubos-kolouch/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def max_min_pair_sum(array: List[int]) -> int: + # Sort the array in ascending order + sorted_array = sorted(array) + + # Use pairwise iterator to get pairs of adjacent elements + # (0,1), (2,3), (4,5), ... + pairwise_array = [(sorted_array[i], sorted_array[i + 1]) + for i in range(0, len(sorted_array), 2)] + + # Calculate the sum of the minimum of each pair + return sum(min(pair) for pair in pairwise_array) + + +# Define test cases +test1 = [1, 2, 3, 4] +expected1 = 4 +test2 = [0, 2, 1, 3] +expected2 = 2 + +# Run tests +assert max_min_pair_sum(test1) == expected1 +assert max_min_pair_sum(test2) == expected2 |
