The weekly challenge 219 - task 1: sorted squares

 1 #!/usr/bin/perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-219/#TASK1
 3 #
 4 # Task 1: Sorted Squares
 5 # ======================
 6 #
 7 # You are given a list of numbers.
 8 #
 9 # Write a script to square each number in the list and return the sorted list, increasing order.
10 #
11 ## Example 1
12 ##
13 ## Input: @list = (-2, -1, 0, 3, 4)
14 ## Output: (0, 1, 4, 9, 16)
15 #
16 ## Example 2
17 ##
18 ## Input: @list = (5, -4, -1, 3, 6)
19 ## Output: (1, 9, 16, 25, 36)
20 #
21 ############################################################
22 ##
23 ## discussion
24 ##
25 ############################################################
26 #
27 # Just square everything, then sort.
28 
29 use strict;
30 use warnings;
31 
32 sorted_squares(-2, -1, 0, 3, 4);
33 sorted_squares(5, -4, -1, 3, 6);
34 
35 sub sorted_squares {
36    my @list = @_;
37    print "Input: (" . join(", ", @list) . ")\n";
38    print "Output: (" . join(", ", sort {$a<=>$b} map {$_*$_} @list) . ")\n";
39 }