September 2022 Archives

Perl Weekly Challenge 184: Sequence Number and Split Array

These are some answers to the Week 184 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days from now (on Oct. 2, 2022 at 23:59). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Sequence Number

You are given list of strings in the format aa9999 i.e. first 2 characters can be anything ‘a-z’ followed by 4 digits ‘0-9’.

Write a script to replace the first two characters with sequence starting with ‘00’, ‘01’, ‘02’ etc.

Example 1

Input: @list = ( 'ab1234', 'cd5678', 'ef1342')
Output: ('001234', '015678', '021342')

Example 2

Input: @list = ( 'pq1122', 'rs3334')
Output: ('001122', '013334')

Sequence Number in Raku

The program is fairly straight forward. For various reasons, I found that using the subst method was slightly more efficient (code-wise) than using a regex substitution.

for <ab1234 cd5678 ef1342>, <pq1122 rs3334> -> @test {
    my $i = 0;
    my @out;
    for @test {
        push @out, .subst(/^<[a..z]>**2/, $i.fmt("%02d"));
        $i++;
    }
    say "@test[]  =>  @out[]";
}

This program displays the following output:

$ raku ./sequence-number.raku
ab1234 cd5678 ef1342  =>  001234 015678 021342
pq1122 rs3334  =>  001122 013334

Sequence Number in Perl

This is essentially a port to Perl of the Raku program above:

use strict;
use warnings;
use feature qw/say/;

for my $test ([<ab1234 cd5678 ef1342>], [<pq1122 rs3334>]) {
    my $i = 0;
    my @out = @$test;
    for (@out) {
        my $count = sprintf("%02d", $i);
        s/^[a-z]{2}/$count/;
        $i++;
    }
    say "@$test  =>  @out";
}

This program displays the following output:

$ perl ./sequence-number.pl
ab1234 cd5678 ef1342  =>  001234 015678 021342
pq1122 rs3334  =>  001122 013334

Task 2: Split Array

You are given list of strings containing 0-9 and a-z separated by space only.

Write a script to split the data into two arrays, one for integers and one for alphabets only.

Example 1

Input: @list = ( 'a 1 2 b 0', '3 c 4 d')
Output: [[1,2,0], [3,4]] and [['a','b'], ['c','d']]

Example 2

Input: @list = ( '1 2', 'p q r', 's 3', '4 5 t')
Output: [[1,2], [3], [4,5]] and [['p','q','r'], ['s'], ['t']]

Split Array in Raku

Using grep to keep either letters or numerals:

for ('a 1 2 b 0', '3 c 4 d'), ('1 2', 'p q r', 's 3', '4 5 t') -> @test {
    my (@letters, @digits);
    for @test -> $item {
        append @letters, grep {  /<alpha>+/ }, $item.split(/\s+/);
        append @digits, grep { /\d+/ }, $item.split(/\s+/);
    }
    .say for @letters, @digits;
}

This program displays the following output:

$ raku ./split-array.raku
[a b c d]
[1 2 0 3 4]
[p q r s t]
[1 2 3 4 5]

Split Array in Perl

This is a port to Perl of the above Raku program:

use strict;
use warnings;
use feature qw/say/;

for my $test (['a 1 2 b 0', '3 c 4 d'], ['1 2', 'p q r', 's 3', '4 5 t']) {
    my (@letters, @digits);
    for my $item (@$test) {
        push @letters, grep { /[a-zA-Z]+/ } split /\s+/, $item;
        push @digits, grep { /\d+/ } split /\s+/, $item;;
    }
    say  "@letters \n@digits";
}

This program displays the following output:

$ perl  ./split-array.pl
a b c d
1 2 0 3 4
p q r s t
1 2 3 4 5

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on October 9, 2022. And, please, also spread the word about the Perl Weekly Challenge if you can.

Perl Weekly Challenge 182: Unique Array and Date Difference

These are some answers to the Week 183 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Task 1: Unique Array

You are given list of arrayrefs.

Write a script to remove the duplicate arrayrefs from the given list.

Example 1

Input: @list = ([1,2], [3,4], [5,6], [1,2])
Output: ([1,2], [3,4], [5,6])

Example 2

Input: @list = ([9,1], [3,7], [2,5], [2,5])
Output: ([9, 1], [3,7], [2,5])

Unique Array in Raku

The Raku solution is essentially a one-liner (more than one line because of the tests). We convert the sub-arrays into strings and use the unique built-in routine to remove duplicates.

for ([1,2], [3,4], [5,6], [1,2]), 
    ([9,1], [3,7], [2,5], [2,5]) -> @test {
    @test>>.map({"[$^a, $^b]"}).flat.unique.say;
}

This program displays the following output:

$ raku ./unique-arrays.raku
([1, 2] [3, 4] [5, 6])
([9, 1] [3, 7] [2, 5])

Unique Array in Perl

In Perl, we use the %unique hash to remove duplicates.

use strict;
use warnings;
use feature qw/say/;

for my $test ( [[1,2], [3,4], [5,6], [1,2]], 
               [[9,1], [3,7], [2,5], [2,5]] ) {
    my %unique = map { $_ => 1 } map { "[@$_]"} @$test;
    say join ", ",  keys %unique;
}

Note that, since this is not requested in the task specification, we’re not trying to keep the order of the input. It would be easy to keep the input order with an additional array.

This program displays the following output:

$ perl ./unique-arrays.pl
[3 4], [5 6], [1 2]
[3 7], [2 5], [9 1]

Task 2: Date Difference

You are given two dates, $date1 and $date2 in the format YYYY-MM-DD.

Write a script to find the difference between the given dates in terms on years and days only.

Example 1

    Input: $date1 = '2019-02-10'
           $date2 = '2022-11-01'
    Output: 3 years 264 days

Example 2

    Input: $date1 = '2020-09-15'
           $date2 = '2022-03-29'
    Output: 1 year 195 days

Example 3

    Input: $date1 = '2019-12-31'
           $date2 = '2020-01-01'
    Output: 1 day

Example 4

    Input: $date1 = '2019-12-01'
           $date2 = '2019-12-31'
    Output: 30 days

Example 5

    Input: $date1 = '2019-12-31'
           $date2 = '2020-12-31'
    Output: 1 year

Example 6

    Input: $date1 = '2019-12-31'
           $date2 = '2021-12-31'
    Output: 2 years

Example 7

    Input: $date1 = '2020-09-15'
           $date2 = '2021-09-16'
    Output: 1 year 1 day

Example 8

    Input: $date1 = '2019-09-15'
           $date2 = '2021-09-16'
    Output: 2 years 1 day

Date Difference in Raku

for ('2019-02-10', '2022-11-01'), 
    ('2020-09-15', '2022-03-29'),
    ('2019-12-31', '2020-01-01'),
    ('2019-12-01', '2019-12-31'),
    ('2019-12-31', '2020-12-31'),
    ('2019-12-31', '2021-12-31'),
    ('2020-09-15', '2020-09-16'),
    ('2019-09-15', '2021-09-16') -> @test {
    my @dates = map {Date.new($_) }, sort @test;
    my $delta-y = @dates[1].year - @dates[0].year;
    my ($y, $m, $d) = @dates[0].year, @dates[0].month.fmt("%02d"), 
        @dates[0].day.fmt("%02d");
    $delta-y -= 1 if "$m$d" > join "", @dates[1].month.fmt("%02d"), 
        @dates[1].day.fmt("%02d");
    $y += $delta-y;
    my $new-date = Date.new("$y-$m-$d");
    my $delta-d = @dates[1] - $new-date;
    say "@dates[]: $delta-y year(s) {$delta-d.fmt("%3d")} day(s)";
}

This script displays the following output:

$ raku ./date-diff.raku
2019-02-10 2022-11-01: 3 year(s) 264 day(s)
2020-09-15 2022-03-29: 1 year(s) 195 day(s)
2019-12-31 2020-01-01: 0 year(s)   1 day(s)
2019-12-01 2019-12-31: 0 year(s)  30 day(s)
2019-12-31 2020-12-31: 1 year(s)   0 day(s)
2019-12-31 2021-12-31: 2 year(s)   0 day(s)
2020-09-15 2020-09-16: 0 year(s)   1 day(s)
2019-09-15 2021-09-16: 2 year(s)   1 day(s)

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on October 2, 2022. And, please, also spread the word about the Perl Weekly Challenge if you can.

Perl Weekly Challenge 182: Max Index and Common Path

These are some answers to the Week 182 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on Sept. 18, 2022 at 23:59). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Max Index

You are given a list of integers.

Write a script to find the index of the first biggest number in the list.

Example:

Input: @n = (5, 2, 9, 1, 7, 6)
Output: 2 (as 3rd element in the list is the biggest number)

Input: @n = (4, 2, 3, 1, 5, 0)
Output: 4 (as 5th element in the list is the biggest number)

Max Index in Raku

The initial idea here was to to store the input data into a hash and to use the max routine to find the maximum value. The max documentation says:

Coerces the invocant to Iterable and returns the numerically largest element; in the case of Hashes, the Pair with the highest value.

But that did not work properly in my tests: max consistently appeared to return the pair with the highest key, not the highest value. I tried to use the maxpairs routine, which, according to the documentation,

returns a Seq with all of the Pairs with maximum value.

The maxpairs method works as expected. This leads to a very short program:

for (5, 2, 9, 1, 7, 6), (4, 2, 3, 1, 5, 0) -> @test {
    my %nums = @test.kv;
    say "@test[] : ", %nums.maxpairs;
}

This program displays the following output:

$ raku ./max-index.raku
5 2 9 1 7 6 : (2 => 9)
4 2 3 1 5 0 : (4 => 5)

Max Index in Perl

In Perl, we use a standard for loop to traverse the input list and find the index of the largest value:

use strict;
use warnings;
use feature qw/say/;

for my $test ([5, 2, 9, 1, 7, 6], [4, 2, 3, 1, 5, 0]) {
    my @nums = @{$test};
    my ($max_i, $max_n) = (0, 0);
    for my $i (0..$#nums) {
        if ($nums[$i] > $max_n) {
            $max_n = $nums[$i];
            $max_i = $i;
        }
    }
    say "@nums : $max_i => $max_n";
}

This program displays the following output:

$ perl ./max-index.pl
5 2 9 1 7 6 : 2 => 9
4 2 3 1 5 0 : 4 => 5

Max Index in 7 Other Languages

We have now implementations of the max index task in 7 languages as follows:

  • Awk
  • JavaScript
  • Julia
  • Python
  • Ring
  • Ruby
  • Scala

Max Index in Awk

In awk, the data is normally passed as standard input. So we use a Unix pipe to pass the data to the program.

# run for example as:
# echo '5 2 9 1 7 6
# 4 2 3 1 5 0' | awk -f ./max-index.awk

function find_max() {
    max_i = 0
    max_n = $1
    for (i = 2; i < NF; i++) {
        if ($i > max_n) {
            max_i = i
            max_n = $i
        }
    }
    printf("Max index for %s: %d => %d\n", $0, max_i - 1, max_n)
}
{
    find_max()
}

Output:

$ echo '5 2 9 1 7 6
4 2 3 1 5 0' | awk -f ./max-index.awk
Max index for 5 2 9 1 7 6: 2 => 9
Max index for 4 2 3 1 5 0: 4 => 5

Max Index in JavaScript

let tests = [ [5, 2, 9, 1, 7, 6], [4, 2, 3, 1, 5, 0] ]
for  (let j = 0; j < tests.length; j++) {
    let test = tests[j]
    let max_i = 0
    let max_n = test[0]
    for (let i = 1; i <= test.length; i++) {
        if (test[i] > max_n) {
            max_n = test[i]
            max_i = i
        }
    }
    console.log("Max index for " + test + ": " + max_i + " => " + max_n)
}

Output:

Max index for 5,2,9,1,7,6: 2 => 9
Max index for 4,2,3,1,5,0: 4 => 5

Max Index in Julia

Note that Julia indexes start at 1. So we need to subtract 1 from the max_i variable to get a result consistent with other (0-based index) languages.

for test in [[5, 2, 9, 1, 7, 6], [4, 2, 3, 1, 5, 0]]
    max_i = 1
    max_n = test[1]
    for i in 2:length(test)
        if (test[i] > max_n)
            max_n = test[i]
            max_i = i
        end
    end
    println("Max index for $test: $(max_i - 1) => $max_n)")
end

Output:

$ julia ./max-index.jl
Max index for [5, 2, 9, 1, 7, 6]: 2 => 9)
Max index for [4, 2, 3, 1, 5, 0]: 4 => 5)

Max Index in Python

for test in [5, 2, 9, 1, 7, 6], [4, 2, 3, 1, 5, 0]:
  max_i = 0
  max_n = test[0]
  for i in range(1, len(test)):
    if test[i] > max_n:
      max_n = test[i]
      max_i = i
  print("Max index for ", test, ": ", max_i, " => ", max_n)

Output:

$ python3 ./max-index.py
Max index for  [5, 2, 9, 1, 7, 6] :  2  =>  9
Max index for  [4, 2, 3, 1, 5, 0] :  4  =>  5

Max Index in Ring

Note that Ring indexes start at 1 (like Julia indexes). So we need to subtract 1 from the max_i variable to get a result consistent with other (0-based index) languages.

for test in [[5, 2, 9, 1, 7, 6], [4, 2, 3, 1, 5, 0]]
    max_i = 1
    max_n = test[1]
    str = ""
    for i = 1 to len(test)
        str = str + test[i] + " "
        if test[i] > max_n
            max_n = test[i]
            max_i = i
        ok
    next
    see "Max index for " + str + ": " +
        (max_i - 1) + " => " + max_n + nl
next

Output:

$ ring ./max-index.ring
Max index for 5 2 9 1 7 6 : 2 => 9
Max index for 4 2 3 1 5 0 : 4 => 5

Max Index in Ruby

for test in [[5, 2, 9, 1, 7, 6], [4, 2, 3, 1, 5, 0]]
    max_i = 0
    max_n = test[0]
    for i in 1..(test.length - 1)
        if test[i] > max_n
            max_n = test[i]
            max_i = i
        end
    end
    printf("Max index for %s: %d => %d\n", test.to_s, max_i, max_n)
end

Output:

Max index for [5, 2, 9, 1, 7, 6]: 2 => 9
Max index for [4, 2, 3, 1, 5, 0]: 4 => 5

Max Index in Scala

object fraction_tree extends App {

  val tests: List[List[Int]] =
    List(List(5, 2, 9, 1, 7, 6), List(4, 2, 3, 1, 5, 0))
  for (test <- tests) {
    var max_i = 0
    var max_n = test(max_i)
    for (i <- 1 to test.length - 1) {
      if (test(i) > max_n) {
        max_n = test(i)
        max_i = i
      }
    }
    println("Max index for " + test.mkString(" ") + s": $max_i => $max_n")
  }
}

Output:

Max index for 5 2 9 1 7 6: 2 => 9
Max index for 4 2 3 1 5 0: 4 => 5

Task 2: Common Path

Given a list of absolute Linux file paths, determine the deepest path to the directory that contains all of them.

Example:

Input:
    /a/b/c/1/x.pl
    /a/b/c/d/e/2/x.pl
    /a/b/c/d/3/x.pl
    /a/b/c/4/x.pl
    /a/b/c/d/5/x.pl

Ouput:
    /a/b/c

Common Path in Raku

This program converts the input into an array of arrays and then compares each item of the first line with the corresponding item of the other lines. The program stops as soon as a difference is found.

my @input = qw <
    /a/b/c/1/x.pl
    /a/b/c/d/e/2/x.pl
    /a/b/c/d/3/x.pl
    /a/b/c/4/x.pl
    /a/b/c/d/5/x.pl
    >;

my @paths = gather {
    for @input <-> $line {
        $line ~~ s/^'/'//;
        my @subpaths = split /'/'/, $line;
        take @subpaths[0..*-2];
    }
}
my $end = @paths.end;
my $k = 0;
OUTLOOP: for 0..(@paths[0].end) -> $i {
    for 0..$end -> $j {
        if @paths[$j][$i]:!exists or @paths[$j][$i] ne @paths[0][$i] {
            $k = $i - 1;
            last OUTLOOP;
        }
    }
}
say '/', join '/', @paths[0][0..$k];

This program displays the following output:

$ raku ./common-path.raku
/a/b/c

Common Path in Perl

use strict;
use warnings;
use feature qw/say/;

my @input = qw <
    /a/b/c/1/x.pl
    /a/b/c/d/e/2/x.pl
    /a/b/c/d/3/x.pl
    /a/b/c/4/x.pl
    /a/b/c/d/5/x.pl
    >;

my @paths;
for my $line (@input) {
    $line =~ s|^/||;
    my @subpaths = split /\//, $line;
    push @paths, [@subpaths[0..$#subpaths-1]];
}

my @first = @{$paths[0]};
my $end = $#paths;
my $k = 0;
OUTLOOP: for my $i (0..$#first) {
    for my $j (0..$end) {
        if ((not exists $paths[$j][$i]) or $paths[$j][$i] ne $first[$i]) {
            $k = $i - 1;
            last OUTLOOP;
        }
    }
}
say '/', join '/', @first[0..$k];

This program displays the following output:

$ perl ./common-path.pl
/a/b/c

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on September 25, 2022. And, please, also spread the word about the Perl Weekly Challenge if you can.

Perl Weekly Challenge 181: Sentence Order and Hot day

These are some answers to the Week 181 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few of days from now (on Sept. 11, 2022 at 23:59). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Sentence Order

You are given a paragraph.

Write a script to order each sentence alphanumerically and print the whole paragraph.

Example:

Input:
    All he could think about was how it would all end. There was
    still a bit of uncertainty in the equation, but the basics
    were there for anyone to see. No matter how much he tried to
    see the positive, it wasn't anywhere to be seen. The end was
    coming and it wasn't going to be pretty.

Ouput:
    about All all could end he how it think was would. a anyone
    basics bit but equation, for in of see still the the There
    there to uncertainty was were. anywhere be he how it matter
    much No positive, see seen the to to tried wasn't. and be
    coming end going it pretty The to was wasn't.

Splitting the paragraph into sentences and then splitting the sentences into words and reordering them in alphabetic order is order is the easiest part, outputting the result in lines with approximately equal lengths is slightly more difficult.

Sentence Order in Raku

We split the input into sentences, and then split each sentence into words, reorder the words into the alphabetic order of their lower-case equivalent, and store each word in the @words array. Once this is completed, we put each word in turn into a string ($line). Once the string reaches a predefined length, we print out the string and continue with a new empty string.

constant $MAX = 55;

my $paragraph = q:to/END/;
    All he could think about was how it would all end. There was
    still a bit of uncertainty in the equation, but the basics
    were there for anyone to see. No matter how much he tried to
    see the positive, it wasn't anywhere to be seen. The end was
    coming and it wasn't going to be pretty.
    END

my @words;
for $paragraph.split(/'.'/) -> $sentence {
    next if $sentence ~~ /^\s*$/;   # remove any empty line
    @words.append(sort { $_.lc }, $sentence.split(/\s/));
    @words[*-1] ~= ".";
}
my $line = "";
for @words -> $w {
    $line ~= "$w ";
    if $line.chars > $MAX {
        say $line;
        $line = "";
    }
}
say $line;

This program displays the following output:

$ raku ./sentence-order.raku
about All all could end he how it think was would.  a anyone
basics bit but equation, for in of see still the the There
there to uncertainty was were.  anywhere be he how it matter
much No positive, see seen the to to tried wasn't.  and
be coming end going it pretty The to was wasn't.

Sentence Order in Perl

We do essentially the same as in Raku: we split the input paragraph into sentences, and then split each sentence into words, reorder the words into the alphabetic order of their lower-case equivalent, and store each word in the @words array. Once this is completed, we put each word and a space in turn into a string ($line). Once the string reaches a predefined length, we print out the string and continue with a new empty string.

use strict;
use warnings;
use feature qw/say/;
use constant MAX => 55;

my $paragraph = <<'END';
All he could think about was how it would all end. There was
still a bit of uncertainty in the equation, but the basics
were there for anyone to see. No matter how much he tried to
see the positive, it wasn't anywhere to be seen. The end was
coming and it wasn't going to be pretty.
END

my @words;
for my $sentence (split /\./, $paragraph) {
    next if $sentence =~ /^\s*$/;   # remove any empty line
    push @words, (sort { lc $a cmp lc $b } split /\s+/, $sentence);
    $words[-1] .= ".";
}
my $line = "";
for my $w (@words) {
    $line .= "$w ";
    if (length $line > MAX) {
        say $line;
        $line = "";
    }
}
say $line;

This program displays the following output:

$ perl ./sentence-order.pl
about All all could end he how it think was would.  a anyone
basics bit but equation, for in of see still the the There
there to uncertainty was were.  anywhere be he how it matter
much No positive, see seen the to to tried wasn't.  and
be coming end going it pretty The to was wasn't.

Task 2: Hot Day

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)

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

Output:
2022-08-02
2022-08-05
2022-08-06
2022-08-08
2022-08-10

For our tests, we’ll use the temperature.txt file with the data provided above in the task specification.

Hot Day in Raku

We read the input file and store the data into an array of arrays, @daily-temps. We then sort the array in ascending dates and output those where the temperature is more that the previous day temperature.

my @daily-temps;
for './temperature.txt'.IO.lines -> $line {
    push @daily-temps, split /','\s/, $line;
}
my $temp = Inf;
for @daily-temps.sort({$^a leg $^b}) -> @record {
    say @record[0] if @record[1] > $temp;
    $temp = @record[1];
}

This program displays the following output:

$ raku ./hot-day.raku
2022-08-02
2022-08-05
2022-08-06
2022-08-08
2022-08-10

Hot Day in Perl

Again, this a simple port to Perl of the Raku program above. Please refer to the previous section if you need any explanations.

use strict;
use warnings;
use feature qw/say/;

my @daily_temps;
my $file_in = "./temperature.txt";
open my $IN, "<", $file_in or die "Cannot open $file_in $!";
while (my $line = <$IN>) {
    chomp $line;
    push @daily_temps, [ split /,\s/, $line ];
}
my $temp = 1e9;
for my $record (sort {$a->[0] cmp $b->[0]} @daily_temps) {
    say $record->[0] if $record->[1] > $temp;
    $temp = $record->[1];
}

This program displays the following output:

$ perl hot-day.pl
2022-08-02
2022-08-05
2022-08-06
2022-08-08
2022-08-10

Hot Day in Awk

It is possible to sort an array in awk, but, in the Unix (and awk) philosophy, it is better to use the Unix sort function to sort the input before it is passed through a pipe to the awk program, so that we don’t even need to use an array and can process each input line in turn and print the dates that satisfy the temperature condition. This leads to a very short and concise program.

# Run as: sort temperature.txt | awk -f hot-day.awk
BEGIN {
    temp = 1000
    FS = ", "
}
{
    if ($2 > temp) {
        print $1
    }
    temp = $1
}

Output:

$ sort ./temperature.txt | awk -f ./hot-day.awk
2022-08-02
2022-08-05
2022-08-06
2022-08-08
2022-08-10

Hot Day in Julia

fh = open("./temperature.txt", "r")
lines = readlines(fh)
sorted_lines = sort(lines)
temp = 100000
for line in sorted_lines
    (date, temp_str) = split(line, ", ")
    curr_temp = parse(Int64, temp_str)
    if curr_temp > temp
        println(date)
    end
    global temp = curr_temp
end

Output:

$ julia ./hot-day.jl
2022-08-02
2022-08-05
2022-08-06
2022-08-08
2022-08-10

Hot Day in Python

lines = []
file_in = "./temperature.txt"
fh = open(file_in, "r")
for line in fh:
    line.rstrip()
    lines.append(line)
lines.sort();
temp = 1e9
for line in lines:
    fields = line.split(',')
    cur_temp = int(fields[1].strip())
    if cur_temp > temp:
        print(fields[0])
    temp = cur_temp

Output:

$ python3 ./hot-day.py
2022-08-02
2022-08-05
2022-08-06
2022-08-08
2022-08-10

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on September 18, 2022. And, please, also spread the word about the Perl Weekly Challenge if you can.

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.