Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Can you help me rewrite this code in C# instead of Fortran, keeping it the same logically?
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Change the following Fortran code into C++ without altering its purpose.
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Rewrite the snippet below in C so it works the same as the original Fortran code.
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Produce a language-to-language conversion: from Fortran to Go, same semantics.
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Change the following Fortran code into Java without altering its purpose.
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Keep all operations the same but rewrite the snippet in Python.
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Port the following code from Fortran to VB with equivalent syntax and logic.
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Translate this program into PHP but keep the logic exactly as in Fortran.
program Median_Test real :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), & b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /) print *, median(a) print *, median(b) contains function median(a, found) real, dimension(:), intent(in) :: a logical, optional,...
function median($arr) { sort($arr); $count = count($arr); //count the number of values in array $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value if ($count % 2) { // odd number, middle is the median $median = $arr[$middleval]; } else { // even number, ca...
Convert this Groovy snippet to C and keep its semantics consistent.
def median(Iterable col) { def s = col as SortedSet if (s == null) return null if (s.empty) return 0 def n = s.size() def m = n.intdiv(2) def l = s.collect { it } n%2 == 1 ? l[m] : (l[m] + l[m-1])/2 }
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Write the same code in C# as shown below in Groovy.
def median(Iterable col) { def s = col as SortedSet if (s == null) return null if (s.empty) return 0 def n = s.size() def m = n.intdiv(2) def l = s.collect { it } n%2 == 1 ? l[m] : (l[m] + l[m-1])/2 }
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Transform the following Groovy implementation into C++, maintaining the same output and logic.
def median(Iterable col) { def s = col as SortedSet if (s == null) return null if (s.empty) return 0 def n = s.size() def m = n.intdiv(2) def l = s.collect { it } n%2 == 1 ? l[m] : (l[m] + l[m-1])/2 }
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Generate an equivalent Java version of this Groovy code.
def median(Iterable col) { def s = col as SortedSet if (s == null) return null if (s.empty) return 0 def n = s.size() def m = n.intdiv(2) def l = s.collect { it } n%2 == 1 ? l[m] : (l[m] + l[m-1])/2 }
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Rewrite the snippet below in Python so it works the same as the original Groovy code.
def median(Iterable col) { def s = col as SortedSet if (s == null) return null if (s.empty) return 0 def n = s.size() def m = n.intdiv(2) def l = s.collect { it } n%2 == 1 ? l[m] : (l[m] + l[m-1])/2 }
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Convert this Groovy snippet to VB and keep its semantics consistent.
def median(Iterable col) { def s = col as SortedSet if (s == null) return null if (s.empty) return 0 def n = s.size() def m = n.intdiv(2) def l = s.collect { it } n%2 == 1 ? l[m] : (l[m] + l[m-1])/2 }
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Translate the given Groovy code snippet into Go without altering its behavior.
def median(Iterable col) { def s = col as SortedSet if (s == null) return null if (s.empty) return 0 def n = s.size() def m = n.intdiv(2) def l = s.collect { it } n%2 == 1 ? l[m] : (l[m] + l[m-1])/2 }
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Convert the following code from Haskell to C, ensuring the logic remains intact.
import Data.List (partition) nth :: Ord t => [t] -> Int -> t nth (x:xs) n | k == n = x | k > n = nth ys n | otherwise = nth zs $ n - k - 1 where (ys, zs) = partition (< x) xs k = length ys medianMay :: (Fractional a, Ord a) => [a] -> Maybe a medianMay xs | n < 1 = Nothing | even n = Just ((nth xs ...
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Generate a C# translation of this Haskell snippet without changing its computational steps.
import Data.List (partition) nth :: Ord t => [t] -> Int -> t nth (x:xs) n | k == n = x | k > n = nth ys n | otherwise = nth zs $ n - k - 1 where (ys, zs) = partition (< x) xs k = length ys medianMay :: (Fractional a, Ord a) => [a] -> Maybe a medianMay xs | n < 1 = Nothing | even n = Just ((nth xs ...
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Convert the following code from Haskell to C++, ensuring the logic remains intact.
import Data.List (partition) nth :: Ord t => [t] -> Int -> t nth (x:xs) n | k == n = x | k > n = nth ys n | otherwise = nth zs $ n - k - 1 where (ys, zs) = partition (< x) xs k = length ys medianMay :: (Fractional a, Ord a) => [a] -> Maybe a medianMay xs | n < 1 = Nothing | even n = Just ((nth xs ...
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Preserve the algorithm and functionality while converting the code from Haskell to Java.
import Data.List (partition) nth :: Ord t => [t] -> Int -> t nth (x:xs) n | k == n = x | k > n = nth ys n | otherwise = nth zs $ n - k - 1 where (ys, zs) = partition (< x) xs k = length ys medianMay :: (Fractional a, Ord a) => [a] -> Maybe a medianMay xs | n < 1 = Nothing | even n = Just ((nth xs ...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Keep all operations the same but rewrite the snippet in Python.
import Data.List (partition) nth :: Ord t => [t] -> Int -> t nth (x:xs) n | k == n = x | k > n = nth ys n | otherwise = nth zs $ n - k - 1 where (ys, zs) = partition (< x) xs k = length ys medianMay :: (Fractional a, Ord a) => [a] -> Maybe a medianMay xs | n < 1 = Nothing | even n = Just ((nth xs ...
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Produce a language-to-language conversion: from Haskell to VB, same semantics.
import Data.List (partition) nth :: Ord t => [t] -> Int -> t nth (x:xs) n | k == n = x | k > n = nth ys n | otherwise = nth zs $ n - k - 1 where (ys, zs) = partition (< x) xs k = length ys medianMay :: (Fractional a, Ord a) => [a] -> Maybe a medianMay xs | n < 1 = Nothing | even n = Just ((nth xs ...
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Generate a Go translation of this Haskell snippet without changing its computational steps.
import Data.List (partition) nth :: Ord t => [t] -> Int -> t nth (x:xs) n | k == n = x | k > n = nth ys n | otherwise = nth zs $ n - k - 1 where (ys, zs) = partition (< x) xs k = length ys medianMay :: (Fractional a, Ord a) => [a] -> Maybe a medianMay xs | n < 1 = Nothing | even n = Just ((nth xs ...
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Maintain the same structure and functionality when rewriting this code in C.
require 'stats/base' median 1 9 2 4 3
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Write the same code in C# as shown below in J.
require 'stats/base' median 1 9 2 4 3
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Preserve the algorithm and functionality while converting the code from J to C++.
require 'stats/base' median 1 9 2 4 3
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Write the same code in Java as shown below in J.
require 'stats/base' median 1 9 2 4 3
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Please provide an equivalent version of this J code in Python.
require 'stats/base' median 1 9 2 4 3
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Change the following J code into VB without altering its purpose.
require 'stats/base' median 1 9 2 4 3
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Write a version of this J function in Go with identical behavior.
require 'stats/base' median 1 9 2 4 3
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Ensure the translated C code behaves exactly like the original Julia snippet.
using Statistics function median2(n) s = sort(n) len = length(n) if len % 2 == 0 return (s[floor(Int, len / 2) + 1] + s[floor(Int, len / 2)]) / 2 else return s[floor(Int, len / 2) + 1] end end a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2] @show a b median2(a) median(a) median2(b...
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Keep all operations the same but rewrite the snippet in C#.
using Statistics function median2(n) s = sort(n) len = length(n) if len % 2 == 0 return (s[floor(Int, len / 2) + 1] + s[floor(Int, len / 2)]) / 2 else return s[floor(Int, len / 2) + 1] end end a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2] @show a b median2(a) median(a) median2(b...
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Keep all operations the same but rewrite the snippet in C++.
using Statistics function median2(n) s = sort(n) len = length(n) if len % 2 == 0 return (s[floor(Int, len / 2) + 1] + s[floor(Int, len / 2)]) / 2 else return s[floor(Int, len / 2) + 1] end end a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2] @show a b median2(a) median(a) median2(b...
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Rewrite the snippet below in Java so it works the same as the original Julia code.
using Statistics function median2(n) s = sort(n) len = length(n) if len % 2 == 0 return (s[floor(Int, len / 2) + 1] + s[floor(Int, len / 2)]) / 2 else return s[floor(Int, len / 2) + 1] end end a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2] @show a b median2(a) median(a) median2(b...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Write the same code in Python as shown below in Julia.
using Statistics function median2(n) s = sort(n) len = length(n) if len % 2 == 0 return (s[floor(Int, len / 2) + 1] + s[floor(Int, len / 2)]) / 2 else return s[floor(Int, len / 2) + 1] end end a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2] @show a b median2(a) median(a) median2(b...
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Change the programming language of this snippet from Julia to VB without modifying what it does.
using Statistics function median2(n) s = sort(n) len = length(n) if len % 2 == 0 return (s[floor(Int, len / 2) + 1] + s[floor(Int, len / 2)]) / 2 else return s[floor(Int, len / 2) + 1] end end a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2] @show a b median2(a) median(a) median2(b...
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Rewrite this program in Go while keeping its functionality equivalent to the Julia version.
using Statistics function median2(n) s = sort(n) len = length(n) if len % 2 == 0 return (s[floor(Int, len / 2) + 1] + s[floor(Int, len / 2)]) / 2 else return s[floor(Int, len / 2) + 1] end end a = [4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] b = [4.1, 7.2, 1.7, 9.3, 4.4, 3.2] @show a b median2(a) median(a) median2(b...
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Port the provided Lua code into C while preserving the original functionality.
function median (numlist) if type(numlist) ~= 'table' then return numlist end table.sort(numlist) if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end return numlist[math.ceil(#numlist/2)] end print(median({4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2})) print(median({4.1, 7.2, 1....
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Port the provided Lua code into C# while preserving the original functionality.
function median (numlist) if type(numlist) ~= 'table' then return numlist end table.sort(numlist) if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end return numlist[math.ceil(#numlist/2)] end print(median({4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2})) print(median({4.1, 7.2, 1....
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Convert this Lua block to C++, preserving its control flow and logic.
function median (numlist) if type(numlist) ~= 'table' then return numlist end table.sort(numlist) if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end return numlist[math.ceil(#numlist/2)] end print(median({4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2})) print(median({4.1, 7.2, 1....
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Produce a functionally identical Java code for the snippet given in Lua.
function median (numlist) if type(numlist) ~= 'table' then return numlist end table.sort(numlist) if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end return numlist[math.ceil(#numlist/2)] end print(median({4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2})) print(median({4.1, 7.2, 1....
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Change the following Lua code into Python without altering its purpose.
function median (numlist) if type(numlist) ~= 'table' then return numlist end table.sort(numlist) if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end return numlist[math.ceil(#numlist/2)] end print(median({4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2})) print(median({4.1, 7.2, 1....
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Write a version of this Lua function in VB with identical behavior.
function median (numlist) if type(numlist) ~= 'table' then return numlist end table.sort(numlist) if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end return numlist[math.ceil(#numlist/2)] end print(median({4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2})) print(median({4.1, 7.2, 1....
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Write the same code in Go as shown below in Lua.
function median (numlist) if type(numlist) ~= 'table' then return numlist end table.sort(numlist) if #numlist %2 == 0 then return (numlist[#numlist/2] + numlist[#numlist/2+1]) / 2 end return numlist[math.ceil(#numlist/2)] end print(median({4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2})) print(median({4.1, 7.2, 1....
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Preserve the algorithm and functionality while converting the code from Mathematica to C.
Median[{1, 5, 3, 2, 4}] Median[{1, 5, 3, 6, 4, 2}]
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Port the provided Mathematica code into C# while preserving the original functionality.
Median[{1, 5, 3, 2, 4}] Median[{1, 5, 3, 6, 4, 2}]
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Change the following Mathematica code into C++ without altering its purpose.
Median[{1, 5, 3, 2, 4}] Median[{1, 5, 3, 6, 4, 2}]
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Convert this Mathematica snippet to Java and keep its semantics consistent.
Median[{1, 5, 3, 2, 4}] Median[{1, 5, 3, 6, 4, 2}]
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Produce a functionally identical Python code for the snippet given in Mathematica.
Median[{1, 5, 3, 2, 4}] Median[{1, 5, 3, 6, 4, 2}]
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Preserve the algorithm and functionality while converting the code from Mathematica to VB.
Median[{1, 5, 3, 2, 4}] Median[{1, 5, 3, 6, 4, 2}]
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Translate the given Mathematica code snippet into Go without altering its behavior.
Median[{1, 5, 3, 2, 4}] Median[{1, 5, 3, 6, 4, 2}]
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Maintain the same structure and functionality when rewriting this code in C.
function medianValue = findmedian(setOfValues) medianValue = median(setOfValues); end
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Rewrite the snippet below in C# so it works the same as the original MATLAB code.
function medianValue = findmedian(setOfValues) medianValue = median(setOfValues); end
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Convert this MATLAB block to C++, preserving its control flow and logic.
function medianValue = findmedian(setOfValues) medianValue = median(setOfValues); end
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Port the following code from MATLAB to Java with equivalent syntax and logic.
function medianValue = findmedian(setOfValues) medianValue = median(setOfValues); end
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Translate this program into Python but keep the logic exactly as in MATLAB.
function medianValue = findmedian(setOfValues) medianValue = median(setOfValues); end
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Rewrite this program in VB while keeping its functionality equivalent to the MATLAB version.
function medianValue = findmedian(setOfValues) medianValue = median(setOfValues); end
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Please provide an equivalent version of this MATLAB code in Go.
function medianValue = findmedian(setOfValues) medianValue = median(setOfValues); end
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Maintain the same structure and functionality when rewriting this code in C.
import algorithm, strutils proc median(xs: seq[float]): float = var ys = xs sort(ys, system.cmp[float]) 0.5 * (ys[ys.high div 2] + ys[ys.len div 2]) var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precision = 0) a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precisio...
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Keep all operations the same but rewrite the snippet in C#.
import algorithm, strutils proc median(xs: seq[float]): float = var ys = xs sort(ys, system.cmp[float]) 0.5 * (ys[ys.high div 2] + ys[ys.len div 2]) var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precision = 0) a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precisio...
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Produce a language-to-language conversion: from Nim to C++, same semantics.
import algorithm, strutils proc median(xs: seq[float]): float = var ys = xs sort(ys, system.cmp[float]) 0.5 * (ys[ys.high div 2] + ys[ys.len div 2]) var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precision = 0) a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precisio...
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Can you help me rewrite this code in Java instead of Nim, keeping it the same logically?
import algorithm, strutils proc median(xs: seq[float]): float = var ys = xs sort(ys, system.cmp[float]) 0.5 * (ys[ys.high div 2] + ys[ys.len div 2]) var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precision = 0) a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precisio...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Produce a language-to-language conversion: from Nim to Python, same semantics.
import algorithm, strutils proc median(xs: seq[float]): float = var ys = xs sort(ys, system.cmp[float]) 0.5 * (ys[ys.high div 2] + ys[ys.len div 2]) var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precision = 0) a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precisio...
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Port the following code from Nim to VB with equivalent syntax and logic.
import algorithm, strutils proc median(xs: seq[float]): float = var ys = xs sort(ys, system.cmp[float]) 0.5 * (ys[ys.high div 2] + ys[ys.len div 2]) var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precision = 0) a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precisio...
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Maintain the same structure and functionality when rewriting this code in Go.
import algorithm, strutils proc median(xs: seq[float]): float = var ys = xs sort(ys, system.cmp[float]) 0.5 * (ys[ys.high div 2] + ys[ys.len div 2]) var a = @[4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precision = 0) a = @[4.1, 7.2, 1.7, 9.3, 4.4, 3.2] echo formatFloat(median(a), precisio...
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Transform the following OCaml implementation into C, maintaining the same output and logic.
let median array = let len = Array.length array in Array.sort compare array; (array.((len-1)/2) +. array.(len/2)) /. 2.0;; let a = [|4.1; 5.6; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;; let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;;
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Produce a language-to-language conversion: from OCaml to C#, same semantics.
let median array = let len = Array.length array in Array.sort compare array; (array.((len-1)/2) +. array.(len/2)) /. 2.0;; let a = [|4.1; 5.6; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;; let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;;
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Ensure the translated C++ code behaves exactly like the original OCaml snippet.
let median array = let len = Array.length array in Array.sort compare array; (array.((len-1)/2) +. array.(len/2)) /. 2.0;; let a = [|4.1; 5.6; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;; let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;;
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Rewrite this program in Java while keeping its functionality equivalent to the OCaml version.
let median array = let len = Array.length array in Array.sort compare array; (array.((len-1)/2) +. array.(len/2)) /. 2.0;; let a = [|4.1; 5.6; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;; let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;;
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Transform the following OCaml implementation into Python, maintaining the same output and logic.
let median array = let len = Array.length array in Array.sort compare array; (array.((len-1)/2) +. array.(len/2)) /. 2.0;; let a = [|4.1; 5.6; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;; let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;;
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Generate an equivalent VB version of this OCaml code.
let median array = let len = Array.length array in Array.sort compare array; (array.((len-1)/2) +. array.(len/2)) /. 2.0;; let a = [|4.1; 5.6; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;; let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;;
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Write a version of this OCaml function in Go with identical behavior.
let median array = let len = Array.length array in Array.sort compare array; (array.((len-1)/2) +. array.(len/2)) /. 2.0;; let a = [|4.1; 5.6; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;; let a = [|4.1; 7.2; 1.7; 9.3; 4.4; 3.2|];; median a;;
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Produce a language-to-language conversion: from Pascal to C, same semantics.
Program AveragesMedian(output); type TDoubleArray = array of double; procedure bubbleSort(var list: TDoubleArray); var i, j, n: integer; t: double; begin n := length(list); for i := n downto 2 do for j := 0 to i - 1 do if list[j] > list[j + 1] then begin t := list[j]; list[j...
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Rewrite the snippet below in C# so it works the same as the original Pascal code.
Program AveragesMedian(output); type TDoubleArray = array of double; procedure bubbleSort(var list: TDoubleArray); var i, j, n: integer; t: double; begin n := length(list); for i := n downto 2 do for j := 0 to i - 1 do if list[j] > list[j + 1] then begin t := list[j]; list[j...
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Maintain the same structure and functionality when rewriting this code in C++.
Program AveragesMedian(output); type TDoubleArray = array of double; procedure bubbleSort(var list: TDoubleArray); var i, j, n: integer; t: double; begin n := length(list); for i := n downto 2 do for j := 0 to i - 1 do if list[j] > list[j + 1] then begin t := list[j]; list[j...
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Generate a Java translation of this Pascal snippet without changing its computational steps.
Program AveragesMedian(output); type TDoubleArray = array of double; procedure bubbleSort(var list: TDoubleArray); var i, j, n: integer; t: double; begin n := length(list); for i := n downto 2 do for j := 0 to i - 1 do if list[j] > list[j + 1] then begin t := list[j]; list[j...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Change the following Pascal code into Python without altering its purpose.
Program AveragesMedian(output); type TDoubleArray = array of double; procedure bubbleSort(var list: TDoubleArray); var i, j, n: integer; t: double; begin n := length(list); for i := n downto 2 do for j := 0 to i - 1 do if list[j] > list[j + 1] then begin t := list[j]; list[j...
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Write the same algorithm in VB as shown in this Pascal implementation.
Program AveragesMedian(output); type TDoubleArray = array of double; procedure bubbleSort(var list: TDoubleArray); var i, j, n: integer; t: double; begin n := length(list); for i := n downto 2 do for j := 0 to i - 1 do if list[j] > list[j + 1] then begin t := list[j]; list[j...
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Change the programming language of this snippet from Pascal to Go without modifying what it does.
Program AveragesMedian(output); type TDoubleArray = array of double; procedure bubbleSort(var list: TDoubleArray); var i, j, n: integer; t: double; begin n := length(list); for i := n downto 2 do for j := 0 to i - 1 do if list[j] > list[j + 1] then begin t := list[j]; list[j...
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Maintain the same structure and functionality when rewriting this code in C.
sub median { my @a = sort {$a <=> $b} @_; return ($a[$ }
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Write a version of this Perl function in C# with identical behavior.
sub median { my @a = sort {$a <=> $b} @_; return ($a[$ }
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Produce a functionally identical C++ code for the snippet given in Perl.
sub median { my @a = sort {$a <=> $b} @_; return ($a[$ }
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Change the following Perl code into Java without altering its purpose.
sub median { my @a = sort {$a <=> $b} @_; return ($a[$ }
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Convert this Perl snippet to Python and keep its semantics consistent.
sub median { my @a = sort {$a <=> $b} @_; return ($a[$ }
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Port the provided Perl code into VB while preserving the original functionality.
sub median { my @a = sort {$a <=> $b} @_; return ($a[$ }
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Write the same code in Go as shown below in Perl.
sub median { my @a = sort {$a <=> $b} @_; return ($a[$ }
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Can you help me rewrite this code in C instead of PowerShell, keeping it the same logically?
function Measure-Data { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, Position=0)] [double[]] $Data ) Begin { function Get-Mode ([double[]]$Data) { if ($Data.Count -gt ($Data | Sele...
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Rewrite the snippet below in C# so it works the same as the original PowerShell code.
function Measure-Data { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, Position=0)] [double[]] $Data ) Begin { function Get-Mode ([double[]]$Data) { if ($Data.Count -gt ($Data | Sele...
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Generate a C++ translation of this PowerShell snippet without changing its computational steps.
function Measure-Data { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, Position=0)] [double[]] $Data ) Begin { function Get-Mode ([double[]]$Data) { if ($Data.Count -gt ($Data | Sele...
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Rewrite the snippet below in Java so it works the same as the original PowerShell code.
function Measure-Data { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, Position=0)] [double[]] $Data ) Begin { function Get-Mode ([double[]]$Data) { if ($Data.Count -gt ($Data | Sele...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Produce a language-to-language conversion: from PowerShell to Python, same semantics.
function Measure-Data { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, Position=0)] [double[]] $Data ) Begin { function Get-Mode ([double[]]$Data) { if ($Data.Count -gt ($Data | Sele...
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Generate an equivalent VB version of this PowerShell code.
function Measure-Data { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, Position=0)] [double[]] $Data ) Begin { function Get-Mode ([double[]]$Data) { if ($Data.Count -gt ($Data | Sele...
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Convert this PowerShell snippet to Go and keep its semantics consistent.
function Measure-Data { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, Position=0)] [double[]] $Data ) Begin { function Get-Mode ([double[]]$Data) { if ($Data.Count -gt ($Data | Sele...
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Generate a C translation of this Racket snippet without changing its computational steps.
#lang racket (define (median numbers) (define sorted (list->vector (sort (vector->list numbers) <))) (define count (vector-length numbers)) (if (zero? count) #f (/ (+ (vector-ref sorted (floor (/ (sub1 count) 2))) (vector-ref sorted (floor (/ count 2)))) 2))) (median '#(5 3 4)) ...
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...
Generate an equivalent C# version of this Racket code.
#lang racket (define (median numbers) (define sorted (list->vector (sort (vector->list numbers) <))) (define count (vector-length numbers)) (if (zero? count) #f (/ (+ (vector-ref sorted (floor (/ (sub1 count) 2))) (vector-ref sorted (floor (/ count 2)))) 2))) (median '#(5 3 4)) ...
using System; using System.Linq; namespace Test { class Program { static void Main() { double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 }; myArr = myArr.OrderBy(i => i).ToArray(); int mid = myArr.Length / 2; double median; ...
Convert this Racket snippet to C++ and keep its semantics consistent.
#lang racket (define (median numbers) (define sorted (list->vector (sort (vector->list numbers) <))) (define count (vector-length numbers)) (if (zero? count) #f (/ (+ (vector-ref sorted (floor (/ (sub1 count) 2))) (vector-ref sorted (floor (/ count 2)))) 2))) (median '#(5 3 4)) ...
#include <algorithm> template <typename Iterator> double median(Iterator begin, Iterator end) { Iterator middle = begin + (end - begin) / 2; std::nth_element(begin, middle, end); if ((end - begin) % 2 != 0) { return *middle; } else { Iterator lower_middle = std::max_element(begin, midd...
Generate a Java translation of this Racket snippet without changing its computational steps.
#lang racket (define (median numbers) (define sorted (list->vector (sort (vector->list numbers) <))) (define count (vector-length numbers)) (if (zero? count) #f (/ (+ (vector-ref sorted (floor (/ (sub1 count) 2))) (vector-ref sorted (floor (/ count 2)))) 2))) (median '#(5 3 4)) ...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Convert this Racket block to Python, preserving its control flow and logic.
#lang racket (define (median numbers) (define sorted (list->vector (sort (vector->list numbers) <))) (define count (vector-length numbers)) (if (zero? count) #f (/ (+ (vector-ref sorted (floor (/ (sub1 count) 2))) (vector-ref sorted (floor (/ count 2)))) 2))) (median '#(5 3 4)) ...
def median(aray): srtd = sorted(aray) alen = len(srtd) return 0.5*( srtd[(alen-1)//2] + srtd[alen//2]) a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a) a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2) print a, median(a)
Translate this program into VB but keep the logic exactly as in Racket.
#lang racket (define (median numbers) (define sorted (list->vector (sort (vector->list numbers) <))) (define count (vector-length numbers)) (if (zero? count) #f (/ (+ (vector-ref sorted (floor (/ (sub1 count) 2))) (vector-ref sorted (floor (/ count 2)))) 2))) (median '#(5 3 4)) ...
Private Function medianq(s As Variant) As Double Dim res As Double, tmp As Integer Dim l As Integer, k As Integer res = 0 l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1) If l Then res = quick_select(s, k) If l Mod 2 = 0 Then tmp = quick_...
Port the following code from Racket to Go with equivalent syntax and logic.
#lang racket (define (median numbers) (define sorted (list->vector (sort (vector->list numbers) <))) (define count (vector-length numbers)) (if (zero? count) #f (/ (+ (vector-ref sorted (floor (/ (sub1 count) 2))) (vector-ref sorted (floor (/ count 2)))) 2))) (median '#(5 3 4)) ...
package main import ( "fmt" "sort" ) func main() { fmt.Println(median([]float64{3, 1, 4, 1})) fmt.Println(median([]float64{3, 1, 4, 1, 5})) } func median(a []float64) float64 { sort.Float64s(a) half := len(a) / 2 m := a[half] if len(a)%2 == 0 { m = (m + a[half-1]) / 2 ...
Port the provided COBOL code into C while preserving the original functionality.
FUNCTION MEDIAN(some-table (ALL))
#include <stdio.h> #include <stdlib.h> typedef struct floatList { float *list; int size; } *FloatList; int floatcmp( const void *a, const void *b) { if (*(const float *)a < *(const float *)b) return -1; else return *(const float *)a > *(const float *)b; } float median( FloatList fl ) { qsort( f...