Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Rewrite this program in Rust while keeping its functionality equivalent to the C# version.
using System; namespace CheckTerminal { class Program { static void Main(string[] args) { Console.WriteLine("Stdout is tty: {0}", Console.IsOutputRedirected); } } }
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
Port the provided C# code into Rust while preserving the original functionality.
using System; namespace CheckTerminal { class Program { static void Main(string[] args) { Console.WriteLine("Stdout is tty: {0}", Console.IsOutputRedirected); } } }
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
Keep all operations the same but rewrite the snippet in Rust.
package main import ( "os" "fmt" ) func main() { if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 { fmt.Println("Hello terminal") } else { fmt.Println("Who are you? You're not a terminal") } }
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
Change the following Go code into Rust without altering its purpose.
package main import ( "os" "fmt" ) func main() { if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 { fmt.Println("Hello terminal") } else { fmt.Println("Who are you? You're not a terminal") } }
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
Produce a functionally identical Python code for the snippet given in Rust.
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
from sys import stdout if stdout.isatty(): print 'The output device is a teletype. Or something like a teletype.' else: print 'The output device isn\'t like a teletype.'
Port the following code from Rust to VB with equivalent syntax and logic.
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
Module Module1 Sub Main() Console.WriteLine("Stdout is tty: {0}", Console.IsOutputRedirected) End Sub End Module
Generate a Python translation of this Rust snippet without changing its computational steps.
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
from sys import stdout if stdout.isatty(): print 'The output device is a teletype. Or something like a teletype.' else: print 'The output device isn\'t like a teletype.'
Write the same algorithm in VB as shown in this Rust implementation.
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
Module Module1 Sub Main() Console.WriteLine("Stdout is tty: {0}", Console.IsOutputRedirected) End Sub End Module
Write the same code in Rust as shown below in C.
#include <unistd.h> #include <stdio.h> int main() { puts(isatty(fileno(stdout)) ? "stdout is tty" : "stdout is not tty"); return 0; }
f = File.open("test.txt") p f.isatty # => false p STDOUT.isatty # => true
Maintain the same structure and functionality when rewriting this code in C#.
with Ada.Text_IO, Ada.Float_Text_IO; procedure FindMedian is f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 ); min_idx: integer; min_val, median_val, swap: float; begin for i in f'range loop min_idx := i; min_val := f(i); for j in i+1 .. f'last...
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 Ada snippet to C and keep its semantics consistent.
with Ada.Text_IO, Ada.Float_Text_IO; procedure FindMedian is f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 ); min_idx: integer; min_val, median_val, swap: float; begin for i in f'range loop min_idx := i; min_val := f(i); for j in i+1 .. f'last...
#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...
Convert this Ada block to C++, preserving its control flow and logic.
with Ada.Text_IO, Ada.Float_Text_IO; procedure FindMedian is f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 ); min_idx: integer; min_val, median_val, swap: float; begin for i in f'range loop min_idx := i; min_val := f(i); for j in i+1 .. f'last...
#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 Ada to Go.
with Ada.Text_IO, Ada.Float_Text_IO; procedure FindMedian is f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 ); min_idx: integer; min_val, median_val, swap: float; begin for i in f'range loop min_idx := i; min_val := f(i); for j in i+1 .. f'last...
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 ...
Please provide an equivalent version of this Ada code in Java.
with Ada.Text_IO, Ada.Float_Text_IO; procedure FindMedian is f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 ); min_idx: integer; min_val, median_val, swap: float; begin for i in f'range loop min_idx := i; min_val := f(i); for j in i+1 .. f'last...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Ensure the translated Python code behaves exactly like the original Ada snippet.
with Ada.Text_IO, Ada.Float_Text_IO; procedure FindMedian is f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 ); min_idx: integer; min_val, median_val, swap: float; begin for i in f'range loop min_idx := i; min_val := f(i); for j in i+1 .. f'last...
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 Ada function in VB with identical behavior.
with Ada.Text_IO, Ada.Float_Text_IO; procedure FindMedian is f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 ); min_idx: integer; min_val, median_val, swap: float; begin for i in f'range loop min_idx := i; min_val := f(i); for j in i+1 .. f'last...
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 the following code from Arturo to C, ensuring the logic remains intact.
arr: [1 2 3 4 5 6 7] arr2: [1 2 3 4 5 6] print median arr print median arr2
#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...
Convert this Arturo block to C#, preserving its control flow and logic.
arr: [1 2 3 4 5 6 7] arr2: [1 2 3 4 5 6] print median arr print median arr2
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 Arturo snippet without changing its computational steps.
arr: [1 2 3 4 5 6 7] arr2: [1 2 3 4 5 6] print median arr print median arr2
#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 Arturo code into Java without altering its purpose.
arr: [1 2 3 4 5 6 7] arr2: [1 2 3 4 5 6] print median arr print median arr2
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Convert the following code from Arturo to Python, ensuring the logic remains intact.
arr: [1 2 3 4 5 6 7] arr2: [1 2 3 4 5 6] print median arr print median arr2
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)
Transform the following Arturo implementation into VB, maintaining the same output and logic.
arr: [1 2 3 4 5 6 7] arr2: [1 2 3 4 5 6] print median arr print median arr2
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 an equivalent Go version of this Arturo code.
arr: [1 2 3 4 5 6 7] arr2: [1 2 3 4 5 6] print median arr print median arr2
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 ...
Keep all operations the same but rewrite the snippet in C.
seq = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2, 5 MsgBox % median(seq, "`,")   median(seq, delimiter) { Sort, seq, ND%delimiter% StringSplit, seq, seq, % delimiter median := Floor(seq0 / 2) Return seq%median% }
#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...
Can you help me rewrite this code in C# instead of AutoHotKey, keeping it the same logically?
seq = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2, 5 MsgBox % median(seq, "`,")   median(seq, delimiter) { Sort, seq, ND%delimiter% StringSplit, seq, seq, % delimiter median := Floor(seq0 / 2) Return seq%median% }
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 programming language of this snippet from AutoHotKey to C++ without modifying what it does.
seq = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2, 5 MsgBox % median(seq, "`,")   median(seq, delimiter) { Sort, seq, ND%delimiter% StringSplit, seq, seq, % delimiter median := Floor(seq0 / 2) Return seq%median% }
#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 AutoHotKey.
seq = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2, 5 MsgBox % median(seq, "`,")   median(seq, delimiter) { Sort, seq, ND%delimiter% StringSplit, seq, seq, % delimiter median := Floor(seq0 / 2) Return seq%median% }
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 AutoHotKey implementation into Python, maintaining the same output and logic.
seq = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2, 5 MsgBox % median(seq, "`,")   median(seq, delimiter) { Sort, seq, ND%delimiter% StringSplit, seq, seq, % delimiter median := Floor(seq0 / 2) Return seq%median% }
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 the given AutoHotKey code snippet into VB without altering its behavior.
seq = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2, 5 MsgBox % median(seq, "`,")   median(seq, delimiter) { Sort, seq, ND%delimiter% StringSplit, seq, seq, % delimiter median := Floor(seq0 / 2) Return seq%median% }
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_...
Can you help me rewrite this code in Go instead of AutoHotKey, keeping it the same logically?
seq = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2, 5 MsgBox % median(seq, "`,")   median(seq, delimiter) { Sort, seq, ND%delimiter% StringSplit, seq, seq, % delimiter median := Floor(seq0 / 2) Return seq%median% }
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 ...
Write the same algorithm in C as shown in this AWK implementation.
BEGIN { d[1] = 3.0 d[2] = 4.0 d[3] = 1.0 d[4] = -8.4 d[5] = 7.2 d[6] = 4.0 d[7] = 1.0 d[8] = 1.2 showD("Before: ") gnomeSortD() showD("Sorted: ") printf "Median: %f\n", medianD() exit } function medianD( len, mid) { len = length(d) mid = int(len/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...
Maintain the same structure and functionality when rewriting this code in C#.
BEGIN { d[1] = 3.0 d[2] = 4.0 d[3] = 1.0 d[4] = -8.4 d[5] = 7.2 d[6] = 4.0 d[7] = 1.0 d[8] = 1.2 showD("Before: ") gnomeSortD() showD("Sorted: ") printf "Median: %f\n", medianD() exit } function medianD( len, mid) { len = length(d) mid = int(len/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; ...
Change the following AWK code into C++ without altering its purpose.
BEGIN { d[1] = 3.0 d[2] = 4.0 d[3] = 1.0 d[4] = -8.4 d[5] = 7.2 d[6] = 4.0 d[7] = 1.0 d[8] = 1.2 showD("Before: ") gnomeSortD() showD("Sorted: ") printf "Median: %f\n", medianD() exit } function medianD( len, mid) { len = length(d) mid = int(len/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...
Ensure the translated Java code behaves exactly like the original AWK snippet.
BEGIN { d[1] = 3.0 d[2] = 4.0 d[3] = 1.0 d[4] = -8.4 d[5] = 7.2 d[6] = 4.0 d[7] = 1.0 d[8] = 1.2 showD("Before: ") gnomeSortD() showD("Sorted: ") printf "Median: %f\n", medianD() exit } function medianD( len, mid) { len = length(d) mid = int(len/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; }
Convert this AWK snippet to Python and keep its semantics consistent.
BEGIN { d[1] = 3.0 d[2] = 4.0 d[3] = 1.0 d[4] = -8.4 d[5] = 7.2 d[6] = 4.0 d[7] = 1.0 d[8] = 1.2 showD("Before: ") gnomeSortD() showD("Sorted: ") printf "Median: %f\n", medianD() exit } function medianD( len, mid) { len = length(d) mid = int(len/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)
Port the following code from AWK to VB with equivalent syntax and logic.
BEGIN { d[1] = 3.0 d[2] = 4.0 d[3] = 1.0 d[4] = -8.4 d[5] = 7.2 d[6] = 4.0 d[7] = 1.0 d[8] = 1.2 showD("Before: ") gnomeSortD() showD("Sorted: ") printf "Median: %f\n", medianD() exit } function medianD( len, mid) { len = length(d) mid = int(len/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_...
Port the following code from AWK to Go with equivalent syntax and logic.
BEGIN { d[1] = 3.0 d[2] = 4.0 d[3] = 1.0 d[4] = -8.4 d[5] = 7.2 d[6] = 4.0 d[7] = 1.0 d[8] = 1.2 showD("Before: ") gnomeSortD() showD("Sorted: ") printf "Median: %f\n", medianD() exit } function medianD( len, mid) { len = length(d) mid = int(len/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 ...
Generate a C translation of this BBC_Basic snippet without changing its computational steps.
INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) DIM a(6), b(5) 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 PRINT "Median of a() is " ; FNmedian(a()) PRINT "Median of b() is " ; FNmedian(b()) END DEF FNmedian(a()) LOCAL C% ...
#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...
Translate this program into C# but keep the logic exactly as in BBC_Basic.
INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) DIM a(6), b(5) 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 PRINT "Median of a() is " ; FNmedian(a()) PRINT "Median of b() is " ; FNmedian(b()) END DEF FNmedian(a()) LOCAL C% ...
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 BBC_Basic block to C++, preserving its control flow and logic.
INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) DIM a(6), b(5) 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 PRINT "Median of a() is " ; FNmedian(a()) PRINT "Median of b() is " ; FNmedian(b()) END DEF FNmedian(a()) LOCAL C% ...
#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 BBC_Basic version.
INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) DIM a(6), b(5) 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 PRINT "Median of a() is " ; FNmedian(a()) PRINT "Median of b() is " ; FNmedian(b()) END DEF FNmedian(a()) LOCAL C% ...
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 BBC_Basic snippet to Python and keep its semantics consistent.
INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) DIM a(6), b(5) 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 PRINT "Median of a() is " ; FNmedian(a()) PRINT "Median of b() is " ; FNmedian(b()) END DEF FNmedian(a()) LOCAL C% ...
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 BBC_Basic to VB without modifying what it does.
INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) DIM a(6), b(5) 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 PRINT "Median of a() is " ; FNmedian(a()) PRINT "Median of b() is " ; FNmedian(b()) END DEF FNmedian(a()) LOCAL C% ...
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_...
Preserve the algorithm and functionality while converting the code from BBC_Basic to Go.
INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) DIM a(6), b(5) 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 PRINT "Median of a() is " ; FNmedian(a()) PRINT "Median of b() is " ; FNmedian(b()) END DEF FNmedian(a()) LOCAL C% ...
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 an equivalent C version of this Common_Lisp code.
(defn median [ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 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...
Translate this program into C# but keep the logic exactly as in Common_Lisp.
(defn median [ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 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 Common_Lisp implementation into C++, maintaining the same output and logic.
(defn median [ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 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...
Translate this program into Java but keep the logic exactly as in Common_Lisp.
(defn median [ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 2))))
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 Common_Lisp code in Python.
(defn median [ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 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)
Transform the following Common_Lisp implementation into VB, maintaining the same output and logic.
(defn median [ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 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_...
Generate a Go translation of this Common_Lisp snippet without changing its computational steps.
(defn median [ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 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 ...
Change the following D code into C without altering its purpose.
import std.stdio, std.algorithm; T median(T)(T[] nums) pure nothrow { nums.sort(); if (nums.length & 1) return nums[$ / 2]; else return (nums[$ / 2 - 1] + nums[$ / 2]) / 2.0; } void main() { auto a1 = [5.1, 2.6, 6.2, 8.8, 4.6, 4.1]; writeln("Even median: ", a1.median); auto a2...
#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...
Convert this D block to C#, preserving its control flow and logic.
import std.stdio, std.algorithm; T median(T)(T[] nums) pure nothrow { nums.sort(); if (nums.length & 1) return nums[$ / 2]; else return (nums[$ / 2 - 1] + nums[$ / 2]) / 2.0; } void main() { auto a1 = [5.1, 2.6, 6.2, 8.8, 4.6, 4.1]; writeln("Even median: ", a1.median); auto a2...
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; ...
Write the same algorithm in C++ as shown in this D implementation.
import std.stdio, std.algorithm; T median(T)(T[] nums) pure nothrow { nums.sort(); if (nums.length & 1) return nums[$ / 2]; else return (nums[$ / 2 - 1] + nums[$ / 2]) / 2.0; } void main() { auto a1 = [5.1, 2.6, 6.2, 8.8, 4.6, 4.1]; writeln("Even median: ", a1.median); auto a2...
#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 D snippet to Java and keep its semantics consistent.
import std.stdio, std.algorithm; T median(T)(T[] nums) pure nothrow { nums.sort(); if (nums.length & 1) return nums[$ / 2]; else return (nums[$ / 2 - 1] + nums[$ / 2]) / 2.0; } void main() { auto a1 = [5.1, 2.6, 6.2, 8.8, 4.6, 4.1]; writeln("Even median: ", a1.median); auto a2...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Can you help me rewrite this code in Python instead of D, keeping it the same logically?
import std.stdio, std.algorithm; T median(T)(T[] nums) pure nothrow { nums.sort(); if (nums.length & 1) return nums[$ / 2]; else return (nums[$ / 2 - 1] + nums[$ / 2]) / 2.0; } void main() { auto a1 = [5.1, 2.6, 6.2, 8.8, 4.6, 4.1]; writeln("Even median: ", a1.median); auto a2...
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 functionally identical VB code for the snippet given in D.
import std.stdio, std.algorithm; T median(T)(T[] nums) pure nothrow { nums.sort(); if (nums.length & 1) return nums[$ / 2]; else return (nums[$ / 2 - 1] + nums[$ / 2]) / 2.0; } void main() { auto a1 = [5.1, 2.6, 6.2, 8.8, 4.6, 4.1]; writeln("Even median: ", a1.median); auto a2...
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_...
Ensure the translated Go code behaves exactly like the original D snippet.
import std.stdio, std.algorithm; T median(T)(T[] nums) pure nothrow { nums.sort(); if (nums.length & 1) return nums[$ / 2]; else return (nums[$ / 2 - 1] + nums[$ / 2]) / 2.0; } void main() { auto a1 = [5.1, 2.6, 6.2, 8.8, 4.6, 4.1]; writeln("Even median: ", a1.median); auto a2...
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 ...
Write the same code in C as shown below in Delphi.
program AveragesMedian; uses Generics.Collections, Types; function Median(aArray: TDoubleDynArray): Double; var lMiddleIndex: Integer; begin TArray.Sort<Double>(aArray); lMiddleIndex := Length(aArray) div 2; if Odd(Length(aArray)) then Result := aArray[lMiddleIndex] else Result := (aArray[lMiddle...
#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...
Change the following Delphi code into C# without altering its purpose.
program AveragesMedian; uses Generics.Collections, Types; function Median(aArray: TDoubleDynArray): Double; var lMiddleIndex: Integer; begin TArray.Sort<Double>(aArray); lMiddleIndex := Length(aArray) div 2; if Odd(Length(aArray)) then Result := aArray[lMiddleIndex] else Result := (aArray[lMiddle...
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; ...
Rewrite the snippet below in C++ so it works the same as the original Delphi code.
program AveragesMedian; uses Generics.Collections, Types; function Median(aArray: TDoubleDynArray): Double; var lMiddleIndex: Integer; begin TArray.Sort<Double>(aArray); lMiddleIndex := Length(aArray) div 2; if Odd(Length(aArray)) then Result := aArray[lMiddleIndex] else Result := (aArray[lMiddle...
#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 Delphi code.
program AveragesMedian; uses Generics.Collections, Types; function Median(aArray: TDoubleDynArray): Double; var lMiddleIndex: Integer; begin TArray.Sort<Double>(aArray); lMiddleIndex := Length(aArray) div 2; if Odd(Length(aArray)) then Result := aArray[lMiddleIndex] else Result := (aArray[lMiddle...
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 Delphi implementation into Python, maintaining the same output and logic.
program AveragesMedian; uses Generics.Collections, Types; function Median(aArray: TDoubleDynArray): Double; var lMiddleIndex: Integer; begin TArray.Sort<Double>(aArray); lMiddleIndex := Length(aArray) div 2; if Odd(Length(aArray)) then Result := aArray[lMiddleIndex] else Result := (aArray[lMiddle...
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)
Maintain the same structure and functionality when rewriting this code in VB.
program AveragesMedian; uses Generics.Collections, Types; function Median(aArray: TDoubleDynArray): Double; var lMiddleIndex: Integer; begin TArray.Sort<Double>(aArray); lMiddleIndex := Length(aArray) div 2; if Odd(Length(aArray)) then Result := aArray[lMiddleIndex] else Result := (aArray[lMiddle...
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_...
Preserve the algorithm and functionality while converting the code from Delphi to Go.
program AveragesMedian; uses Generics.Collections, Types; function Median(aArray: TDoubleDynArray): Double; var lMiddleIndex: Integer; begin TArray.Sort<Double>(aArray); lMiddleIndex := Length(aArray) div 2; if Odd(Length(aArray)) then Result := aArray[lMiddleIndex] else Result := (aArray[lMiddle...
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 Elixir code into C without altering its purpose.
defmodule Average do def median([]), do: nil def median(list) do len = length(list) sorted = Enum.sort(list) mid = div(len, 2) if rem(len,2) == 0, do: (Enum.at(sorted, mid-1) + Enum.at(sorted, mid)) / 2, else: Enum.at(sorted, mid) end end median = fn list -> IO.puts " medi...
#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...
Convert this Elixir block to C#, preserving its control flow and logic.
defmodule Average do def median([]), do: nil def median(list) do len = length(list) sorted = Enum.sort(list) mid = div(len, 2) if rem(len,2) == 0, do: (Enum.at(sorted, mid-1) + Enum.at(sorted, mid)) / 2, else: Enum.at(sorted, mid) end end median = fn list -> IO.puts " medi...
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; ...
Rewrite the snippet below in C++ so it works the same as the original Elixir code.
defmodule Average do def median([]), do: nil def median(list) do len = length(list) sorted = Enum.sort(list) mid = div(len, 2) if rem(len,2) == 0, do: (Enum.at(sorted, mid-1) + Enum.at(sorted, mid)) / 2, else: Enum.at(sorted, mid) end end median = fn list -> IO.puts " medi...
#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 Elixir, keeping it the same logically?
defmodule Average do def median([]), do: nil def median(list) do len = length(list) sorted = Enum.sort(list) mid = div(len, 2) if rem(len,2) == 0, do: (Enum.at(sorted, mid-1) + Enum.at(sorted, mid)) / 2, else: Enum.at(sorted, mid) end end median = fn list -> IO.puts " medi...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Generate a Python translation of this Elixir snippet without changing its computational steps.
defmodule Average do def median([]), do: nil def median(list) do len = length(list) sorted = Enum.sort(list) mid = div(len, 2) if rem(len,2) == 0, do: (Enum.at(sorted, mid-1) + Enum.at(sorted, mid)) / 2, else: Enum.at(sorted, mid) end end median = fn list -> IO.puts " medi...
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 functionally identical VB code for the snippet given in Elixir.
defmodule Average do def median([]), do: nil def median(list) do len = length(list) sorted = Enum.sort(list) mid = div(len, 2) if rem(len,2) == 0, do: (Enum.at(sorted, mid-1) + Enum.at(sorted, mid)) / 2, else: Enum.at(sorted, mid) end end median = fn list -> IO.puts " medi...
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_...
Preserve the algorithm and functionality while converting the code from Elixir to Go.
defmodule Average do def median([]), do: nil def median(list) do len = length(list) sorted = Enum.sort(list) mid = div(len, 2) if rem(len,2) == 0, do: (Enum.at(sorted, mid-1) + Enum.at(sorted, mid)) / 2, else: Enum.at(sorted, mid) end end median = fn list -> IO.puts " medi...
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 ...
Write the same code in C as shown below in Erlang.
-module(median). -import(lists, [nth/2, sort/1]). -compile(export_all). test(MaxInt,ListSize,TimesToRun) -> test(MaxInt,ListSize,TimesToRun,[[],[]]). test(_,_,0,[GMAcc, OMAcc]) -> Len = length(GMAcc), {GMT,GMV} = lists:foldl(fun({T, V}, {AT,AV}) -> {AT + T, AV + V} end, {0,0}, GMAcc), {OMT,OMV} = lis...
#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...
Ensure the translated C# code behaves exactly like the original Erlang snippet.
-module(median). -import(lists, [nth/2, sort/1]). -compile(export_all). test(MaxInt,ListSize,TimesToRun) -> test(MaxInt,ListSize,TimesToRun,[[],[]]). test(_,_,0,[GMAcc, OMAcc]) -> Len = length(GMAcc), {GMT,GMV} = lists:foldl(fun({T, V}, {AT,AV}) -> {AT + T, AV + V} end, {0,0}, GMAcc), {OMT,OMV} = lis...
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 Erlang to C++.
-module(median). -import(lists, [nth/2, sort/1]). -compile(export_all). test(MaxInt,ListSize,TimesToRun) -> test(MaxInt,ListSize,TimesToRun,[[],[]]). test(_,_,0,[GMAcc, OMAcc]) -> Len = length(GMAcc), {GMT,GMV} = lists:foldl(fun({T, V}, {AT,AV}) -> {AT + T, AV + V} end, {0,0}, GMAcc), {OMT,OMV} = lis...
#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 Erlang code into Java without altering its purpose.
-module(median). -import(lists, [nth/2, sort/1]). -compile(export_all). test(MaxInt,ListSize,TimesToRun) -> test(MaxInt,ListSize,TimesToRun,[[],[]]). test(_,_,0,[GMAcc, OMAcc]) -> Len = length(GMAcc), {GMT,GMV} = lists:foldl(fun({T, V}, {AT,AV}) -> {AT + T, AV + V} end, {0,0}, GMAcc), {OMT,OMV} = lis...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Generate a Python translation of this Erlang snippet without changing its computational steps.
-module(median). -import(lists, [nth/2, sort/1]). -compile(export_all). test(MaxInt,ListSize,TimesToRun) -> test(MaxInt,ListSize,TimesToRun,[[],[]]). test(_,_,0,[GMAcc, OMAcc]) -> Len = length(GMAcc), {GMT,GMV} = lists:foldl(fun({T, V}, {AT,AV}) -> {AT + T, AV + V} end, {0,0}, GMAcc), {OMT,OMV} = lis...
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 Erlang version.
-module(median). -import(lists, [nth/2, sort/1]). -compile(export_all). test(MaxInt,ListSize,TimesToRun) -> test(MaxInt,ListSize,TimesToRun,[[],[]]). test(_,_,0,[GMAcc, OMAcc]) -> Len = length(GMAcc), {GMT,GMV} = lists:foldl(fun({T, V}, {AT,AV}) -> {AT + T, AV + V} end, {0,0}, GMAcc), {OMT,OMV} = lis...
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 the snippet below in Go so it works the same as the original Erlang code.
-module(median). -import(lists, [nth/2, sort/1]). -compile(export_all). test(MaxInt,ListSize,TimesToRun) -> test(MaxInt,ListSize,TimesToRun,[[],[]]). test(_,_,0,[GMAcc, OMAcc]) -> Len = length(GMAcc), {GMT,GMV} = lists:foldl(fun({T, V}, {AT,AV}) -> {AT + T, AV + V} end, {0,0}, GMAcc), {OMT,OMV} = lis...
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 F# code into C while preserving the original functionality.
let rec splitToFives list = match list with | a::b::c::d::e::tail -> ([a;b;c;d;e])::(splitToFives tail) | [] -> [] | _ -> let left = 5 - List.length (list) let last = List.append list (List.init left (fun _ -> System.Double.PositiveInfinity) ) ...
#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 this program in C# while keeping its functionality equivalent to the F# version.
let rec splitToFives list = match list with | a::b::c::d::e::tail -> ([a;b;c;d;e])::(splitToFives tail) | [] -> [] | _ -> let left = 5 - List.length (list) let last = List.append list (List.init left (fun _ -> System.Double.PositiveInfinity) ) ...
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; ...
Write a version of this F# function in C++ with identical behavior.
let rec splitToFives list = match list with | a::b::c::d::e::tail -> ([a;b;c;d;e])::(splitToFives tail) | [] -> [] | _ -> let left = 5 - List.length (list) let last = List.append list (List.init left (fun _ -> System.Double.PositiveInfinity) ) ...
#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 a version of this F# function in Java with identical behavior.
let rec splitToFives list = match list with | a::b::c::d::e::tail -> ([a;b;c;d;e])::(splitToFives tail) | [] -> [] | _ -> let left = 5 - List.length (list) let last = List.append list (List.init left (fun _ -> System.Double.PositiveInfinity) ) ...
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 F# code into Python without altering its purpose.
let rec splitToFives list = match list with | a::b::c::d::e::tail -> ([a;b;c;d;e])::(splitToFives tail) | [] -> [] | _ -> let left = 5 - List.length (list) let last = List.append list (List.init left (fun _ -> System.Double.PositiveInfinity) ) ...
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)
Can you help me rewrite this code in VB instead of F#, keeping it the same logically?
let rec splitToFives list = match list with | a::b::c::d::e::tail -> ([a;b;c;d;e])::(splitToFives tail) | [] -> [] | _ -> let left = 5 - List.length (list) let last = List.append list (List.init left (fun _ -> System.Double.PositiveInfinity) ) ...
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 F# snippet without changing its computational steps.
let rec splitToFives list = match list with | a::b::c::d::e::tail -> ([a;b;c;d;e])::(splitToFives tail) | [] -> [] | _ -> let left = 5 - List.length (list) let last = List.append list (List.init left (fun _ -> System.Double.PositiveInfinity) ) ...
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 Factor to C, same semantics.
USING: arrays kernel locals math math.functions random sequences ; IN: median : pivot ( seq -- pivot ) random ; : split ( seq pivot -- {lt,eq,gt} ) [ [ < ] curry partition ] keep [ = ] curry partition 3array ; DEFER: nth-in-order :: nth-in-order-recur ( seq ind -- elt ) seq dup pivot split dup [ length ] m...
#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...
Convert the following code from Factor to C#, ensuring the logic remains intact.
USING: arrays kernel locals math math.functions random sequences ; IN: median : pivot ( seq -- pivot ) random ; : split ( seq pivot -- {lt,eq,gt} ) [ [ < ] curry partition ] keep [ = ] curry partition 3array ; DEFER: nth-in-order :: nth-in-order-recur ( seq ind -- elt ) seq dup pivot split dup [ length ] m...
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 Factor code into C++ without altering its purpose.
USING: arrays kernel locals math math.functions random sequences ; IN: median : pivot ( seq -- pivot ) random ; : split ( seq pivot -- {lt,eq,gt} ) [ [ < ] curry partition ] keep [ = ] curry partition 3array ; DEFER: nth-in-order :: nth-in-order-recur ( seq ind -- elt ) seq dup pivot split dup [ length ] m...
#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 Factor.
USING: arrays kernel locals math math.functions random sequences ; IN: median : pivot ( seq -- pivot ) random ; : split ( seq pivot -- {lt,eq,gt} ) [ [ < ] curry partition ] keep [ = ] curry partition 3array ; DEFER: nth-in-order :: nth-in-order-recur ( seq ind -- elt ) seq dup pivot split dup [ length ] m...
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 Factor code.
USING: arrays kernel locals math math.functions random sequences ; IN: median : pivot ( seq -- pivot ) random ; : split ( seq pivot -- {lt,eq,gt} ) [ [ < ] curry partition ] keep [ = ] curry partition 3array ; DEFER: nth-in-order :: nth-in-order-recur ( seq ind -- elt ) seq dup pivot split dup [ length ] m...
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 Factor version.
USING: arrays kernel locals math math.functions random sequences ; IN: median : pivot ( seq -- pivot ) random ; : split ( seq pivot -- {lt,eq,gt} ) [ [ < ] curry partition ] keep [ = ] curry partition 3array ; DEFER: nth-in-order :: nth-in-order-recur ( seq ind -- elt ) seq dup pivot split dup [ length ] m...
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 Factor to Go without modifying what it does.
USING: arrays kernel locals math math.functions random sequences ; IN: median : pivot ( seq -- pivot ) random ; : split ( seq pivot -- {lt,eq,gt} ) [ [ < ] curry partition ] keep [ = ] curry partition 3array ; DEFER: nth-in-order :: nth-in-order-recur ( seq ind -- elt ) seq dup pivot split dup [ length ] m...
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 an equivalent C version of this Forth code.
-1 cells constant -cell : cell- -cell + ; defer lessthan ' < is lessthan : mid over - 2/ -cell and + ; : exch dup @ >r over @ swap ! r> swap ! ; : part 2dup mid @ >r 2dup begin swap begin dup @ r@ lessthan while cell+ repeat swap begin r@ over @ lessthan while cell- repeat 2dup <= if 2dup ...
#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...
Convert this Forth snippet to C# and keep its semantics consistent.
-1 cells constant -cell : cell- -cell + ; defer lessthan ' < is lessthan : mid over - 2/ -cell and + ; : exch dup @ >r over @ swap ! r> swap ! ; : part 2dup mid @ >r 2dup begin swap begin dup @ r@ lessthan while cell+ repeat swap begin r@ over @ lessthan while cell- repeat 2dup <= if 2dup ...
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 Forth snippet without changing its computational steps.
-1 cells constant -cell : cell- -cell + ; defer lessthan ' < is lessthan : mid over - 2/ -cell and + ; : exch dup @ >r over @ swap ! r> swap ! ; : part 2dup mid @ >r 2dup begin swap begin dup @ r@ lessthan while cell+ repeat swap begin r@ over @ lessthan while cell- repeat 2dup <= if 2dup ...
#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...
Maintain the same structure and functionality when rewriting this code in Java.
-1 cells constant -cell : cell- -cell + ; defer lessthan ' < is lessthan : mid over - 2/ -cell and + ; : exch dup @ >r over @ swap ! r> swap ! ; : part 2dup mid @ >r 2dup begin swap begin dup @ r@ lessthan while cell+ repeat swap begin r@ over @ lessthan while cell- repeat 2dup <= if 2dup ...
public static double median(List<Double> list) { Collections.sort(list); return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2; }
Port the following code from Forth to Python with equivalent syntax and logic.
-1 cells constant -cell : cell- -cell + ; defer lessthan ' < is lessthan : mid over - 2/ -cell and + ; : exch dup @ >r over @ swap ! r> swap ! ; : part 2dup mid @ >r 2dup begin swap begin dup @ r@ lessthan while cell+ repeat swap begin r@ over @ lessthan while cell- repeat 2dup <= if 2dup ...
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 Forth function in VB with identical behavior.
-1 cells constant -cell : cell- -cell + ; defer lessthan ' < is lessthan : mid over - 2/ -cell and + ; : exch dup @ >r over @ swap ! r> swap ! ; : part 2dup mid @ >r 2dup begin swap begin dup @ r@ lessthan while cell+ repeat swap begin r@ over @ lessthan while cell- repeat 2dup <= if 2dup ...
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 Forth code snippet into Go without altering its behavior.
-1 cells constant -cell : cell- -cell + ; defer lessthan ' < is lessthan : mid over - 2/ -cell and + ; : exch dup @ >r over @ swap ! r> swap ! ; : part 2dup mid @ >r 2dup begin swap begin dup @ r@ lessthan while cell+ repeat swap begin r@ over @ lessthan while cell- repeat 2dup <= if 2dup ...
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 ...