Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Change the following PowerShell code into C# without altering its purpose. | function Get-StandardDeviation {
begin {
$avg = 0
$nums = @()
}
process {
$nums += $_
$avg = ($nums | Measure-Object -Average).Average
$sum = 0;
$nums | ForEach-Object { $sum += ($avg - $_) * ($avg - $_) }
[Math]::Sqrt($sum / $nums.Length)
}
}
| using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Rewrite the snippet below in C++ so it works the same as the original PowerShell code. | function Get-StandardDeviation {
begin {
$avg = 0
$nums = @()
}
process {
$nums += $_
$avg = ($nums | Measure-Object -Average).Average
$sum = 0;
$nums | ForEach-Object { $sum += ($avg - $_) * ($avg - $_) }
[Math]::Sqrt($sum / $nums.Length)
}
}
| #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Can you help me rewrite this code in Java instead of PowerShell, keeping it the same logically? | function Get-StandardDeviation {
begin {
$avg = 0
$nums = @()
}
process {
$nums += $_
$avg = ($nums | Measure-Object -Average).Average
$sum = 0;
$nums | ForEach-Object { $sum += ($avg - $_) * ($avg - $_) }
[Math]::Sqrt($sum / $nums.Length)
}
}
| public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Preserve the algorithm and functionality while converting the code from PowerShell to Python. | function Get-StandardDeviation {
begin {
$avg = 0
$nums = @()
}
process {
$nums += $_
$avg = ($nums | Measure-Object -Average).Average
$sum = 0;
$nums | ForEach-Object { $sum += ($avg - $_) * ($avg - $_) }
[Math]::Sqrt($sum / $nums.Length)
}
}
| >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Produce a language-to-language conversion: from PowerShell to VB, same semantics. | function Get-StandardDeviation {
begin {
$avg = 0
$nums = @()
}
process {
$nums += $_
$avg = ($nums | Measure-Object -Average).Average
$sum = 0;
$nums | ForEach-Object { $sum += ($avg - $_) * ($avg - $_) }
[Math]::Sqrt($sum / $nums.Length)
}
}
| data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Produce a functionally identical Go code for the snippet given in PowerShell. | function Get-StandardDeviation {
begin {
$avg = 0
$nums = @()
}
process {
$nums += $_
$avg = ($nums | Measure-Object -Average).Average
$sum = 0;
$nums | ForEach-Object { $sum += ($avg - $_) * ($avg - $_) }
[Math]::Sqrt($sum / $nums.Length)
}
}
| package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Generate an equivalent C version of this Racket code. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... |
Write the same algorithm in C# as shown in this Racket implementation. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Ensure the translated C++ code behaves exactly like the original Racket snippet. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Transform the following Racket implementation into Java, maintaining the same output and logic. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Ensure the translated Python code behaves exactly like the original Racket snippet. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Keep all operations the same but rewrite the snippet in VB. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Please provide an equivalent version of this Racket code in Go. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Generate an equivalent C version of this COBOL code. | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... |
Can you help me rewrite this code in C# instead of COBOL, keeping it the same logically? | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Write a version of this COBOL function in C++ with identical behavior. | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Rewrite the snippet below in Java so it works the same as the original COBOL code. | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Can you help me rewrite this code in Python instead of COBOL, keeping it the same logically? | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Convert the following code from COBOL to VB, ensuring the logic remains intact. | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Write the same code in Go as shown below in COBOL. | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Change the programming language of this snippet from REXX to C without modifying what it does. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... |
Translate this program into C# but keep the logic exactly as in REXX. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Please provide an equivalent version of this REXX code in C++. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Please provide an equivalent version of this REXX code in Java. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Rewrite the snippet below in Python so it works the same as the original REXX code. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Port the provided REXX code into VB while preserving the original functionality. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Port the provided REXX code into Go while preserving the original functionality. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Write the same algorithm in C as shown in this Ruby implementation. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... |
Convert the following code from Ruby to C#, ensuring the logic remains intact. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Preserve the algorithm and functionality while converting the code from Ruby to C++. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Change the following Ruby code into Java without altering its purpose. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Preserve the algorithm and functionality while converting the code from Ruby to Python. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Convert this Ruby block to VB, preserving its control flow and logic. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Write the same algorithm in Go as shown in this Ruby implementation. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Port the provided Scala code into C while preserving the original functionality. |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... |
Translate this program into C# but keep the logic exactly as in Scala. |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Translate this program into C++ but keep the logic exactly as in Scala. |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Can you help me rewrite this code in Java instead of Scala, keeping it the same logically? |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Produce a language-to-language conversion: from Scala to Python, same semantics. |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Convert the following code from Scala to VB, ensuring the logic remains intact. |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Transform the following Scala implementation into Go, maintaining the same output and logic. |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Maintain the same structure and functionality when rewriting this code in C. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... |
Translate the given Swift code snippet into C# without altering its behavior. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Translate this program into C++ but keep the logic exactly as in Swift. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Write a version of this Swift function in Java with identical behavior. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Transform the following Swift implementation into Python, maintaining the same output and logic. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Ensure the translated VB code behaves exactly like the original Swift snippet. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Rewrite the snippet below in Go so it works the same as the original Swift code. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Preserve the algorithm and functionality while converting the code from Tcl to C. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... |
Port the provided Tcl code into C# while preserving the original functionality. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... |
Generate an equivalent C++ version of this Tcl code. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... |
Convert the following code from Tcl to Java, ensuring the logic remains intact. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... |
Transform the following Tcl implementation into Python, maintaining the same output and logic. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Generate an equivalent VB version of this Tcl code. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Produce a language-to-language conversion: from Tcl to Go, same semantics. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... |
Ensure the translated PHP code behaves exactly like the original Rust snippet. | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Convert this Ada block to PHP, preserving its control flow and logic. | with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; ... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Convert the following code from Arturo to PHP, ensuring the logic remains intact. | arr: new []
loop [2 4 4 4 5 5 7 9] 'value [
'arr ++ value
print [value "->" deviation arr]
]
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Produce a functionally identical PHP code for the snippet given in AutoHotKey. | Data := [2,4,4,4,5,5,7,9]
for k, v in Data {
FileAppend, % "#" a_index " value = " v " stddev = " stddev(v) "`n", *
}
return
stddev(x) {
static n, sum, sum2
n++
sum += x
sum2 += x*x
return sqrt((sum2/n) - (((sum*sum)/n)/n))
}
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Please provide an equivalent version of this AWK code in PHP. |
BEGIN {
n = split("2,4,4,4,5,5,7,9",arr,",")
for (i=1; i<=n; i++) {
temp[i] = arr[i]
printf("%g %g\n",arr[i],stdev(temp))
}
exit(0)
}
function stdev(arr, i,n,s1,s2,variance,x) {
for (i in arr) {
n++
x = arr[i]
s1 += x ^ 2
s2 += x
}
variance = ((n * s1) -... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Write the same code in PHP as shown below in BBC_Basic. | MAXITEMS = 100
FOR i% = 1 TO 8
READ n
PRINT "Value = "; n ", running SD = " FNrunningsd(n)
NEXT
END
DATA 2,4,4,4,5,5,7,9
DEF FNrunningsd(n)
PRIVATE list(), i%
DIM list(MAXITEMS)
i% += 1
list(i%) = n
= SQR(MOD(list())^2/i% - ... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Port the provided Common_Lisp code into PHP while preserving the original functionality. | (defn stateful-std-deviation[x]
(letfn [(std-dev[x]
(let [v (deref (find-var (symbol (str *ns* "/v"))))]
(swap! v conj x)
(let [m (/ (reduce + @v) (count @v))]
(Math/sqrt (/ (reduce + (map #(* (- m %) (- m %)) @v)) (count @v))))))]
(when (nil? (resolve 'v))
... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Change the following D code into PHP without altering its purpose. | import std.stdio, std.math;
struct StdDev {
real sum = 0.0, sqSum = 0.0;
long nvalues;
void addNumber(in real input) pure nothrow {
nvalues++;
sum += input;
sqSum += input ^^ 2;
}
real getStdDev() const pure nothrow {
if (nvalues == 0)
return 0.0;
... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Please provide an equivalent version of this Delphi code in PHP. | program prj_CalcStdDerv;
uses
Math;
var Series:Array of Extended;
UserString:String;
function AppendAndCalc(NewVal:Extended):Extended;
begin
setlength(Series,high(Series)+2);
Series[high(Series)] := NewVal;
result := PopnStdDev(Series);
end;
const data:array[0..7] of Extended =
(2,4,4,4,5,5,7,9);
... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Convert the following code from Elixir to PHP, ensuring the logic remains intact. | defmodule Standard_deviation do
def add_sample( pid, n ), do: send( pid, {:add, n} )
def create, do: spawn_link( fn -> loop( [] ) end )
def destroy( pid ), do: send( pid, :stop )
def get( pid ) do
send( pid, {:get, self()} )
receive do
{ :get, value, _pid } -> value
end
end
def... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Port the following code from Erlang to PHP with equivalent syntax and logic. | -module( standard_deviation ).
-export( [add_sample/2, create/0, destroy/1, get/1, task/0] ).
-compile({no_auto_import,[get/1]}).
add_sample( Pid, N ) -> Pid ! {add, N}.
create() -> erlang:spawn_link( fun() -> loop( [] ) end ).
destroy( Pid ) -> Pid ! stop.
get( Pid ) ->
Pid ! {get, erlang:self()},
receive
{ge... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Change the following Factor code into PHP without altering its purpose. | USING: accessors io kernel math math.functions math.parser
sequences ;
IN: standard-deviator
TUPLE: standard-deviator sum sum^2 n ;
: <standard-deviator> ( -- standard-deviator )
0.0 0.0 0 standard-deviator boa ;
: current-std ( standard-deviator -- std )
[ [ sum^2>> ] [ n>> ] bi / ]
[ [ sum>> ] [ n>> ] ... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Convert the following code from Forth to PHP, ensuring the logic remains intact. | : f+! dup f@ f+ f! ;
: st-count f@ ;
: st-sum float+ f@ ;
: st-sumsq 2 floats + f@ ;
: st-mean
dup st-sum st-count f/ ;
: st-variance
dup st-sumsq
dup st-mean fdup f* dup st-count f* f-
st-count f/ ;
: st-stddev
st-variance fsqrt ;
: st-add
dup
1e dup f+! float+... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Translate this program into PHP but keep the logic exactly as in Fortran. | program standard_deviation
implicit none
integer(kind=4), parameter :: dp = kind(0.0d0)
real(kind=dp), dimension(:), allocatable :: vals
integer(kind=4) :: i
real(kind=dp), dimension(8) :: sample_data = (/ 2, 4, 4, 4, 5, 5, 7, 9 /)
do i = lbound(sample_data, 1), ubound(sample_data, 1)
call sample_add... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Produce a functionally identical PHP code for the snippet given in Groovy. | List samples = []
def stdDev = { def sample ->
samples << sample
def sum = samples.sum()
def sumSq = samples.sum { it * it }
def count = samples.size()
(sumSq/count - (sum/count)**2)**0.5
}
[2,4,4,4,5,5,7,9].each {
println "${stdDev(it)}"
}
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Maintain the same structure and functionality when rewriting this code in PHP. |
import Data.List (foldl')
import Data.STRef
import Control.Monad.ST
data Pair a b = Pair !a !b
sumLen :: [Double] -> Pair Double Double
sumLen = fiof2 . foldl' (\(Pair s l) x -> Pair (s+x) (l+1)) (Pair 0.0 0)
where fiof2 (Pair s l) = Pair s (fromIntegral l)
divl :: Pair Double Double -> Double
divl (Pair _ 0.0... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Port the provided Icon code into PHP while preserving the original functionality. | procedure main()
stddev()
every s := stddev(![2,4,4,4,5,5,7,9]) do
write("stddev (so far) := ",s)
end
procedure stddev(x)
static X,sumX,sum2X
if /x then {
X := []
sumX := sum2X := 0.
}
else {
put(X,x)
sumX +:= x
sum2X +:= x^2
return sqrt( (sum2X / *... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Write a version of this J function in PHP with identical behavior. | mean=: +/ % #
dev=: - mean
stddevP=: [: %:@mean *:@dev
stddevP=: [: mean&.:*: dev
stddevP=: %:@(mean@:*: - *:@mean)
stddevP\ 2 4 4 4 5 5 7 9
0 1 0.942809 0.866025 0.979796 1 1.39971 2
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Generate an equivalent PHP version of this Julia code. | function makerunningstd(::Type{T} = Float64) where T
∑x = ∑x² = zero(T)
n = 0
function runningstd(x)
∑x += x
∑x² += x ^ 2
n += 1
s = ∑x² / n - (∑x / n) ^ 2
return s
end
return runningstd
end
test = Float64[2, 4, 4, 4, 5, 5, 7, 9]
rstd = makerunningstd()
... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Port the provided Lua code into PHP while preserving the original functionality. | function stdev()
local sum, sumsq, k = 0,0,0
return function(n)
sum, sumsq, k = sum + n, sumsq + n^2, k+1
return math.sqrt((sumsq / k) - (sum/k)^2)
end
end
ldev = stdev()
for i, v in ipairs{2,4,4,4,5,5,7,9} do
print(ldev(v))
end
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Produce a functionally identical PHP code for the snippet given in Mathematica. | runningSTDDev[n_] := (If[Not[ValueQ[$Data]], $Data = {}];StandardDeviation[AppendTo[$Data, n]])
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Keep all operations the same but rewrite the snippet in PHP. | x = [2,4,4,4,5,5,7,9];
n = length (x);
m = mean (x);
x2 = mean (x .* x);
dev= sqrt (x2 - m * m)
dev = 2
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Change the following Nim code into PHP without altering its purpose. | import math, strutils
var sdSum, sdSum2, sdN = 0.0
proc sd(x: float): float =
sdN += 1
sdSum += x
sdSum2 += x * x
sqrt(sdSum2 / sdN - sdSum * sdSum / (sdN * sdN))
for value in [float 2,4,4,4,5,5,7,9]:
echo value, " ", formatFloat(sd(value), precision = -1)
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Rewrite the snippet below in PHP so it works the same as the original OCaml code. | let sqr x = x *. x
let stddev l =
let n, sx, sx2 =
List.fold_left
(fun (n, sx, sx2) x -> succ n, sx +. x, sx2 +. sqr x)
(0, 0., 0.) l
in
sqrt ((sx2 -. sqr sx /. float n) /. float n)
let _ =
let l = [ 2.;4.;4.;4.;5.;5.;7.;9. ] in
Printf.printf "List: ";
List.iter (Printf.printf "%g ") l;
... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Maintain the same structure and functionality when rewriting this code in PHP. | program stddev;
uses math;
const
n=8;
var
arr: array[1..n] of real =(2,4,4,4,5,5,7,9);
function stddev(n: integer): real;
var
i: integer;
s1,s2,variance,x: real;
begin
for i:=1 to n do
begin
x:=arr[i];
s1:=s1+power(x,2);
s2:=s2+x
end;
variance:=((n*s1)-(power(s2,2)))/(power(n... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Generate a PHP translation of this Perl snippet without changing its computational steps. | {
package SDAccum;
sub new {
my $class = shift;
my $self = {};
$self->{sum} = 0.0;
$self->{sum2} = 0.0;
$self->{num} = 0;
bless $self, $class;
return $self;
}
sub count {
my $self = shift;
return $self->{num};
}
sub mean {
my $self = shift;
return ($self->{num}>0) ? $self->{sum}/$sel... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Write the same code in PHP as shown below in PowerShell. | function Get-StandardDeviation {
begin {
$avg = 0
$nums = @()
}
process {
$nums += $_
$avg = ($nums | Measure-Object -Average).Average
$sum = 0;
$nums | ForEach-Object { $sum += ($avg - $_) * ($avg - $_) }
[Math]::Sqrt($sum / $nums.Length)
}
}
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Convert this Racket block to PHP, preserving its control flow and logic. | #lang racket
(require math)
(define running-stddev
(let ([ns '()])
(λ(n) (set! ns (cons n ns)) (stddev ns))))
(last (map running-stddev '(2 4 4 4 5 5 7 9)))
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Port the provided COBOL code into PHP while preserving the original functionality. | IDENTIFICATION DIVISION.
PROGRAM-ID. run-stddev.
environment division.
input-output section.
file-control.
select input-file assign to "input.txt"
organization is line sequential.
data division.
file section.
fd input-file.
01 inp-record.
03 inp-fld pic 9(03).
working-storage section.
01 filler pic 9(01... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Keep all operations the same but rewrite the snippet in PHP. | sdacc = .SDAccum~new
x = .array~of(2,4,4,4,5,5,7,9)
sd = 0
do i = 1 to x~size
sd = sdacc~value(x[i])
Say '#'i 'value =' x[i] 'stdev =' sd
end
::class SDAccum
::method sum attribute
::method sum2 attribute
::method count attribute
::method init
self~sum = 0.0
self~sum2 = 0.0
self~count = 0
::method value
... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Translate this program into PHP but keep the logic exactly as in Ruby. | class StdDevAccumulator
def initialize
@n, @sum, @sum2 = 0, 0.0, 0.0
end
def <<(num)
@n += 1
@sum += num
@sum2 += num**2
Math.sqrt (@sum2 * @n - @sum**2) / @n**2
end
end
sd = StdDevAccumulator.new
i = 0
[2,4,4,4,5,5,7,9].each { |n| puts "adding
| <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Produce a language-to-language conversion: from Scala to PHP, same semantics. |
class CumStdDev {
private var n = 0
private var sum = 0.0
private var sum2 = 0.0
fun sd(x: Double): Double {
n++
sum += x
sum2 += x * x
return Math.sqrt(sum2 / n - sum * sum / n / n)
}
}
fun main(args: Array<String>) {
val testData = doubleArrayOf(2.0, 4.0, 4.... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Transform the following Swift implementation into PHP, maintaining the same output and logic. | import Darwin
class stdDev{
var n:Double = 0.0
var sum:Double = 0.0
var sum2:Double = 0.0
init(){
let testData:[Double] = [2,4,4,4,5,5,7,9];
for x in testData{
var a:Double = calcSd(x)
println("value \(Int(x)) SD = \(a)");
}... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Convert this Tcl snippet to PHP and keep its semantics consistent. | oo::class create SDAccum {
variable sum sum2 num
constructor {} {
set sum 0.0
set sum2 0.0
set num 0
}
method value {x} {
set sum2 [expr {$sum2 + $x**2}]
set sum [expr {$sum + $x}]
incr num
return [my stddev]
}
method count {} {
ret... | <?php
class sdcalc {
private $cnt, $sumup, $square;
function __construct() {
$this->reset();
}
# callable on an instance
function reset() {
$this->cnt=0; $this->sumup=0; $this->square=0;
}
function add($f) {
$this->cnt++;
$this->sumup += $f;
$this->sq... |
Port the provided C code into Rust while preserving the original functionality. | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;
typedef struct stat_obj_struct {
double sum, sum2;
size_t num;
Action action;
} sStatObject, *StatObject;
StatObject NewStatObject( Action action )
{
StatObject so;
so = malloc(sizeof(sSta... | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... |
Can you help me rewrite this code in Rust instead of C++, keeping it the same logically? | #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>
template<int N> struct MomentsAccumulator_
{
std::vector<double> m_;
MomentsAccumulator_() : m_(N + 1, 0.0) {}
void operator()(double v)
{
double inc = 1.0;
for (auto& mi : m_)
{
mi += inc;
inc *= v;
}
}
};
double Stdev(cons... | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... |
Rewrite this program in Rust while keeping its functionality equivalent to the C# version. | using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
... | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... |
Rewrite the snippet below in Rust so it works the same as the original Java code. | public class StdDev {
int n = 0;
double sum = 0;
double sum2 = 0;
public double sd(double x) {
n++;
sum += x;
sum2 += x*x;
return Math.sqrt(sum2/n - sum*sum/n/n);
}
public static void main(String[] args) {
double[] testData = {2,4,4,4,5,5,7,9};
StdDev sd = new StdDev();
... | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... |
Translate the given Go code snippet into Rust without altering its behavior. | package main
import (
"fmt"
"math"
)
func newRsdv() func(float64) float64 {
var n, a, q float64
return func(x float64) float64 {
n++
a1 := a+(x-a)/n
q, a = q+(x-a)*(x-a1), a1
return math.Sqrt(q/n)
}
}
func main() {
r := newRsdv()
for _, x := range []float6... | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... |
Change the programming language of this snippet from Rust to Python without modifying what it does. | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... | >>> from math import sqrt
>>> def sd(x):
sd.sum += x
sd.sum2 += x*x
sd.n += 1.0
sum, sum2, n = sd.sum, sd.sum2, sd.n
return sqrt(sum2/n - sum*sum/n/n)
>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
print (value, sd(value))
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258... |
Write the same algorithm in VB as shown in this Rust implementation. | pub struct CumulativeStandardDeviation {
n: f64,
sum: f64,
sum_sq: f64
}
impl CumulativeStandardDeviation {
pub fn new() -> Self {
CumulativeStandardDeviation {
n: 0.,
sum: 0.,
sum_sq: 0.
}
}
fn push(&mut self, x: f64) -> f64 {
self.n... | data = Array(2,4,4,4,5,5,7,9)
For i = 0 To UBound(data)
WScript.StdOut.Write "value = " & data(i) &_
" running sd = " & sd(data,i)
WScript.StdOut.WriteLine
Next
Function sd(arr,n)
mean = 0
variance = 0
For j = 0 To n
mean = mean + arr(j)
Next
mean = mean/(n+1)
For k = 0 To n
variance = variance + ((arr(... |
Generate a C# translation of this Ada snippet without changing its computational steps. | function Palindrome (Text : String) return Boolean is
begin
for Offset in 0..Text'Length / 2 - 1 loop
if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
return False;
end if;
end loop;
return True;
end Palindrome;
| using System;
class Program
{
static string Reverse(string value)
{
char[] chars = value.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
static bool IsPalindrome(string value)
{
return value == Reverse(value);
}
static void Main(string[] ar... |
Transform the following Ada implementation into C, maintaining the same output and logic. | function Palindrome (Text : String) return Boolean is
begin
for Offset in 0..Text'Length / 2 - 1 loop
if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
return False;
end if;
end loop;
return True;
end Palindrome;
| #include <string.h>
int palindrome(const char *s)
{
int i,l;
l = strlen(s);
for(i=0; i<l/2; i++)
{
if ( s[i] != s[l-i-1] ) return 0;
}
return 1;
}
|
Write a version of this Ada function in C++ with identical behavior. | function Palindrome (Text : String) return Boolean is
begin
for Offset in 0..Text'Length / 2 - 1 loop
if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
return False;
end if;
end loop;
return True;
end Palindrome;
| #include <string>
#include <algorithm>
bool is_palindrome(std::string const& s)
{
return std::equal(s.begin(), s.end(), s.rbegin());
}
|
Produce a language-to-language conversion: from Ada to Go, same semantics. | function Palindrome (Text : String) return Boolean is
begin
for Offset in 0..Text'Length / 2 - 1 loop
if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
return False;
end if;
end loop;
return True;
end Palindrome;
|
var str;
str = argument0
str = string_lettersdigits(string_lower(string_replace(str,' ','')));
var inv;
inv = '';
var i;
for (i = 0; i < string_length(str); i += 1;)
{
inv += string_copy(str,string_length(str)-i,1);
}
return (str == inv);
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.