I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Minimum numbers of three lists

From Rosetta Code
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.



ALGOL 68[edit]

Generallising a little...

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
Output:
 5 22 12 21 38

ALGOL W[edit]

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

AWK[edit]

 
# 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[edit]

#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#[edit]

 
// 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[edit]

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[edit]

[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[edit]

#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[edit]

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[edit]

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[edit]

(() => {
"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[edit]

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[edit]

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
 

Nim[edit]

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]

Perl[edit]

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[edit]

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[edit]

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[edit]

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]

Raku[edit]

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

Red[edit]

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

Ring[edit]

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...

Ruby[edit]

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]

Wren[edit]

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]