diff options
| author | boblied <boblied@gmail.com> | 2022-12-01 14:20:48 -0600 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2022-12-01 14:20:48 -0600 |
| commit | 29b17d1c56e2c40cd2307c598f36395ec25a2ab9 (patch) | |
| tree | d9d94badc5b3f4ed666da52c21301b98322ba40f | |
| parent | 4043cebbac94e6cf8775202ec59c31d6cac6a1cd (diff) | |
| download | perlweeklychallenge-club-29b17d1c56e2c40cd2307c598f36395ec25a2ab9.tar.gz perlweeklychallenge-club-29b17d1c56e2c40cd2307c598f36395ec25a2ab9.tar.bz2 perlweeklychallenge-club-29b17d1c56e2c40cd2307c598f36395ec25a2ab9.zip | |
Solution for challenge 2
| -rw-r--r-- | challenge-181/bob-lied/perl/ch-2.pl | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/challenge-181/bob-lied/perl/ch-2.pl b/challenge-181/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..1137770a95 --- /dev/null +++ b/challenge-181/bob-lied/perl/ch-2.pl @@ -0,0 +1,99 @@ +#!/bin/env perl +# You are given file with daily temperature record in random order. +# Write a script to find out days hotter than previous day. +# Example: +# +# Input File: (temperature.txt) Output: +# +# 2022-08-01, 20 2022-08-02 +# 2022-08-09, 10 2022-08-05 +# 2022-08-03, 19 2022-08-06 +# 2022-08-06, 24 2022-08-08 +# 2022-08-05, 22 2022-08-10 +# 2022-08-10, 28 +# 2022-08-07, 20 +# 2022-08-04, 18 +# 2022-08-08, 21 +# 2022-08-02, 25 + +use v5.36; +use strict; +use warnings; + +use Time::Piece; +use Time::Seconds; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +my %tempData; +while (<>) +{ + chomp; + my ($day, $temp) = split /, /; + $tempData{$day} = $temp; +} + + +sub findTempIncrease($tempData) +{ + my @tempHistory; + my @tempIncrease; + my $dayIndex = 0; + for my $day (sort keys %$tempData ) + { + $tempHistory[$dayIndex] = { date => $day, temp => $tempData->{$day} }; + $dayIndex++; + } + + my ($prevDay, $nextDay) = (0, 1); + while ( $nextDay < @tempHistory ) + { + my $prevTemp = $tempHistory[$prevDay]->{temp}; + my $nextTemp = $tempHistory[$nextDay]->{temp}; + + push @tempIncrease, $tempHistory[$nextDay]->{date} if ( $nextTemp > $prevTemp ); + $prevDay++; + $nextDay++; + } + + return \@tempIncrease; +} + + +sub runTest +{ + my @TestCase = ( + { data => { "2022-08-01" => 20 , + "2022-08-09" => 10 , + "2022-08-03" => 19 , + "2022-08-06" => 24 , + "2022-08-05" => 22 , + "2022-08-10" => 28 , + "2022-08-07" => 20 , + "2022-08-04" => 18 , + "2022-08-08" => 21 , + "2022-08-02" => 25 , + }, + result => [ "2022-08-02", + "2022-08-05", + "2022-08-06", + "2022-08-08", + "2022-08-10" + ] + }, + ); + + use Test::More; + + for my $tc ( @TestCase ) + { + is_deeply( findTempIncrease( $tc->{data} ), $tc->{result} ); + } + + done_testing; +} |
