Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Convert this MATLAB block to Python, preserving its control flow and logic. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
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)
|
Please provide an equivalent version of this MATLAB code in Python. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
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)
|
Ensure the translated VB code behaves exactly like the original MATLAB snippet. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
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
|
Write the same algorithm in VB as shown in this MATLAB implementation. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
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
|
Preserve the algorithm and functionality while converting the code from MATLAB to Go. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
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 MATLAB. | function r = fivenum(x)
r = quantile(x,[0:4]/4);
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))
}
|
Maintain the same structure and functionality when rewriting this code in C. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| #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;
}
|
Change the following Nim code into C without altering its purpose. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| #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;
}
|
Translate the given Nim code snippet into C# without altering its behavior. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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 a version of this Nim function in C# with identical behavior. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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}"));
}
}
}
}
|
Generate an equivalent C++ version of this Nim code. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| #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;
}
|
Change the following Nim code into C++ without altering its purpose. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| #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;
}
|
Write the same code in Java as shown below in Nim. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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)));
}
}
|
Port the provided Nim code into Python while preserving the original functionality. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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 Nim code into Python while preserving the original functionality. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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)
|
Please provide an equivalent version of this Nim code in VB. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| #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 Nim. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| #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 following Nim code into Go without altering its purpose. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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 the snippet below in Go so it works the same as the original Nim code. | import algorithm
type FiveNum = array[5, float]
template isOdd(n: SomeInteger): bool = (n and 1) != 0
func median(x: openArray[float]; startIndex, endIndex: Natural): float =
let size = endIndex - startIndex + 1
assert(size > 0, "array slice cannot be empty")
let m = startIndex + size div 2
result = if size.isOdd: x[m] else: (x[m-1] + x[m]) / 2
func fivenum(x: openArray[float]): FiveNum =
let x = sorted(x)
let m = x.len div 2
let lowerEnd = if x.len.isOdd: m else: m - 1
result[0] = x[0]
result[1] = median(x, 0, lowerEnd)
result[2] = median(x, 0, x.high)
result[3] = median(x, m, x.high)
result[4] = x[^1]
const Lists = [@[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 list in Lists:
echo ""
echo list
echo " β ", list.fivenum
| 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))
}
|
Write a version of this Perl function in C with identical behavior. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| #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;
}
|
Please provide an equivalent version of this Perl code in C. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| #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 Perl block to C#, preserving its control flow and logic. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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 a version of this Perl function in C# with identical behavior. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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 Perl code. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| #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;
}
|
Write a version of this Perl function in C++ with identical behavior. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| #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 Perl to Java. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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 Perl. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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 code in Python as shown below in Perl. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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 Perl to Python with equivalent syntax and logic. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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)
|
Transform the following Perl implementation into VB, maintaining the same output and logic. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| #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 Perl to VB without modifying what it does. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| #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 Perl snippet. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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. | use POSIX qw(ceil floor);
sub fivenum {
my(@array) = @_;
my $n = scalar @array;
die "No values were entered into fivenum!" if $n == 0;
my @x = sort {$a <=> $b} @array;
my $n4 = floor(($n+3)/2)/2;
my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);
my @sum_array;
for my $e (0..4) {
my $floor = floor($d[$e]-1);
my $ceil = ceil($d[$e]-1);
push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
}
return @sum_array;
}
my @x = (15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43);
my @tukey = fivenum(\@x);
say join (',', @tukey);
@x = (36, 40, 7, 39, 41, 15),
@tukey = fivenum(\@x);
say join (',', @tukey);
@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);
@tukey = fivenum(\@x);
say join (',', @tukey);
| 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))
}
|
Maintain the same structure and functionality when rewriting this code in C. | x <- c(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(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;
}
|
Maintain the same structure and functionality when rewriting this code in C. | x <- c(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(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 this program in C# while keeping its functionality equivalent to the R version. | x <- c(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(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}"));
}
}
}
}
|
Keep all operations the same but rewrite the snippet in C#. | x <- c(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(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}"));
}
}
}
}
|
Preserve the algorithm and functionality while converting the code from R to C++. | x <- c(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(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;
}
|
Write the same algorithm in C++ as shown in this R implementation. | x <- c(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(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;
}
|
Write a version of this R function in Java with identical behavior. | x <- c(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(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)));
}
}
|
Port the following code from R to Java with equivalent syntax and logic. | x <- c(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(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)));
}
}
|
Produce a functionally identical Python code for the snippet given in R. | x <- c(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(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)
|
Change the following R code into Python without altering its purpose. | x <- c(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(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)
|
Convert this R block to VB, preserving its control flow and logic. | x <- c(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(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
|
Translate the given R code snippet into VB without altering its behavior. | x <- c(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(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
|
Translate the given R code snippet into Go without altering its behavior. | x <- c(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(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))
}
|
Change the programming language of this snippet from R to Go without modifying what it does. | x <- c(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(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))
}
|
Can you help me rewrite this code in C instead of Racket, keeping it the same logically? | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| #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 Racket function in C with identical behavior. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| #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;
}
|
Transform the following Racket implementation into C#, maintaining the same output and logic. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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 Racket code. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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}"));
}
}
}
}
|
Maintain the same structure and functionality when rewriting this code in C++. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| #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 Racket snippet to C++ and keep its semantics consistent. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| #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 Racket code into Java while preserving the original functionality. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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 Java as shown in this Racket implementation. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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 Python. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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 a Python translation of this Racket snippet without changing its computational steps. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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 a version of this Racket function in VB with identical behavior. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| #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 Racket code. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| #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 following code from Racket to Go with equivalent syntax and logic. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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 Go version of this Racket code. | #lang racket/base
(require math/private/statistics/quickselect)
(define (fivenum! data-v)
(define (tukey-median start end)
(define-values (n/2 parity) (quotient/remainder (- end start) 2))
(define mid (+ start n/2))
(if (zero? parity)
(/ (+ (data-kth-value! (+ mid (sub1 parity))) (data-kth-value! mid)) 2)
(data-kth-value! mid)))
(define n-data (let ((l (vector-length data-v)))
(if (zero? l)
(raise-argument-error 'data-v "nonempty (Vectorof Real)" data-v)
l)))
(define (data-kth-value! n) (kth-value! data-v n <))
(define subset-size (let-values (((n/2 parity) (quotient/remainder n-data 2))) (+ n/2 parity)))
(vector (data-kth-value! 0)
(tukey-median 0 subset-size)
(tukey-median 0 n-data)
(tukey-median (- n-data subset-size) n-data)
(data-kth-value! (sub1 n-data))))
(define (fivenum data-seq)
(fivenum! (if (and (vector? data-seq) (not (immutable? data-seq)))
data-seq
(for/vector ((datum data-seq)) datum))))
(module+ test
(require rackunit
racket/vector)
(check-equal? #(14 14 14 14 14) (fivenum #(14)) "Minimal case")
(check-equal? #(8 11 14 17 20) (fivenum #(8 14 20)) "3-value case")
(check-equal? #(8 11 15 18 20) (fivenum #(8 14 16 20)) "4-value case")
(define x1-seq #(36 40 7 39 41 15))
(define x1-v (vector-copy x1-seq))
(check-equal? x1-seq x1-v "before fivenum! sequence and vector were not `equal?`")
(check-equal? #(7 15 #e37.5 40 41) (fivenum! x1-v) "Test against Go results x1")
(check-not-equal? x1-seq x1-v "fivenum! did not mutate mutable input vectors")
(check-equal? #(6 #e25.5 40 #e42.5 49) (fivenum #(15 6 42 41 7 36 49 40 39 47 43)) "Test against Go results x2")
(check-equal? #(-1.95059594 -0.676741205 0.23324706 0.746070945 1.73131507)
(fivenum (vector 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))
"Test against Go results x3"))
| 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 REXX block to C, preserving its control flow and logic. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| #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 REXX to C, ensuring the logic remains intact. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| #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 REXX to C#, ensuring the logic remains intact. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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}"));
}
}
}
}
|
Convert this REXX snippet to C# and keep its semantics consistent. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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 following REXX code into C++ without altering its purpose. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| #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;
}
|
Can you help me rewrite this code in C++ instead of REXX, keeping it the same logically? |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| #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;
}
|
Translate this program into Java but keep the logic exactly as in REXX. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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 this program into Java but keep the logic exactly as in REXX. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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)));
}
}
|
Ensure the translated Python code behaves exactly like the original REXX snippet. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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 a version of this REXX function in Python with identical behavior. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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 this program into VB but keep the logic exactly as in REXX. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| #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 this program in VB while keeping its functionality equivalent to the REXX version. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| #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 the given REXX code snippet into Go without altering its behavior. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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 following code from REXX to Go with equivalent syntax and logic. |
parse arg x
if x='' then x= 15 6 42 41 7 36 49 40 39 47 43
say 'input numbers: ' space(x)
call 5num
say ' fiveβnumbers: ' result
exit
bSort: procedure expose @.; parse arg n; m=n-1
do m=m for m by -1 until ok; ok= 1
do j=1 for m; k= j + 1;
if @.j<=@.k then iterate
parse value @.j @.k 0 with @.k @.j ok
end
end
med: arg s,e; $=e-s+1; m=s+$%2; if $//2 then return @.m; _=m-1; return (@._+@.m)/2
5num: #= words(x); if #==0 then return '***error*** array is empty.'
parse var x . 1 LO . 1 HI .
q2= # % 2; er= '***error*** element'
do j=1 for #; @.j= word(x, j)
if \datatype(@.j, 'N') then return er j "isn't numeric: " @.j
LO= min(LO, @.j); HI= max(HI, @.j)
end
call bSort #
if #//2 then p25= q2
else p25= q2 - 1
return LO med(1, p25) med(1, #) med(q2, #) HI
| 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 C code behaves exactly like the original Ruby snippet. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| #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 this program in C while keeping its functionality equivalent to the Ruby version. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| #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 Ruby to C#, ensuring the logic remains intact. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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 following Ruby code into C# without altering its purpose. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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}"));
}
}
}
}
|
Can you help me rewrite this code in C++ instead of Ruby, keeping it the same logically? | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| #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;
}
|
Change the programming language of this snippet from Ruby to C++ without modifying what it does. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| #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 Ruby code. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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 Ruby code. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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)));
}
}
|
Port the following code from Ruby to Python with equivalent syntax and logic. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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 Ruby to VB, ensuring the logic remains intact. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| #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
|
Convert this Ruby snippet to VB and keep its semantics consistent. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| #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 Ruby. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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 Ruby to Go, ensuring the logic remains intact. | def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e|
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
| 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 following code from Scala to C with equivalent syntax and logic. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| #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;
}
|
Can you help me rewrite this code in C instead of Scala, keeping it the same logically? |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| #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 Scala to C#, same semantics. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| 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}"));
}
}
}
}
|
Preserve the algorithm and functionality while converting the code from Scala to C#. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| 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 Scala code in C++. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| #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 Scala snippet without changing its computational steps. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| #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;
}
|
Change the following Scala code into Java without altering its purpose. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| 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)));
}
}
|
Port the following code from Scala to Java with equivalent syntax and logic. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| 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 Scala code snippet into Python without altering its behavior. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| 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 Scala snippet. |
fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
val size = endInclusive - start + 1
require (size > 0) { "Array slice cannot be empty" }
val m = start + size / 2
return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}
fun fivenum(x: DoubleArray): DoubleArray {
require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
val result = DoubleArray(5)
x.sort()
result[0] = x[0]
result[2] = median(x, 0, x.size - 1)
result[4] = x[x.lastIndex]
val m = x.size / 2
var lowerEnd = if (x.size % 2 == 1) m else m - 1
result[1] = median(x, 0, lowerEnd)
result[3] = median(x, m, x.size - 1)
return result
}
fun main(args: Array<String>) {
var xl = listOf(
doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
doubleArrayOf(
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
)
)
xl.forEach { println("${fivenum(it).asList()}\n") }
}
| 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)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.