|
| 1 | +#!/usr/bin/env -S perl -CSDA |
| 2 | + |
| 3 | +=pod |
| 4 | +
|
| 5 | +-------------------------------------------------------------------------------------------------------------- |
| 6 | +COLOPHON: |
| 7 | +This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). |
| 8 | +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 |
| 9 | +
|
| 10 | +-------------------------------------------------------------------------------------------------------------- |
| 11 | +TITLE BLOCK: |
| 12 | +Solutions in Perl for The Weekly Challenge 257-1. |
| 13 | +Written by Robbie Hatley on Wed Feb 21, 2024. |
| 14 | +
|
| 15 | +-------------------------------------------------------------------------------------------------------------- |
| 16 | +PROBLEM DESCRIPTION: |
| 17 | +Task 257-1: Smaller than Current |
| 18 | +Submitted by: Mohammad Sajid Anwar |
| 19 | +You are given a array of integers, @ints. Write a script to find |
| 20 | +out how many integers are smaller than current. |
| 21 | +Ie: foreach ints[i], count ints[j] < ints[i] where i != j. |
| 22 | +
|
| 23 | +Example 1: |
| 24 | +Input: @ints = (5, 2, 1, 6) |
| 25 | +Output: (2, 1, 0, 3) |
| 26 | +For $ints[0] = 5, there are two integers (2,1) smaller than 5. |
| 27 | +For $ints[1] = 2, there is one integer (1) smaller than 2. |
| 28 | +For $ints[2] = 1, there is none integer smaller than 1. |
| 29 | +For $ints[3] = 6, there are three integers (5,2,1) smaller than 6. |
| 30 | +
|
| 31 | +Example 2: |
| 32 | +Input: @ints = (1, 2, 0, 3) |
| 33 | +Output: (1, 2, 0, 3) |
| 34 | +
|
| 35 | +Example 3: |
| 36 | +Input: @ints = (0, 1) |
| 37 | +Output: (0, 1) |
| 38 | +
|
| 39 | +Example 4: |
| 40 | +Input: @ints = (9, 4, 9, 2) |
| 41 | +Output: (2, 1, 2, 0) |
| 42 | +
|
| 43 | +-------------------------------------------------------------------------------------------------------------- |
| 44 | +PROBLEM NOTES: |
| 45 | +Nested foreach will solve this: |
| 46 | +sub smaller ($aref) { |
| 47 | + my @smaller = (); |
| 48 | + foreach my $x (@$aref) { |
| 49 | + my $smaller = 0; |
| 50 | + foreach my $y (@$aref) { |
| 51 | + $y < $x and ++$smaller; |
| 52 | + } |
| 53 | + push @smaller, $smaller; |
| 54 | + } |
| 55 | + return @smaller; |
| 56 | +
|
| 57 | +-------------------------------------------------------------------------------------------------------------- |
| 58 | +IO NOTES: |
| 59 | +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a |
| 60 | +single-quoted array of arrays of integers, in proper Perl syntax, like so: |
| 61 | +./ch-1.pl '([42, -42, 8, -17, -23, 27], [11, 12, 13, 14, 15])' |
| 62 | +
|
| 63 | +Output is to STDOUT and will be each input followed by the corresponding output. |
| 64 | +
|
| 65 | +=cut |
| 66 | + |
| 67 | +# ------------------------------------------------------------------------------------------------------------ |
| 68 | +# PRAGMAS, MODULES, AND SUBROUTINES: |
| 69 | +use v5.38; |
| 70 | +sub smaller ($aref) { |
| 71 | + my @smaller = (); |
| 72 | + foreach my $x (@$aref) { |
| 73 | + my $smaller = 0; |
| 74 | + foreach my $y (@$aref) { |
| 75 | + $y < $x and ++$smaller; |
| 76 | + } |
| 77 | + push @smaller, $smaller; |
| 78 | + } |
| 79 | + return @smaller; |
| 80 | +} |
| 81 | + |
| 82 | +# ------------------------------------------------------------------------------------------------------------ |
| 83 | +# INPUTS: |
| 84 | +my @arrays = @ARGV ? eval($ARGV[0]) : |
| 85 | +( |
| 86 | + # Example 1 Input: |
| 87 | + [5, 2, 1, 6], |
| 88 | + # Expected Output: (2, 1, 0, 3) |
| 89 | + |
| 90 | + # Example 2 Input: |
| 91 | + [1, 2, 0, 3], |
| 92 | + # Expected Output: (1, 2, 0, 3) |
| 93 | + |
| 94 | + # Example 3 Input: |
| 95 | + [0, 1], |
| 96 | + # Expected Output: (0, 1) |
| 97 | + |
| 98 | + # Example 4 Input: |
| 99 | + [9, 4, 9, 2], |
| 100 | + # Expected Output: (2, 1, 2, 0) |
| 101 | +); |
| 102 | + |
| 103 | +# ------------------------------------------------------------------------------------------------------------ |
| 104 | +# MAIN LOOP: |
| 105 | +for my $aref (@arrays) { |
| 106 | + say ''; |
| 107 | + say 'Array of integers: (', join(', ', @$aref ), ')'; |
| 108 | + say 'Smaller than current: (', join(', ', smaller($aref) ), ')'; |
| 109 | +} |
| 110 | +exit; |
0 commit comments