Collect and sort square numbers in ascending order from three lists: Difference between revisions

m
syntax highlighting fixup automation
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 9:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V lists = [[3, 4, 34, 25, 9, 12, 36, 56, 36], [2, 8, 81, 169, 34, 55, 76, 49, 7], [75, 121, 75, 144, 35, 16, 46, 35]]
[Int] squares
 
Line 21:
 
squares.sort()
print(squares)</langsyntaxhighlight>
 
{{out}}
Line 30:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-rows}}
<langsyntaxhighlight lang="algol68">BEGIN # construct a list of the squares from some other lists #
# lists are represented by arrays in this sample #
PR read "rows.incl.a68" PR # sorting and printing utilities #
Line 62:
QUICKSORT squares FROMELEMENT 1 TOELEMENT r pos;
SHOW squares[ 1 : r pos ]
END</langsyntaxhighlight>
{{out}}
<pre>
Line 69:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">lists: [
[3,4,34,25,9,12,36,56,36]
[2,8,81,169,34,55,76,49,7]
Line 78:
square?: function [n]-> contains? squares n
 
print select sort fold.seed:[] lists [a,b][a++b] => square?</langsyntaxhighlight>
 
{{out}}
Line 85:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="AutoHotkey">list := [], t := [], result := []
 
list[1] := [3,4,34,25,9,12,36,56,36]
Line 98:
for n, obj in t
for i, v in obj
result.push(n)</langsyntaxhighlight>
{{out}}
<pre>[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="AWK">
# syntax: GAWK -f COLLECT_AND_SORT_SQUARE_NUMBERS_IN_ASCENDING_ORDER_FROM_THREE_LISTS.AWK
BEGIN {
Line 127:
return (int(sqrt(n))^2 == n)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 134:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Collect and sort square numbers in ascending order from three lists. Nigel Galloway: December 21st., 2021
let fN g=g*g in printfn "%A" (([3;4;34;25;9;12;36;56;36]@[2;8;81;169;34;55;76;49;7]@[75;121;75;144;35;16;46;35])|>List.filter(fun n->(float>>sqrt>>int>>fN)n=n)|>List.sort)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 143:
</pre>
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function issquare( n as integer ) as boolean
if int(sqr(n))^2=n then return true else return false
end function
Line 173:
for i = 0 to ubound(squares) 'output data
print squares(i);" ";
next i</langsyntaxhighlight>
{{out}}<pre>4 9 16 25 36 36 49 81 121 144 169</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 206:
sort.Ints(squares)
fmt.Println(squares)
}</langsyntaxhighlight>
 
{{out}}
Line 217:
'''Works with gojq, the Go implementation of jq'''
 
<langsyntaxhighlight lang="jq">def isSquare: sqrt | (. == floor);</langsyntaxhighlight>
 
'''The Task'''
<syntaxhighlight lang="text">def lists: [
[3,4,34,25,9,12,36,56,36],
[2,8,81,169,34,55,76,49,7],
Line 226:
];
 
[lists[][]] | unique | map(select(isSquare))</langsyntaxhighlight>
{{out}}
<pre>
Line 234:
=={{header|Julia}}==
vcat is list "addition" in Julia.
<langsyntaxhighlight lang="julia">lists = [
[3,4,34,25,9,12,36,56,36],
[2,8,81,169,34,55,76,49,7],
Line 243:
sort!(squares)
println(squares)
</syntaxhighlight>
</lang>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="Mathematica">list1 = {3, 4, 34, 25, 9, 12, 36, 56, 36};
list2 = {2, 8, 81, 169, 34, 55, 76, 49, 7};
list3 = {75, 121, 75, 144, 35, 16, 46, 35};
Sort@Select[Join[list1, list2, list3], IntegerQ[Sqrt[#]] &]</langsyntaxhighlight>
 
{{out}}<pre>
Line 255:
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">Parse Version v
Say v
l.1=.array~of(3,4,34,25,9,12,36,56,36)
Line 303:
End
Say ol
Exit</langsyntaxhighlight>
{{out}}
<pre>REXX-ooRexx_5.0.0(MT)_64-bit 6.05 8 Sep 2020
Line 310:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Add_and_sort_square_numbers_in_ascending_order_from_three_lists
Line 323:
 
my $sum = sum my @squares = grep is_square($_), sort { $a <=> $b } map @$_, @lists;
print "sum $sum - @squares\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 330:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang="Phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">squares</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">lists</span><span style="color: #0000FF;">)</span>
Line 342:
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">81</span><span style="color: #0000FF;">,</span><span style="color: #000000;">169</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">55</span><span style="color: #0000FF;">,</span><span style="color: #000000;">76</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">75</span><span style="color: #0000FF;">,</span><span style="color: #000000;">121</span><span style="color: #0000FF;">,</span><span style="color: #000000;">75</span><span style="color: #0000FF;">,</span><span style="color: #000000;">144</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">46</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">}})</span>
<!--</langsyntaxhighlight>-->
Alternatively, and a smidgen faster but not enough to be measurable here, you could replace that inner while loop with:
<!--<langsyntaxhighlight lang="Phix">(phixonline)-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[$],</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">></span><span style="color: #000000;">s</span> <span style="color: #008080;">do</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">sq</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">;</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 355:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
import math
 
Line 374:
print(Squares)
print("done...")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 383:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my $s = cache sort ( my $l = ( cache flat
 
[3,4,34,25,9,12,36,56,36],
Line 392:
 
put 'Added - Sorted';
put ' ' ~ $s.sum ~ ' ' ~ $s.gist;</langsyntaxhighlight>
<pre>Added - Sorted
690 (4 9 16 25 36 36 49 81 121 144 169)
Line 398:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">Parse Version v
Say v
l.=0
Line 452:
End
Say ol
Exit</langsyntaxhighlight>
{{out}}
<pre>REXX-Regina_3.9.4(MT) 5.00 25 Oct 2021
Line 459:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 499:
txt = txt + "]"
see txt
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 508:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var lists = [
[3,4,34,25,9,12,36,56,36],
[2,8,81,169,34,55,76,49,7],
Line 514:
]
 
say lists.flat.grep{.is_square}.sort</langsyntaxhighlight>
 
{{out}}
Line 523:
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
 
var lists = [
Line 536:
}
squares.sort()
System.print(squares)</langsyntaxhighlight>
 
{{out}}
Line 552:
Displaying a small number of sorted items is easily done with a few lines
of code.
<langsyntaxhighlight lang="XPL0">int List(3+1), Min, I, J, S, MI, MJ;
def Inf = -1>>1;
[List(1):= [3,4,34,25,9,12,36,56,36];
Line 569:
List(MI, MJ):= Inf;
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits