Minimum numbers of three lists: Difference between revisions

From Rosetta Code
Content added Content deleted
(added Arturo)
(add RPL)
Line 671: Line 671:
The minimum numbers of three lists = [5,22,12,21,38]
The minimum numbers of three lists = [5,22,12,21,38]
done...
done...
</pre>

=={{header|RPL}}==
≪ → l1 l2 l3
≪ {} 1 l1 SIZE l2 SIZE l3 SIZE MIN MIN '''FOR''' j
l1 j GET l2 j GET l3 j GET MIN MIN + '''NEXT'''
≫ ≫ ‘'''L3MIN'''’ STO

{5 45 23 21 67} {43 22 78 46 38} {9 98 12 98 53} '''L3MIN'''
{{out}}
<pre>
1: { 5 22 12 21 38 }
</pre>
</pre>



Revision as of 17:44, 29 April 2023

Minimum numbers of three lists is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


Given three lists:

  • Numbers1 = [5,45,23,21,67]
  • Numbers2 = [43,22,78,46,38]
  • Numbers3 = [9,98,12,98,53]


then:

  1. Select the minimum of Numbers[n], Numbers2[n] and Numbers3[n], where n <= 5 (one based).
  2. Add minimum to a new list (Numbers).
  3. Show Numbers on this page.



11l

Translation of: Python
V numbers1 = [5, 45, 23, 21, 67]
V numbers2 = [43, 22, 78, 46, 38]
V numbers3 = [9, 98, 12, 98, 53]

V numbers = (0 .< numbers1.len).map(i -> min(:numbers1[i], :numbers2[i], :numbers3[i]))

print(numbers)
Output:
[5, 22, 12, 21, 38]

Ada

with Ada.Text_Io; use Ada.Text_Io;

procedure Minimum_Three_Lists is

   type Number_Array is array (Positive range 1 .. 5) of Integer;

   Numbers_1 : constant Number_Array := (5,45,23,21,67);
   Numbers_2 : constant Number_Array := (43,22,78,46,38);
   Numbers_3 : constant Number_Array := (9,98,12,98,53);

   Result : Number_Array;
begin
   for A in Number_Array'Range loop
      declare
         R   : Integer renames Result    (A);
         N_1 : Integer renames Numbers_1 (A);
         N_2 : Integer renames Numbers_2 (A);
         N_3 : Integer renames Numbers_3 (A);
      begin
         R := Integer'Min (N_1, Integer'Min (N_2, N_3));
      end;
   end loop;

   for R of Result loop
      Put (R'Image);
   end loop;
   New_Line;
end Minimum_Three_Lists;
Output:
 5 22 12 21 38

ALGOL 68

Generallising a little...

Library: ALGOL 68-rows
BEGIN # construct a list of the minimum values of some other lists #
    # lists are represented by arrays in this sample #
    PR read "rows.incl.a68" PR # row-related utilities #
    # returns a list composed of the minimum values of the lists in lists #
    #         the lists must all have te same bounds #
    PROC min = ( []REF[]INT lists )[]INT:
         IF   LWB lists > UPB lists
         THEN # no lists #
              []INT()
         ELIF INT l length = ( UPB lists[ LWB lists ] + 1 ) - LWB lists[ LWB lists ];
              l length < 1
         THEN # the lists are empty #
              []INT()
         ELSE # have some elements in the lists #
              [ 1 : l length ]INT result;
              INT r pos  := 0;
              FOR i FROM LWB lists[ LWB lists ] TO UPB lists[ LWB lists ] DO
                  INT l min := lists[ LWB lists ][ i ];
                  FOR j FROM LWB lists + 1 TO UPB lists DO
                      IF l min > lists[ j ][ i ] THEN l min := lists[ j ][ i ] FI
                  OD;
                  result[ r pos +:= 1 ] := l min
              OD;
              result
         FI # min # ;

    # construct the lists of numbers required by the task #
    REF[]INT numbers1 = HEAP[ 1 : 5 ]INT := (  5, 45, 23, 21, 67 );
    REF[]INT numbers2 = HEAP[ 1 : 5 ]INT := ( 43, 22, 78, 46, 38 );
    REF[]INT numbers3 = HEAP[ 1 : 5 ]INT := (  9, 98, 12, 98, 53 );
    # display the minimum values for each element in the lists #
    SHOW min( ( numbers1, numbers2, numbers3 ) )
END
Output:
 5 22 12 21 38

ALGOL W

begin % show the minimum elements of three lists %
    integer array numbers1, numbers2, numbers3 ( 1 :: 5 );
    integer pos;
    pos := 1; for v :=  5, 45, 23, 21, 67 do begin numbers1( pos ) := v; pos := pos + 1 end;
    pos := 1; for v := 43, 22, 78, 46, 38 do begin numbers2( pos ) := v; pos := pos + 1 end;
    pos := 1; for v :=  9, 98, 12, 98, 53 do begin numbers3( pos ) := v; pos := pos + 1 end;
    for i := 1 until 5 do begin
        integer m;
        m := numbers1( i );
        if numbers2( i ) < m then m := numbers2( i );
        if numbers3( i ) < m then m := numbers3( i );
        writeon( i_w := 1, s_w := 0, " ", m )
    end for_i;
end.
Output:
 5 22 12 21 38

Arturo

lists: [
    [ 5 45 23 21 67]
    [43 22 78 46 38]
    [ 9 98 12 98 53]
]

print map 0..dec size first lists 'i ->
    min map lists 'l -> l\[i]
Output:
5 22 12 21 38

AutoHotkey

Numbers1 := [5,45,23,21,67]
Numbers2 := [43,22,78,46,38]
Numbers3 := [9,98,12,98,53]
Numbers := []
for i, v in Numbers1
{
    tempArr := []
    loop 3
        tempArr.Push(Numbers%A_Index%[i])
    Numbers.Push(Min(tempArr*))
}

for i, v in Numbers
    result .= v ", "
MsgBox % result := "[" . Trim(result, ", ") . "]"
Output:
[5, 22, 12, 21, 38]

AWK

# syntax: GAWK -f MINIMUM_NUMBERS_OF_THREE_LISTS.AWK
BEGIN {
    n1 = split("5,45,23,21,67",numbers1,",")
    n2 = split("43,22,78,46,38",numbers2,",")
    n3 = split("9,98,12,98,53",numbers3,",")
    if (n1 != n2 || n1 != n3) {
      print("error: arrays must be same length")
      exit(1)
    }
    for (i=1; i<=n1; i++) {
      numbers[i] = min(min(numbers1[i],numbers2[i]),numbers3[i])
      printf("%d ",numbers[i])
    }
    printf("\n")
    exit(0)
}
function min(x,y) { return((x < y) ? x : y) }
Output:
5 22 12 21 38

C

#include <stdio.h>

int min(int a, int b) {
    if (a < b) return a;
    return b;
}

int main() {
    int n;
    int numbers1[5] = {5, 45, 23, 21, 67};
    int numbers2[5] = {43, 22, 78, 46, 38};
    int numbers3[5] = {9, 98, 12, 98, 53};
    int numbers[5]  = {};
    for (n = 0; n < 5; ++n) {
        numbers[n] = min(min(numbers1[n], numbers2[n]), numbers3[n]);
        printf("%d ", numbers[n]);
    }
    printf("\n");
    return 0;
}
Output:
5 22 12 21 38 

F#

// Minimum numbers of three lists. Nigel Galloway: October 26th., 2021
let N1,N2,N3=[5;45;23;21;67],[43;22;78;46;38],[9;98;12;98;53]
printfn "%A" (List.zip3 N1 N2 N3|>List.map(fun(n,g,l)->min (min n g) l))
Output:
[5; 22; 12; 21; 38]

Factor

Works with: Factor version 0.99 2021-06-02
USING: math.order sequences prettyprint ;

{ 5 45 23 21 67 } { 43 22 78 46 38 } { 9 98 12 98 53 }
[ min min ] 3map .
Output:
{ 5 22 12 21 38 }

Fermat

[numbers1] := [(5,45,23,21,67)];
[numbers2] := [(43,22,78,46,38)];
[numbers3] := [(9,98,12,98,53)];

Func Minby( a, b, c, n ) =
    if a[n]<b[n] and a[n]<b[n] then Return(a[n]) fi;
    if b[n]<c[n] then Return(b[n]) fi;
    Return(c[n]).;
    
for i = 1 to 5 do !!Minby( [numbers1], [numbers2], [numbers3], i ) od;
Output:

5 22 23 21 38

FreeBASIC

#define min(a, b) Iif(a<b,a,b)

dim as integer numbers(1 to 3, 1 to 5) = _
     { {5,45,23,21,67}, {43,22,78,46,38}, {9,98,12,98,53} }

for i as uinteger = 1 to 5
    print min( numbers(1, i), min(numbers(2,i), numbers(3, i) ) )
next i
Output:

5
22
12
21
38

Go

Library: Go-rcu
package main

import (
    "fmt"
    "rcu"
)

func main() {
    numbers1 := [5]int{5, 45, 23, 21, 67}
    numbers2 := [5]int{43, 22, 78, 46, 38}
    numbers3 := [5]int{9, 98, 12, 98, 53}
    numbers := [5]int{}
    for n := 0; n < 5; n++ {
        numbers[n] = rcu.Min(rcu.Min(numbers1[n], numbers2[n]), numbers3[n])
    }
    fmt.Println(numbers)
}
Output:
[5 22 12 21 38]

Haskell

import Data.List (transpose)

numbers1, numbers2, numbers3 :: [Integer]
numbers1 = [5, 45, 23, 21, 67]
numbers2 = [43, 22, 78, 46, 38]
numbers3 = [9, 98, 12, 98, 53]

main :: IO ()
main =
  print $
    minimum
      <$> transpose
        [numbers1, numbers2, numbers3]
Output:
[5,22,12,21,38]

JavaScript

(() => {
    "use strict";

    const main = () => {
        const
            numbers1 = [5, 45, 23, 21, 67],
            numbers2 = [43, 22, 78, 46, 38],
            numbers3 = [9, 98, 12, 98, 53];

        return transpose([
            numbers1, numbers2, numbers3
        ]).map(minimum);
    };


    // --------------------- GENERIC ---------------------

    // min :: Ord a => (a, a) -> a
    const min = (a, b) =>
        // The lesser of a and b.
        b < a ? b : a;


    // minimum :: Ord a => [a] -> a
    const minimum = xs =>
        // The least value of xs.
        0 < xs.length ? (
            xs.slice(1).reduce(min, xs[0])
        ) : null;


    // transpose :: [[a]] -> [[a]]
    const transpose = rows =>
        // The columns of the input transposed
        // into new rows.
        // Simpler version of transpose, assuming input
        // rows of even length.
        0 < rows.length ? rows[0].map(
            (_, i) => rows.flatMap(
                v => v[i]
            )
        ) : [];

    // MAIN ---
    return JSON.stringify(main());
})();
Output:
[5,22,12,21,38]

jq

Works with: jq

Works with gojq, the Go implementation of jq

Two solutions are presented - an iterative one that mirrors the problem description, and one that is functional:

def numbers1: [ 5, 45, 23, 21, 67];
def numbers2: [43, 22, 78, 46, 38];
def numbers3: [ 9, 98, 12, 98, 53];

Mirroring the requirements

[range(0;5)
 | [numbers1[.], numbers2[.], numbers3[.]] | min]

Functional solution

[numbers1, numbers2, numbers3]
| transpose
| map(min)
Output:
[5,22,12,21,38]

Julia

Computed in the REPL, using matrix functions.

julia> Numbers1 = [5,45,23,21,67]
5-element Vector{Int64}:
  5
 45
 23
 21
 67

julia> Numbers2 = [43,22,78,46,38]
5-element Vector{Int64}:
 43
 22
 78
 46
 38

julia> Numbers3 = [9,98,12,98,53]
5-element Vector{Int64}:
  9
 98
 12
 98
 53

julia> mat = hcat(Numbers1, Numbers2, Numbers3)
5×3 Matrix{Int64}:
  5  43   9
 45  22  98
 23  78  12
 21  46  98
 67  38  53

julia> minimum(mat, dims=2)
5×1 Matrix{Int64}:
  5
 22
 12
 21
 38

Mathematica / Wolfram Language

Min /@ Transpose@{{5, 45, 23, 21, 67}, {43, 22, 78, 46, 38}, {9, 98, 
    12, 98, 53}}
Output:

{5,22,12,21,38}

Nim

const
  Numbers1 = [ 5, 45, 23, 21, 67]
  Numbers2 = [43, 22, 78, 46, 38]
  Numbers3 = [ 9, 98, 12, 98, 53]

var numbers: array[0..Numbers1.high, int]

for i in 0..numbers.high:
  numbers[i] = min(min(Numbers1[i], Numbers2[i]), Numbers3[i])

echo numbers
Output:
[5, 22, 12, 21, 38]

ooRexx

/* REXX */
l.1=.array~of( 5, 45, 23, 21, 67)
l.2=.array~of(43, 22, 78, 46, 38)
l.3=.array~of( 9, 98, 12, 98, 53)
o=''
Do i=1 To 5
  o=o min(l.1[i],l.2[i],l.3[i])
  End
Say strip(o)
Output:
[5 22 12 21 38]


Perl

use strict;
use warnings;
use List::Util 'min';

my @lists = ([5,45,23,21,67], [43,22,78,46,38], [9,98,12,98,53]);

for my $i (0 .. $#{ $lists[0] }) {
    print ' ' . min map { $lists[$_][$i] } 0..$#lists;
}
Output:
 5 22 12 21 38

Phix

with javascript_semantics
constant N123 = {{ 5, 45, 23, 21, 67},
                 {43, 22, 78, 46, 38},
                 { 9, 98, 12, 98, 53}}
printf(1,"%V\n",{apply(apply(true,vslice,{{N123},tagset(5)}),minsq)})
Output:
{5,22,12,21,38}

Plain English

To run:
Start up.
Create a first list and a second list and a third list.
Find a minimum list (element-wise) of the first list and the second list and the third list.
Destroy the first list. Destroy the second list. Destroy the third list.
Write the minimum list on the console.
Destroy the minimum list.
Wait for the escape key.
Shut down.

An entry is a thing with a number.

A list is some entries.

To add a number to a list:
Allocate memory for an entry.
Put the number into the entry's number.
Append the entry to the list.

To create a first list and a second list and a third list:
Add 5 to the first list.
Add 45 to the first list.
Add 23 to the first list.
Add 21 to the first list.
Add 67 to the first list.
Add 43 to the second list.
Add 22 to the second list.
Add 78 to the second list.
Add 46 to the second list.
Add 38 to the second list.
Add 9 to the third list.
Add 98 to the third list.
Add 12 to the third list.
Add 98 to the third list.
Add 53 to the third list.

To find a minimum number of a number and another number:
If the number is less than the other number, put the number into the minimum; exit.
Put the other number into the minimum.

To find a minimum list (element-wise) of a list and another list and a third list:
Get an entry from the list.
Get another entry from the other list.
Get a third entry from the third list.
Loop.
If the entry is nil, exit.
Find a minimum number of the entry's number and the other entry's number.
Find another minimum number of the third entry's number and the minimum number.
Add the other minimum number to the minimum list.
Put the entry's next into the entry.
Put the other entry's next into the other entry.
Put the third entry's next into the third entry.
Repeat.

To write a list on a console;
To write a list to a console:
Get an entry from the list.
Loop.
If the entry is nil, write "" on the console; exit.
Convert the entry's number to a string.
Write the string on the console without advancing.
If the entry's next is not nil, write ", " on the console without advancing.
Put the entry's next into the entry.
Repeat.
Output:
5, 22, 12, 21, 38

Python

numbers1 = [5,45,23,21,67]
numbers2 = [43,22,78,46,38]
numbers3 = [9,98,12,98,53]

numbers = [min(numbers1[i],numbers2[i],numbers3[i]) for i in range(0,len(numbers1))]

print(numbers)
Output:
[5, 22, 12, 21, 38]


Or, in terms of zip:

'''Minimum value in each column'''

numbers1 = [5, 45, 23, 21, 67]
numbers2 = [43, 22, 78, 46, 38]
numbers3 = [9, 98, 12, 98, 53]

print([
    min(x) for x
    in zip(*[numbers1, numbers2, numbers3])
])
Output:
[5, 22, 12, 21, 38]

Quackery

transpose is defined at Matrix transposition#Quackery.

  [ ' [  5 45 23 21 67 ] ] is Numbers1
  [ ' [ 43 22 78 46 38 ] ] is Numbers2
  [ ' [  9 98 12 98 53 ] ] is Numbers3

  []
  Numbers1 nested
  Numbers2 nested join
  Numbers3 nested join
  transpose
  witheach
     [ behead swap
       witheach min
       join ]
  echo
Output:
[ 5 22 12 21 38 ]

Raku

say [Zmin] (5,45,23,21,67), (43,22,78,46,38), (9,98,12,98,53);
Output:
(5 22 12 21 38)

Red

Red [
    Red-version: 0.6.4
    Description: "Find the element-wise minimum of three lists"
]

numbers1: [5 45 23 21 67]
numbers2: [43 22 78 46 38]
numbers3: [9 98 12 98 53]
length: length? numbers1
result: append/dup [] 0 length
repeat i length [
    result/:i: min min numbers1/:i numbers2/:i numbers3/:i
]
print result
Output:
5 22 12 21 38

REXX

/* REXX */
w= 5 45 23 21 67 43 22 78 46 38 9 98 12 98 53
Do i=1 To 3
  Do j=1 To 5
    Parse Var w l.i.j w
    End
  End
o=''
Do j=1 To 5
  o=o min(l.1.j,l.2.j,l.3.j)
  End
Say strip(o)
Output:
5 22 12 21 38

Ring

see "? "working..."

Num1 = [ 5,45,23,21,67]
Num2 = [43,22,78,46,38]
Num3 = [ 9,98,12,98,53]
n = len(Num1)
Nums = list(n)
 
for i = 1 to n
    Nums[i] = string(min([Num1[i], Num2[i], Num3[i]]))
next

? "The minimum numbers of three lists = " + fmtArray(Nums)
put "done..."

func fmtArray(ar)
    rv = ar[1]
    for n = 2 to len(ar) rv += "," + ar[n] next
    return "[" + rv + "]"
Output:
working...
The minimum numbers of three lists = [5,22,12,21,38]
done...

RPL

≪ → l1 l2 l3 
  ≪ {} 1 l1 SIZE l2 SIZE l3 SIZE MIN MIN FOR j
        l1 j GET l2 j GET l3 j GET MIN MIN + NEXT
≫ ≫ ‘L3MIN’ STO
{5 45 23 21 67} {43 22 78 46 38} {9 98 12 98 53} L3MIN
Output:
1: { 5 22 12 21 38 }

Ruby

numbers1 = [ 5, 45, 23, 21, 67]
numbers2 = [43, 22, 78, 46, 38]
numbers3 = [ 9, 98, 12, 98, 53]
    
p [numbers1, numbers2, numbers3].transpose.map(&:min)
Output:
[5, 22, 12, 21, 38]

Sidef

var lists = [
    [ 5, 45, 23, 21, 67],
    [43, 22, 78, 46, 38],
    [ 9, 98, 12, 98, 53],
]

say lists.zip.map{.min}
Output:
[5, 22, 12, 21, 38]

V (Vlang)

import math
fn main() {
    numbers1 := [5, 45, 23, 21, 67]!
    numbers2 := [43, 22, 78, 46, 38]!
    numbers3 := [9, 98, 12, 98, 53]!
    mut numbers := [5]int{}
    for n in 0..5 {
        numbers[n] = math.min<int>(math.min<int>(numbers1[n], numbers2[n]), numbers3[n])
    }
    println(numbers)
}
Output:
[5, 22, 12, 21, 38]

Wren

var numbers1 = [ 5, 45, 23, 21, 67]
var numbers2 = [43, 22, 78, 46, 38]
var numbers3 = [ 9, 98, 12, 98, 53]
var numbers  = List.filled(5, 0)
for (n in 0..4) numbers[n] = numbers1[n].min(numbers2[n]).min(numbers3[n])
System.print(numbers)
Output:
[5, 22, 12, 21, 38]

XPL0

func Min(A, B);
int  A, B;
return if A<B then A else B;

int N1, N2, N3, I;
[N1:= [5,45,23,21,67];
 N2:= [43,22,78,46,38];
 N3:= [9,98,12,98,53];
 for I:= 0 to 4 do
    [IntOut(0, Min(Min(N1(I), N2(I)), Min(N2(I), N3(I))));
    ChOut(0, ^ );
    ];
]
Output:
5 22 12 21 38