Minimum numbers of three lists

Revision as of 22:07, 31 October 2021 by Steenslag (talk | contribs) (→‎{{header|Ruby}}: Add Ruby)


Given three lists:

  • Numbers1 = [5,45,23,21,67]
  • Numbers2 = [43,22,78,46,38]
  • Numbers3 = [9,98,12,98,53]
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


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.



ALGOL 68

Generallising a little... <lang algol68>BEGIN # construct a list of the minimum values of some other lists #

   # lists are represented by arrays in this sample #
   # 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 # ;
   # displays a list of numbers #
   PROC show = ( []INT list )VOID:
       FOR i FROM LWB list TO UPB list DO print( ( " ", whole( list[ i ], 0 ) ) ) OD;
   # 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</lang>

Output:
 5 22 12 21 38

ALGOL W

<lang algolw>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.</lang>

Output:
 5 22 12 21 38

C

<lang 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;

}</lang>

Output:
5 22 12 21 38 

F#

<lang fsharp> // 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)) </lang>

Output:
[5; 22; 12; 21; 38]

Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: math.order sequences prettyprint ;

{ 5 45 23 21 67 } { 43 22 78 46 38 } { 9 98 12 98 53 } [ min min ] 3map .</lang>

Output:
{ 5 22 12 21 38 }

Go

Library: Go-rcu

<lang go>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)

}</lang>

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:<lang jq>def numbers1: [ 5, 45, 23, 21, 67]; def numbers2: [43, 22, 78, 46, 38]; def numbers3: [ 9, 98, 12, 98, 53];</lang> Mirroring the requirements <lang jq>[range(0;5)

| [numbers1[.], numbers2[.], numbers3[.]] | min]</lang>

Functional solution <lang jq>[numbers1, numbers2, numbers3] | transpose | map(min)</lang>

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

Julia

Computed in the REPL, using matrix functions. <lang julia> 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

</lang>

Nim

<lang 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</lang>

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

Perl

<lang 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;

}</lang>

Output:
 5 22 12 21 38

Plain English

<lang plainenglish>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.</lang>

Output:
5, 22, 12, 21, 38

Raku

<lang perl6>say [Zmin] (5,45,23,21,67), (43,22,78,46,38), (9,98,12,98,53);</lang>

Output:
(5 22 12 21 38)

Red

<lang rebol>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</lang>

Output:
5 22 12 21 38

Ring

<lang 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 + "]"</lang>
Output:
working...
The minimum numbers of three lists = [5,22,12,21,38]
done...

Ruby

<lang 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)</lang>

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

Wren

<lang ecmascript>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)</lang>

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