PWC139 - JortSort

TL;DR

Here we are with TASK #1 from The Weekly Challenge #139. Enjoy!

The challenge

You are given a list of numbers.

Write a script to implement JortSort. It should return true/false depending if the given list of numbers are already sorted.

Example 1:

Input: @n = (1,2,3,4,5)
Output: 1

Since the array is sorted, it prints 1.

Example 2:

Input: @n = (1,3,2,4,5)
Output: 0

Since the array is NOT sorted, it prints 0.

The questions

What the heck is JortSort?!?

(And well, thanks for giving us these candies!)

(Also… is it OK to assume that we’re talking numbers here?!?)

The solution

OK, this is about comparing two arrays, one being the original one and the other being its sorted version.

In Raku we can leverage the matching operator to compare the two arrays, I hope I’m not missing the real meaning of it:

#!/usr/bin/env raku
use v6;
sub MAIN (*@args) { say jort-sort(@args) ?? 1 !! 0 }
sub jort-sort (@args) { @args.sort ~~ @args }

In Perl there’s a little more road to go, though not too much:

#!/usr/bin/env perl
use v5.24;
use warnings;
use experimental 'signatures';
no warnings 'experimental::signatures';
say jort_sort(@ARGV) ? 1 : 0;
sub jort_sort (@args) {
   for (sort @args) { return 0 unless $_ == shift @args }
   return 1
}

I guess it’s everything… and I hope I didn’t miss the point!


Comments? Octodon, , GitHub, Reddit, or drop me a line!