Minimum numbers of three lists: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 17: Line 17:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V numbers1 = [5, 45, 23, 21, 67]
<syntaxhighlight lang="11l">V numbers1 = [5, 45, 23, 21, 67]
V numbers2 = [43, 22, 78, 46, 38]
V numbers2 = [43, 22, 78, 46, 38]
V numbers3 = [9, 98, 12, 98, 53]
V numbers3 = [9, 98, 12, 98, 53]
Line 23: Line 23:
V numbers = (0 .< numbers1.len).map(i -> min(:numbers1[i], :numbers2[i], :numbers3[i]))
V numbers = (0 .< numbers1.len).map(i -> min(:numbers1[i], :numbers2[i], :numbers3[i]))


print(numbers)</lang>
print(numbers)</syntaxhighlight>


{{out}}
{{out}}
Line 31: Line 31:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io; use Ada.Text_Io;
<syntaxhighlight lang="ada">with Ada.Text_Io; use Ada.Text_Io;


procedure Minimum_Three_Lists is
procedure Minimum_Three_Lists is
Line 58: Line 58:
end loop;
end loop;
New_Line;
New_Line;
end Minimum_Three_Lists;</lang>
end Minimum_Three_Lists;</syntaxhighlight>
{{out}}
{{out}}
<pre> 5 22 12 21 38</pre>
<pre> 5 22 12 21 38</pre>
Line 65: Line 65:
Generallising a little...
Generallising a little...
{{libheader|ALGOL 68-rows}}
{{libheader|ALGOL 68-rows}}
<lang algol68>BEGIN # construct a list of the minimum values of some other lists #
<syntaxhighlight lang="algol68">BEGIN # construct a list of the minimum values of some other lists #
# lists are represented by arrays in this sample #
# lists are represented by arrays in this sample #
PR read "rows.incl.a68" PR # row-related utilities #
PR read "rows.incl.a68" PR # row-related utilities #
Line 97: Line 97:
# display the minimum values for each element in the lists #
# display the minimum values for each element in the lists #
SHOW min( ( numbers1, numbers2, numbers3 ) )
SHOW min( ( numbers1, numbers2, numbers3 ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 104: Line 104:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin % show the minimum elements of three lists %
<syntaxhighlight lang="algolw">begin % show the minimum elements of three lists %
integer array numbers1, numbers2, numbers3 ( 1 :: 5 );
integer array numbers1, numbers2, numbers3 ( 1 :: 5 );
integer pos;
integer pos;
Line 117: Line 117:
writeon( i_w := 1, s_w := 0, " ", m )
writeon( i_w := 1, s_w := 0, " ", m )
end for_i;
end for_i;
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 124: Line 124:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Numbers1 := [5,45,23,21,67]
<syntaxhighlight lang="autohotkey">Numbers1 := [5,45,23,21,67]
Numbers2 := [43,22,78,46,38]
Numbers2 := [43,22,78,46,38]
Numbers3 := [9,98,12,98,53]
Numbers3 := [9,98,12,98,53]
Line 138: Line 138:
for i, v in Numbers
for i, v in Numbers
result .= v ", "
result .= v ", "
MsgBox % result := "[" . Trim(result, ", ") . "]"</lang>
MsgBox % result := "[" . Trim(result, ", ") . "]"</syntaxhighlight>
{{out}}
{{out}}
<pre>[5, 22, 12, 21, 38]</pre>
<pre>[5, 22, 12, 21, 38]</pre>


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MINIMUM_NUMBERS_OF_THREE_LISTS.AWK
# syntax: GAWK -f MINIMUM_NUMBERS_OF_THREE_LISTS.AWK
BEGIN {
BEGIN {
Line 161: Line 161:
}
}
function min(x,y) { return((x < y) ? x : y) }
function min(x,y) { return((x < y) ? x : y) }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 168: Line 168:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int min(int a, int b) {
int min(int a, int b) {
Line 187: Line 187:
printf("\n");
printf("\n");
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 195: Line 195:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Minimum numbers of three lists. Nigel Galloway: October 26th., 2021
// 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]
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))
printfn "%A" (List.zip3 N1 N2 N3|>List.map(fun(n,g,l)->min (min n g) l))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 207: Line 207:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: math.order sequences prettyprint ;
<syntaxhighlight lang="factor">USING: math.order sequences prettyprint ;


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


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>[numbers1] := [(5,45,23,21,67)];
<syntaxhighlight lang="fermat">[numbers1] := [(5,45,23,21,67)];
[numbers2] := [(43,22,78,46,38)];
[numbers2] := [(43,22,78,46,38)];
[numbers3] := [(9,98,12,98,53)];
[numbers3] := [(9,98,12,98,53)];
Line 226: Line 226:
Return(c[n]).;
Return(c[n]).;
for i = 1 to 5 do !!Minby( [numbers1], [numbers2], [numbers3], i ) od;</lang>
for i = 1 to 5 do !!Minby( [numbers1], [numbers2], [numbers3], i ) od;</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 237: Line 237:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>#define min(a, b) Iif(a<b,a,b)
<syntaxhighlight lang="freebasic">#define min(a, b) Iif(a<b,a,b)


dim as integer numbers(1 to 3, 1 to 5) = _
dim as integer numbers(1 to 3, 1 to 5) = _
Line 244: Line 244:
for i as uinteger = 1 to 5
for i as uinteger = 1 to 5
print min( numbers(1, i), min(numbers(2,i), numbers(3, i) ) )
print min( numbers(1, i), min(numbers(2,i), numbers(3, i) ) )
next i</lang>
next i</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 256: Line 256:
=={{header|Go}}==
=={{header|Go}}==
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 272: Line 272:
}
}
fmt.Println(numbers)
fmt.Println(numbers)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 280: Line 280:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (transpose)
<syntaxhighlight lang="haskell">import Data.List (transpose)


numbers1, numbers2, numbers3 :: [Integer]
numbers1, numbers2, numbers3 :: [Integer]
Line 292: Line 292:
minimum
minimum
<$> transpose
<$> transpose
[numbers1, numbers2, numbers3]</lang>
[numbers1, numbers2, numbers3]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>[5,22,12,21,38]</pre>
<pre>[5,22,12,21,38]</pre>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 342: Line 342:
// MAIN ---
// MAIN ---
return JSON.stringify(main());
return JSON.stringify(main());
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>[5,22,12,21,38]</pre>
<pre>[5,22,12,21,38]</pre>
Line 349: Line 349:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of 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];
Two solutions are presented - an iterative one that mirrors the problem description, and one that is functional:<syntaxhighlight lang="jq">def numbers1: [ 5, 45, 23, 21, 67];
def numbers2: [43, 22, 78, 46, 38];
def numbers2: [43, 22, 78, 46, 38];
def numbers3: [ 9, 98, 12, 98, 53];</lang>
def numbers3: [ 9, 98, 12, 98, 53];</syntaxhighlight>
'''Mirroring the requirements'''
'''Mirroring the requirements'''
<lang jq>[range(0;5)
<syntaxhighlight lang="jq">[range(0;5)
| [numbers1[.], numbers2[.], numbers3[.]] | min]</lang>
| [numbers1[.], numbers2[.], numbers3[.]] | min]</syntaxhighlight>
'''Functional solution'''
'''Functional solution'''
<lang jq>[numbers1, numbers2, numbers3]
<syntaxhighlight lang="jq">[numbers1, numbers2, numbers3]
| transpose
| transpose
| map(min)</lang>
| map(min)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 366: Line 366:
=={{header|Julia}}==
=={{header|Julia}}==
Computed in the REPL, using matrix functions.
Computed in the REPL, using matrix functions.
<lang julia>
<syntaxhighlight lang="julia">
julia> Numbers1 = [5,45,23,21,67]
julia> Numbers1 = [5,45,23,21,67]
5-element Vector{Int64}:
5-element Vector{Int64}:
Line 406: Line 406:
21
21
38
38
</syntaxhighlight>
</lang>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>Min /@ Transpose@{{5, 45, 23, 21, 67}, {43, 22, 78, 46, 38}, {9, 98,
<syntaxhighlight lang="mathematica">Min /@ Transpose@{{5, 45, 23, 21, 67}, {43, 22, 78, 46, 38}, {9, 98,
12, 98, 53}}</lang>
12, 98, 53}}</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 416: Line 416:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>const
<syntaxhighlight lang="nim">const
Numbers1 = [ 5, 45, 23, 21, 67]
Numbers1 = [ 5, 45, 23, 21, 67]
Numbers2 = [43, 22, 78, 46, 38]
Numbers2 = [43, 22, 78, 46, 38]
Line 426: Line 426:
numbers[i] = min(min(Numbers1[i], Numbers2[i]), Numbers3[i])
numbers[i] = min(min(Numbers1[i], Numbers2[i]), Numbers3[i])


echo numbers</lang>
echo numbers</syntaxhighlight>


{{out}}
{{out}}
Line 432: Line 432:


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang oorexx>/* REXX */
<syntaxhighlight lang="oorexx">/* REXX */
l.1=.array~of( 5, 45, 23, 21, 67)
l.1=.array~of( 5, 45, 23, 21, 67)
l.2=.array~of(43, 22, 78, 46, 38)
l.2=.array~of(43, 22, 78, 46, 38)
Line 440: Line 440:
o=o min(l.1[i],l.2[i],l.3[i])
o=o min(l.1[i],l.2[i],l.3[i])
End
End
Say strip(o)</lang>
Say strip(o)</syntaxhighlight>
{{out}}
{{out}}
<pre>[5 22 12 21 38]</pre>
<pre>[5 22 12 21 38]</pre>
Line 446: Line 446:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use List::Util 'min';
use List::Util 'min';
Line 454: Line 454:
for my $i (0 .. $#{ $lists[0] }) {
for my $i (0 .. $#{ $lists[0] }) {
print ' ' . min map { $lists[$_][$i] } 0..$#lists;
print ' ' . min map { $lists[$_][$i] } 0..$#lists;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 5 22 12 21 38</pre>
<pre> 5 22 12 21 38</pre>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">N123</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">45</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">67</span><span style="color: #0000FF;">},</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">N123</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">45</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">67</span><span style="color: #0000FF;">},</span>
Line 465: Line 465:
<span style="color: #0000FF;">{</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">98</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">98</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">53</span><span style="color: #0000FF;">}}</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">98</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">98</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">53</span><span style="color: #0000FF;">}}</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">N123</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)}),</span><span style="color: #7060A8;">minsq</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">N123</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)}),</span><span style="color: #7060A8;">minsq</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 472: Line 472:


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Create a first list and a second list and a third list.
Create a first list and a second list and a third list.
Line 535: Line 535:
If the entry's next is not nil, write ", " 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.
Put the entry's next into the entry.
Repeat.</lang>
Repeat.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 542: Line 542:


=={{header|Python}}==
=={{header|Python}}==
<lang Python>numbers1 = [5,45,23,21,67]
<syntaxhighlight lang="python">numbers1 = [5,45,23,21,67]
numbers2 = [43,22,78,46,38]
numbers2 = [43,22,78,46,38]
numbers3 = [9,98,12,98,53]
numbers3 = [9,98,12,98,53]
Line 548: Line 548:
numbers = [min(numbers1[i],numbers2[i],numbers3[i]) for i in range(0,len(numbers1))]
numbers = [min(numbers1[i],numbers2[i],numbers3[i]) for i in range(0,len(numbers1))]


print(numbers)</lang>
print(numbers)</syntaxhighlight>
{{Output}}
{{Output}}
<pre>[5, 22, 12, 21, 38]</pre>
<pre>[5, 22, 12, 21, 38]</pre>
Line 554: Line 554:


Or, in terms of zip:
Or, in terms of zip:
<lang python>'''Minimum value in each column'''
<syntaxhighlight lang="python">'''Minimum value in each column'''


numbers1 = [5, 45, 23, 21, 67]
numbers1 = [5, 45, 23, 21, 67]
Line 563: Line 563:
min(x) for x
min(x) for x
in zip(*[numbers1, numbers2, numbers3])
in zip(*[numbers1, numbers2, numbers3])
])</lang>
])</syntaxhighlight>
{{Out}}
{{Out}}
<pre>[5, 22, 12, 21, 38]</pre>
<pre>[5, 22, 12, 21, 38]</pre>


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>say [Zmin] (5,45,23,21,67), (43,22,78,46,38), (9,98,12,98,53);</lang>
<syntaxhighlight lang="raku" line>say [Zmin] (5,45,23,21,67), (43,22,78,46,38), (9,98,12,98,53);</syntaxhighlight>
{{out}}
{{out}}
<pre>(5 22 12 21 38)</pre>
<pre>(5 22 12 21 38)</pre>


=={{header|Red}}==
=={{header|Red}}==
<lang rebol>Red [
<syntaxhighlight lang="rebol">Red [
Red-version: 0.6.4
Red-version: 0.6.4
Description: "Find the element-wise minimum of three lists"
Description: "Find the element-wise minimum of three lists"
Line 586: Line 586:
result/:i: min min numbers1/:i numbers2/:i numbers3/:i
result/:i: min min numbers1/:i numbers2/:i numbers3/:i
]
]
print result</lang>
print result</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 593: Line 593:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
w= 5 45 23 21 67 43 22 78 46 38 9 98 12 98 53
w= 5 45 23 21 67 43 22 78 46 38 9 98 12 98 53
Do i=1 To 3
Do i=1 To 3
Line 604: Line 604:
o=o min(l.1.j,l.2.j,l.3.j)
o=o min(l.1.j,l.2.j,l.3.j)
End
End
Say strip(o)</lang>
Say strip(o)</syntaxhighlight>
{{out}}
{{out}}
<pre>5 22 12 21 38</pre>
<pre>5 22 12 21 38</pre>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>see "? "working..."
<syntaxhighlight lang="ring">see "? "working..."


Num1 = [ 5,45,23,21,67]
Num1 = [ 5,45,23,21,67]
Line 627: Line 627:
rv = ar[1]
rv = ar[1]
for n = 2 to len(ar) rv += "," + ar[n] next
for n = 2 to len(ar) rv += "," + ar[n] next
return "[" + rv + "]"</lang>
return "[" + rv + "]"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 636: Line 636:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>numbers1 = [ 5, 45, 23, 21, 67]
<syntaxhighlight lang="ruby">numbers1 = [ 5, 45, 23, 21, 67]
numbers2 = [43, 22, 78, 46, 38]
numbers2 = [43, 22, 78, 46, 38]
numbers3 = [ 9, 98, 12, 98, 53]
numbers3 = [ 9, 98, 12, 98, 53]
p [numbers1, numbers2, numbers3].transpose.map(&:min)</lang>
p [numbers1, numbers2, numbers3].transpose.map(&:min)</syntaxhighlight>
{{out}}
{{out}}
<pre>[5, 22, 12, 21, 38]
<pre>[5, 22, 12, 21, 38]
Line 646: Line 646:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var lists = [
<syntaxhighlight lang="ruby">var lists = [
[ 5, 45, 23, 21, 67],
[ 5, 45, 23, 21, 67],
[43, 22, 78, 46, 38],
[43, 22, 78, 46, 38],
Line 652: Line 652:
]
]


say lists.zip.map{.min}</lang>
say lists.zip.map{.min}</syntaxhighlight>


{{out}}
{{out}}
Line 660: Line 660:


=={{header|Vlang}}==
=={{header|Vlang}}==
<lang vlang>import math
<syntaxhighlight lang="vlang">import math
fn main() {
fn main() {
numbers1 := [5, 45, 23, 21, 67]!
numbers1 := [5, 45, 23, 21, 67]!
Line 670: Line 670:
}
}
println(numbers)
println(numbers)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[5, 22, 12, 21, 38]</pre>
<pre>[5, 22, 12, 21, 38]</pre>


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var numbers1 = [ 5, 45, 23, 21, 67]
<syntaxhighlight lang="ecmascript">var numbers1 = [ 5, 45, 23, 21, 67]
var numbers2 = [43, 22, 78, 46, 38]
var numbers2 = [43, 22, 78, 46, 38]
var numbers3 = [ 9, 98, 12, 98, 53]
var numbers3 = [ 9, 98, 12, 98, 53]
var numbers = List.filled(5, 0)
var numbers = List.filled(5, 0)
for (n in 0..4) numbers[n] = numbers1[n].min(numbers2[n]).min(numbers3[n])
for (n in 0..4) numbers[n] = numbers1[n].min(numbers2[n]).min(numbers3[n])
System.print(numbers)</lang>
System.print(numbers)</syntaxhighlight>


{{out}}
{{out}}
Line 688: Line 688:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func Min(A, B);
<syntaxhighlight lang="xpl0">func Min(A, B);
int A, B;
int A, B;
return if A<B then A else B;
return if A<B then A else B;
Line 700: Line 700:
ChOut(0, ^ );
ChOut(0, ^ );
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}

Revision as of 22:42, 27 August 2022

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

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]

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

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]

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