Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Produce a language-to-language conversion: from Factor to C++, same semantics. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Port the provided Factor code into Java while preserving the original functionality. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Transform the following Factor implementation into Java, maintaining the same output and logic. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Translate the given Factor code snippet into Python without altering its behavior. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Translate the given Factor code snippet into Python without altering its behavior. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Port the following code from Factor to VB with equivalent syntax and logic. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Maintain the same structure and functionality when rewriting this code in VB. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Preserve the algorithm and functionality while converting the code from Factor to Go. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Keep all operations the same but rewrite the snippet in Go. | USING: combinators combinators.smart kernel math
math.statistics prettyprint sequences sorting ;
IN: rosetta-code.five-number
<PRIVATE
: bisect ( seq -- lower upper )
dup length even? [ halves ]
[ dup midpoint@ 1 + [ head ] [ tail* ] 2bi ] if ;
: (fivenum) ( seq -- summary )
natural-sort {
[ infimum ]
[ bisect drop median ]
[ median ]
[ bisect nip median ]
[ supremum ]
} cleave>array ;
PRIVATE>
ERROR: fivenum-empty data ;
ERROR: fivenum-nan data ;
: fivenum ( seq -- summary )
{
{ [ dup empty? ] [ fivenum-empty ] }
{ [ dup [ fp-nan? ] any? ] [ fivenum-nan ] }
[ (fivenum) ]
} cond ;
: fivenum-demo ( -- )
{ 15 6 42 41 7 36 49 40 39 47 43 }
{ 36 40 7 39 41 15 }
{ 0.14082834 0.09748790 1.73131507 0.87636009
-1.95059594 0.73438555 -0.03035726 1.46675970
-0.74621349 -0.72588772 0.63905160 0.61501527
-0.98983780 -1.00447874 -0.62759469 0.66206163
1.04312009 -0.10305385 0.75775634 0.32566578 }
[ fivenum . ] tri@ ;
MAIN: fivenum-demo
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Generate an equivalent C version of this Groovy code. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Produce a language-to-language conversion: from Groovy to C, same semantics. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Rewrite the snippet below in C# so it works the same as the original Groovy code. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Change the programming language of this snippet from Groovy to C# without modifying what it does. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Rewrite the snippet below in C++ so it works the same as the original Groovy code. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Generate a C++ translation of this Groovy snippet without changing its computational steps. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Generate an equivalent Java version of this Groovy code. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Rewrite the snippet below in Java so it works the same as the original Groovy code. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Can you help me rewrite this code in Python instead of Groovy, keeping it the same logically? | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Ensure the translated Python code behaves exactly like the original Groovy snippet. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Port the provided Groovy code into VB while preserving the original functionality. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Rewrite the snippet below in VB so it works the same as the original Groovy code. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Produce a functionally identical Go code for the snippet given in Groovy. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Please provide an equivalent version of this Groovy code in Go. | class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1
if (size <= 0) {
throw new IllegalArgumentException("Array slice cannot be empty")
}
int m = start + (int) (size / 2)
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN()) {
throw new IllegalArgumentException("Unable to deal with arrays containing NaN")
}
}
double[] result = new double[5]
Arrays.sort(x)
result[0] = x[0]
result[2] = median(x, 0, x.length - 1)
result[4] = x[x.length - 1]
int m = (int) (x.length / 2)
int lowerEnd = (x.length % 2 == 1) ? m : m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.length - 1)
return result
}
static void main(String[] args) {
double[][] xl = [
[15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
]
]
for (double[] x : xl) {
println("${fivenum(x)}")
}
}
}
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Rewrite this program in C while keeping its functionality equivalent to the Haskell version. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Write the same algorithm in C as shown in this Haskell implementation. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Write a version of this Haskell function in C# with identical behavior. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Port the provided Haskell code into C# while preserving the original functionality. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Produce a language-to-language conversion: from Haskell to C++, same semantics. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Convert this Haskell snippet to C++ and keep its semantics consistent. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Transform the following Haskell implementation into Java, maintaining the same output and logic. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Please provide an equivalent version of this Haskell code in Java. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Write the same algorithm in Python as shown in this Haskell implementation. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Maintain the same structure and functionality when rewriting this code in Python. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Rewrite this program in VB while keeping its functionality equivalent to the Haskell version. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Generate a Go translation of this Haskell snippet without changing its computational steps. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Preserve the algorithm and functionality while converting the code from Haskell to Go. | import Data.List (sort)
fivenum :: [Double] -> [Double]
fivenum [] = []
fivenum xs
| l >= 5 =
fmap
( (/ 2)
. ( (+) . (!!) s
. floor
<*> (!!) s . ceiling
)
. pred
)
[1, q, succ l / 2, succ l - q, l]
| otherwise = s
where
l = realToFrac $ length xs
q = realToFrac (floor $ (l + 3) / 2) / 2
s = sort xs
main :: IO ()
main =
print $
fivenum
[ 0.14082834,
0.09748790,
1.73131507,
0.87636009,
-1.95059594,
0.73438555,
-0.03035726,
1.46675970,
-0.74621349,
-0.72588772,
0.63905160,
0.61501527,
-0.98983780,
-1.00447874,
-0.62759469,
0.66206163,
1.04312009,
-0.10305385,
0.75775634,
0.32566578
]
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Port the provided J code into C while preserving the original functionality. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Ensure the translated C code behaves exactly like the original J snippet. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Write a version of this J function in C# with identical behavior. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Keep all operations the same but rewrite the snippet in C#. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Port the provided J code into C++ while preserving the original functionality. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Transform the following J implementation into C++, maintaining the same output and logic. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Port the provided J code into Java while preserving the original functionality. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Produce a functionally identical Java code for the snippet given in J. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Produce a functionally identical Python code for the snippet given in J. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Translate the given J code snippet into Python without altering its behavior. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Convert the following code from J to VB, ensuring the logic remains intact. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Change the programming language of this snippet from J to VB without modifying what it does. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Write the same code in Go as shown below in J. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Generate a Go translation of this J snippet without changing its computational steps. | midpts=: (1 + #) <:@(] , -:@[ , -) -:@<.@-:@(3 + #)
quartiles=: -:@(+/)@((<. ,: >.)@midpts { /:~@])
fivenum=: <./ , quartiles , >./
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Convert this Julia snippet to C and keep its semantics consistent. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Preserve the algorithm and functionality while converting the code from Julia to C. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Produce a language-to-language conversion: from Julia to C#, same semantics. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Rewrite the snippet below in C# so it works the same as the original Julia code. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Please provide an equivalent version of this Julia code in C++. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Produce a functionally identical C++ code for the snippet given in Julia. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Generate a Java translation of this Julia snippet without changing its computational steps. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Change the following Julia code into Java without altering its purpose. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Produce a functionally identical Python code for the snippet given in Julia. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Produce a functionally identical Python code for the snippet given in Julia. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Can you help me rewrite this code in VB instead of Julia, keeping it the same logically? | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Change the programming language of this snippet from Julia to VB without modifying what it does. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Produce a functionally identical Go code for the snippet given in Julia. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Translate this program into Go but keep the logic exactly as in Julia. | function mediansorted(x::AbstractVector{T}, i::Integer, l::Integer)::T where T
len = l - i + 1
len > zero(len) || throw(ArgumentError("Array slice cannot be empty."))
mid = i + len ÷ 2
return isodd(len) ? x[mid] : (x[mid-1] + x[mid]) / 2
end
function fivenum(x::AbstractVector{T}) where T<:AbstractFloat
r = Vector{T}(5)
xs = sort(x)
mid::Int = length(xs) ÷ 2
lowerend::Int = isodd(length(xs)) ? mid : mid - 1
r[1] = xs[1]
r[2] = mediansorted(xs, 1, lowerend)
r[3] = mediansorted(xs, 1, endof(xs))
r[4] = mediansorted(xs, mid, endof(xs))
r[end] = xs[end]
return r
end
for v in ([15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0],
[36.0, 40.0, 7.0, 39.0, 41.0, 15.0],
[0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578])
println("
end
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Produce a language-to-language conversion: from Lua to C, same semantics. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Rewrite the snippet below in C so it works the same as the original Lua code. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Convert this Lua snippet to C# and keep its semantics consistent. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Rewrite this program in C# while keeping its functionality equivalent to the Lua version. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Rewrite this program in C++ while keeping its functionality equivalent to the Lua version. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Maintain the same structure and functionality when rewriting this code in C++. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Port the provided Lua code into Java while preserving the original functionality. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Maintain the same structure and functionality when rewriting this code in Java. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Rewrite this program in Python while keeping its functionality equivalent to the Lua version. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Generate an equivalent Python version of this Lua code. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Write the same code in VB as shown below in Lua. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Translate this program into VB but keep the logic exactly as in Lua. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Maintain the same structure and functionality when rewriting this code in Go. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Ensure the translated Go code behaves exactly like the original Lua snippet. | function slice(tbl, low, high)
local copy = {}
for i=low or 1, high or #tbl do
copy[#copy+1] = tbl[i]
end
return copy
end
function median(tbl)
m = math.floor(#tbl / 2) + 1
if #tbl % 2 == 1 then
return tbl[m]
end
return (tbl[m-1] + tbl[m]) / 2
end
function fivenum(tbl)
table.sort(tbl)
r0 = tbl[1]
r2 = median(tbl)
r4 = tbl[#tbl]
m = math.floor(#tbl / 2)
if #tbl % 2 == 1 then
low = m
else
low = m - 1
end
r1 = median(slice(tbl, nil, low+1))
r3 = median(slice(tbl, low+2, nil))
return r0, r1, r2, r3, r4
end
x1 = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
}
for i,x in ipairs(x1) do
print(fivenum(x))
end
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Convert the following code from Mathematica to C, ensuring the logic remains intact. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Write the same algorithm in C as shown in this Mathematica implementation. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Keep all operations the same but rewrite the snippet in C#. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Port the following code from Mathematica to C# with equivalent syntax and logic. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Write the same code in C++ as shown below in Mathematica. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Preserve the algorithm and functionality while converting the code from Mathematica to C++. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Rewrite this program in Java while keeping its functionality equivalent to the Mathematica version. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Generate a Java translation of this Mathematica snippet without changing its computational steps. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Change the programming language of this snippet from Mathematica to Python without modifying what it does. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Translate the given Mathematica code snippet into Python without altering its behavior. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| from __future__ import division
import math
import sys
def fivenum(array):
n = len(array)
if n == 0:
print("you entered an empty array.")
sys.exit()
x = sorted(array)
n4 = math.floor((n+3.0)/2.0)/2.0
d = [1, n4, (n+1)/2, n+1-n4, n]
sum_array = []
for e in range(5):
floor = int(math.floor(d[e] - 1))
ceil = int(math.ceil(d[e] - 1))
sum_array.append(0.5 * (x[floor] + x[ceil]))
return sum_array
x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970,
-0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163,
1.04312009, -0.10305385, 0.75775634, 0.32566578]
y = fivenum(x)
print(y)
|
Preserve the algorithm and functionality while converting the code from Mathematica to VB. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Port the provided Mathematica code into VB while preserving the original functionality. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| #define floor(x) ((x*2.0-0.5) Shr 1)
Sub rapidSort (array()As Single, l As Integer, r As Integer)
Dim As Integer n, wert, nptr, rep
Dim As Single arr, LoVal = array(l), HiVal = array(r)
For n = l To r
If LoVal > array(n) Then LoVal = array(n)
If HiVal < array(n) Then HiVal = array(n)
Next n
Redim SortArray(LoVal To HiVal) As Single
For n = l To r
wert = array(n)
SortArray(wert) += 1
Next n
nptr = l-1
For arr = LoVal To HiVal
rep = SortArray(arr)
For n = 1 To rep
nptr += 1
array(nptr) = arr
Next n
Next arr
Erase SortArray
End Sub
Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
Dim As Integer l = hi-lo+1
Dim As Integer m = lo+floor(l/2)
If l Mod 2 = 1 Then Return tbl(m)
Return (tbl(m-1)+tbl(m))/2
End Function
Sub fivenum(tbl() As Single)
rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
Dim As Integer l = Ubound(tbl)
Dim As Single m = floor(l/2) + (l Mod 2)
Dim As Single r1,r2,r3,r4,r5
r1 = tbl(1)
r2 = median(tbl(),1,m)
r3 = median(tbl(),1,l)
r4 = median(tbl(),m+1,l)
r5 = tbl(l)
Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub
Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, _
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, _
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())
Sleep
|
Ensure the translated Go code behaves exactly like the original Mathematica snippet. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Transform the following Mathematica implementation into Go, maintaining the same output and logic. | ClearAll[FiveNum]
FiveNum[x_List] := Quantile[x, Range[0, 1, 1/4]]
FiveNum[RandomVariate[NormalDistribution[], 10000]]
| package main
import (
"fmt"
"math"
"sort"
)
func fivenum(a []float64) (n5 [5]float64) {
sort.Float64s(a)
n := float64(len(a))
n4 := float64((len(a)+3)/2) / 2
d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
for e, de := range d {
floor := int(de - 1)
ceil := int(math.Ceil(de - 1))
n5[e] = .5 * (a[floor] + a[ceil])
}
return
}
var (
x1 = []float64{36, 40, 7, 39, 41, 15}
x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
x3 = []float64{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
}
)
func main() {
fmt.Println(fivenum(x1))
fmt.Println(fivenum(x2))
fmt.Println(fivenum(x3))
}
|
Generate an equivalent C version of this MATLAB code. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Convert the following code from MATLAB to C, ensuring the logic remains intact. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| #include <stdio.h>
#include <stdlib.h>
double median(double *x, int start, int end_inclusive) {
int size = end_inclusive - start + 1;
if (size <= 0) {
printf("Array slice cannot be empty\n");
exit(1);
}
int m = start + size / 2;
if (size % 2) return x[m];
return (x[m - 1] + x[m]) / 2.0;
}
int compare (const void *a, const void *b) {
double aa = *(double*)a;
double bb = *(double*)b;
if (aa > bb) return 1;
if (aa < bb) return -1;
return 0;
}
int fivenum(double *x, double *result, int x_len) {
int i, m, lower_end;
for (i = 0; i < x_len; i++) {
if (x[i] != x[i]) {
printf("Unable to deal with arrays containing NaN\n\n");
return 1;
}
}
qsort(x, x_len, sizeof(double), compare);
result[0] = x[0];
result[2] = median(x, 0, x_len - 1);
result[4] = x[x_len - 1];
m = x_len / 2;
lower_end = (x_len % 2) ? m : m - 1;
result[1] = median(x, 0, lower_end);
result[3] = median(x, m, x_len - 1);
return 0;
}
int show(double *result, int places) {
int i;
char f[7];
sprintf(f, "%%.%dlf", places);
printf("[");
for (i = 0; i < 5; i++) {
printf(f, result[i]);
if (i < 4) printf(", ");
}
printf("]\n\n");
}
int main() {
double result[5];
double x1[11] = {15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0};
if (!fivenum(x1, result, 11)) show(result, 1);
double x2[6] = {36.0, 40.0, 7.0, 39.0, 41.0, 15.0};
if (!fivenum(x2, result, 6)) show(result, 1);
double x3[20] = {
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
};
if (!fivenum(x3, result, 20)) show(result, 9);
return 0;
}
|
Port the following code from MATLAB to C# with equivalent syntax and logic. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Translate the given MATLAB code snippet into C# without altering its behavior. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fivenum {
public static class Helper {
public static string AsString<T>(this ICollection<T> c, string format = "{0}") {
StringBuilder sb = new StringBuilder("[");
int count = 0;
foreach (var t in c) {
if (count++ > 0) {
sb.Append(", ");
}
sb.AppendFormat(format, t);
}
return sb.Append("]").ToString();
}
}
class Program {
static double Median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new ArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] Fivenum(double[] x) {
foreach (var d in x) {
if (Double.IsNaN(d)) {
throw new ArgumentException("Unable to deal with arrays containing NaN");
}
}
double[] result = new double[5];
Array.Sort(x);
result[0] = x.First();
result[2] = Median(x, 0, x.Length - 1);
result[4] = x.Last();
int m = x.Length / 2;
int lowerEnd = (x.Length % 2 == 1) ? m : m - 1;
result[1] = Median(x, 0, lowerEnd);
result[3] = Median(x, m, x.Length - 1);
return result;
}
static void Main(string[] args) {
double[][] x1 = new double[][]{
new double[]{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
new double[]{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
new double[]{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
},
};
foreach(var x in x1) {
var result = Fivenum(x);
Console.WriteLine(result.AsString("{0:F8}"));
}
}
}
}
|
Rewrite the snippet below in C++ so it works the same as the original MATLAB code. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Convert this MATLAB block to C++, preserving its control flow and logic. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| #include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
template<std::size_t> struct int_ {};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos>) {
out << std::get< std::tuple_size<Tuple>::value - Pos >(t) << ", ";
return print_tuple(out, t, int_<Pos - 1>());
}
template <class Tuple>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<1>) {
return out << std::get<std::tuple_size<Tuple>::value - 1>(t);
}
template <class... Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& t) {
out << '(';
print_tuple(out, t, int_<sizeof...(Args)>());
return out << ')';
}
template <class RI>
double median(RI beg, RI end) {
if (beg == end) throw std::runtime_error("Range cannot be empty");
auto len = end - beg;
auto m = len / 2;
if (len % 2 == 1) {
return *(beg + m);
}
return (beg[m - 1] + beg[m]) / 2.0;
}
template <class C>
auto fivenum(C& c) {
std::sort(c.begin(), c.end());
auto cbeg = c.cbegin();
auto cend = c.cend();
auto len = cend - cbeg;
auto m = len / 2;
auto lower = (len % 2 == 1) ? m : m - 1;
double r2 = median(cbeg, cbeg + lower + 1);
double r3 = median(cbeg, cend);
double r4 = median(cbeg + lower + 1, cend);
return std::make_tuple(*cbeg, r2, r3, r4, *(cend - 1));
}
int main() {
using namespace std;
vector<vector<double>> cs = {
{ 15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0 },
{ 36.0, 40.0, 7.0, 39.0, 41.0, 15.0 },
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (auto & c : cs) {
cout << fivenum(c) << endl;
}
return 0;
}
|
Preserve the algorithm and functionality while converting the code from MATLAB to Java. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Generate an equivalent Java version of this MATLAB code. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
end;
| import java.util.Arrays;
public class Fivenum {
static double median(double[] x, int start, int endInclusive) {
int size = endInclusive - start + 1;
if (size <= 0) throw new IllegalArgumentException("Array slice cannot be empty");
int m = start + size / 2;
return (size % 2 == 1) ? x[m] : (x[m - 1] + x[m]) / 2.0;
}
static double[] fivenum(double[] x) {
for (Double d : x) {
if (d.isNaN())
throw new IllegalArgumentException("Unable to deal with arrays containing NaN");
}
double[] result = new double[5];
Arrays.sort(x);
result[0] = x[0];
result[2] = median(x, 0, x.length - 1);
result[4] = x[x.length - 1];
int m = x.length / 2;
int lowerEnd = (x.length % 2 == 1) ? m : m - 1;
result[1] = median(x, 0, lowerEnd);
result[3] = median(x, m, x.length - 1);
return result;
}
public static void main(String[] args) {
double xl[][] = {
{15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0},
{36.0, 40.0, 7.0, 39.0, 41.0, 15.0},
{
0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555,
-0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527,
-0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385,
0.75775634, 0.32566578
}
};
for (double[] x : xl) System.out.printf("%s\n\n", Arrays.toString(fivenum(x)));
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.