Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Preserve the algorithm and functionality while converting the code from Mathematica to C. | Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Translate this program into C# but keep the logic exactly as in Mathematica. | Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Port the following code from Mathematica to C++ with equivalent syntax and logic. | Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Transform the following Mathematica implementation into Java, maintaining the same output and logic. | Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Convert this Mathematica snippet to Python and keep its semantics consistent. | Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Port the following code from Mathematica to VB with equivalent syntax and logic. | Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Keep all operations the same but rewrite the snippet in Go. | Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Transform the following MATLAB implementation into C, maintaining the same output and logic. | N = 20
[a,b] = meshgrid(1:N, 1:N);
c = sqrt(a.^2 + b.^2);
[x,y] = find(c == fix(c));
disp([x, y, sqrt(x.^2 + y.^2)])
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Maintain the same structure and functionality when rewriting this code in C#. | N = 20
[a,b] = meshgrid(1:N, 1:N);
c = sqrt(a.^2 + b.^2);
[x,y] = find(c == fix(c));
disp([x, y, sqrt(x.^2 + y.^2)])
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Generate a C++ translation of this MATLAB snippet without changing its computational steps. | N = 20
[a,b] = meshgrid(1:N, 1:N);
c = sqrt(a.^2 + b.^2);
[x,y] = find(c == fix(c));
disp([x, y, sqrt(x.^2 + y.^2)])
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Port the provided MATLAB code into Java while preserving the original functionality. | N = 20
[a,b] = meshgrid(1:N, 1:N);
c = sqrt(a.^2 + b.^2);
[x,y] = find(c == fix(c));
disp([x, y, sqrt(x.^2 + y.^2)])
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Translate this program into Python but keep the logic exactly as in MATLAB. | N = 20
[a,b] = meshgrid(1:N, 1:N);
c = sqrt(a.^2 + b.^2);
[x,y] = find(c == fix(c));
disp([x, y, sqrt(x.^2 + y.^2)])
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Write the same algorithm in VB as shown in this MATLAB implementation. | N = 20
[a,b] = meshgrid(1:N, 1:N);
c = sqrt(a.^2 + b.^2);
[x,y] = find(c == fix(c));
disp([x, y, sqrt(x.^2 + y.^2)])
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Can you help me rewrite this code in Go instead of MATLAB, keeping it the same logically? | N = 20
[a,b] = meshgrid(1:N, 1:N);
c = sqrt(a.^2 + b.^2);
[x,y] = find(c == fix(c));
disp([x, y, sqrt(x.^2 + y.^2)])
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Change the programming language of this snippet from Nim to C without modifying what it does. | import sugar, math
let n = 20
let triplets = collect(newSeq):
for x in 1..n:
for y in x..n:
for z in y..n:
if x^2 + y^2 == z^2:
(x,y,z)
echo triplets
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Can you help me rewrite this code in C# instead of Nim, keeping it the same logically? | import sugar, math
let n = 20
let triplets = collect(newSeq):
for x in 1..n:
for y in x..n:
for z in y..n:
if x^2 + y^2 == z^2:
(x,y,z)
echo triplets
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Preserve the algorithm and functionality while converting the code from Nim to C++. | import sugar, math
let n = 20
let triplets = collect(newSeq):
for x in 1..n:
for y in x..n:
for z in y..n:
if x^2 + y^2 == z^2:
(x,y,z)
echo triplets
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Write a version of this Nim function in Java with identical behavior. | import sugar, math
let n = 20
let triplets = collect(newSeq):
for x in 1..n:
for y in x..n:
for z in y..n:
if x^2 + y^2 == z^2:
(x,y,z)
echo triplets
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Keep all operations the same but rewrite the snippet in Python. | import sugar, math
let n = 20
let triplets = collect(newSeq):
for x in 1..n:
for y in x..n:
for z in y..n:
if x^2 + y^2 == z^2:
(x,y,z)
echo triplets
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Can you help me rewrite this code in VB instead of Nim, keeping it the same logically? | import sugar, math
let n = 20
let triplets = collect(newSeq):
for x in 1..n:
for y in x..n:
for z in y..n:
if x^2 + y^2 == z^2:
(x,y,z)
echo triplets
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Convert this Nim snippet to Go and keep its semantics consistent. | import sugar, math
let n = 20
let triplets = collect(newSeq):
for x in 1..n:
for y in x..n:
for z in y..n:
if x^2 + y^2 == z^2:
(x,y,z)
echo triplets
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Translate this program into C but keep the logic exactly as in OCaml. | let* x = enumeration in
if not condition then empty else
return expression
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Generate a C# translation of this OCaml snippet without changing its computational steps. | let* x = enumeration in
if not condition then empty else
return expression
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Transform the following OCaml implementation into C++, maintaining the same output and logic. | let* x = enumeration in
if not condition then empty else
return expression
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Generate a Java translation of this OCaml snippet without changing its computational steps. | let* x = enumeration in
if not condition then empty else
return expression
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Write the same code in Python as shown below in OCaml. | let* x = enumeration in
if not condition then empty else
return expression
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Write the same algorithm in VB as shown in this OCaml implementation. | let* x = enumeration in
if not condition then empty else
return expression
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Port the following code from OCaml to Go with equivalent syntax and logic. | let* x = enumeration in
if not condition then empty else
return expression
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Change the following Perl code into C without altering its purpose. | sub triples ($) {
my ($n) = @_;
map { my $x = $_; map { my $y = $_; map { [$x, $y, $_] } grep { $x**2 + $y**2 == $_**2 } 1..$n } 1..$n } 1..$n;
}
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Keep all operations the same but rewrite the snippet in C#. | sub triples ($) {
my ($n) = @_;
map { my $x = $_; map { my $y = $_; map { [$x, $y, $_] } grep { $x**2 + $y**2 == $_**2 } 1..$n } 1..$n } 1..$n;
}
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Convert the following code from Perl to C++, ensuring the logic remains intact. | sub triples ($) {
my ($n) = @_;
map { my $x = $_; map { my $y = $_; map { [$x, $y, $_] } grep { $x**2 + $y**2 == $_**2 } 1..$n } 1..$n } 1..$n;
}
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Port the provided Perl code into Java while preserving the original functionality. | sub triples ($) {
my ($n) = @_;
map { my $x = $_; map { my $y = $_; map { [$x, $y, $_] } grep { $x**2 + $y**2 == $_**2 } 1..$n } 1..$n } 1..$n;
}
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Please provide an equivalent version of this Perl code in Python. | sub triples ($) {
my ($n) = @_;
map { my $x = $_; map { my $y = $_; map { [$x, $y, $_] } grep { $x**2 + $y**2 == $_**2 } 1..$n } 1..$n } 1..$n;
}
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Generate a VB translation of this Perl snippet without changing its computational steps. | sub triples ($) {
my ($n) = @_;
map { my $x = $_; map { my $y = $_; map { [$x, $y, $_] } grep { $x**2 + $y**2 == $_**2 } 1..$n } 1..$n } 1..$n;
}
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Generate a Go translation of this Perl snippet without changing its computational steps. | sub triples ($) {
my ($n) = @_;
map { my $x = $_; map { my $y = $_; map { [$x, $y, $_] } grep { $x**2 + $y**2 == $_**2 } 1..$n } 1..$n } 1..$n;
}
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Translate the given R code snippet into C without altering its behavior. | x = (0:10)
> x^2
[1] 0 1 4 9 16 25 36 49 64 81 100
> Reduce(function(y,z){return (y+z)},x)
[1] 55
> x[x[(0:length(x))] %% 2==0]
[1] 0 2 4 6 8 10
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Please provide an equivalent version of this R code in C#. | x = (0:10)
> x^2
[1] 0 1 4 9 16 25 36 49 64 81 100
> Reduce(function(y,z){return (y+z)},x)
[1] 55
> x[x[(0:length(x))] %% 2==0]
[1] 0 2 4 6 8 10
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Ensure the translated C++ code behaves exactly like the original R snippet. | x = (0:10)
> x^2
[1] 0 1 4 9 16 25 36 49 64 81 100
> Reduce(function(y,z){return (y+z)},x)
[1] 55
> x[x[(0:length(x))] %% 2==0]
[1] 0 2 4 6 8 10
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Maintain the same structure and functionality when rewriting this code in Java. | x = (0:10)
> x^2
[1] 0 1 4 9 16 25 36 49 64 81 100
> Reduce(function(y,z){return (y+z)},x)
[1] 55
> x[x[(0:length(x))] %% 2==0]
[1] 0 2 4 6 8 10
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Transform the following R implementation into Python, maintaining the same output and logic. | x = (0:10)
> x^2
[1] 0 1 4 9 16 25 36 49 64 81 100
> Reduce(function(y,z){return (y+z)},x)
[1] 55
> x[x[(0:length(x))] %% 2==0]
[1] 0 2 4 6 8 10
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Generate an equivalent VB version of this R code. | x = (0:10)
> x^2
[1] 0 1 4 9 16 25 36 49 64 81 100
> Reduce(function(y,z){return (y+z)},x)
[1] 55
> x[x[(0:length(x))] %% 2==0]
[1] 0 2 4 6 8 10
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Rewrite this program in Go while keeping its functionality equivalent to the R version. | x = (0:10)
> x^2
[1] 0 1 4 9 16 25 36 49 64 81 100
> Reduce(function(y,z){return (y+z)},x)
[1] 55
> x[x[(0:length(x))] %% 2==0]
[1] 0 2 4 6 8 10
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Can you help me rewrite this code in C instead of Racket, keeping it the same logically? | #lang racket
(for*/list ([x (in-range 1 21)]
[y (in-range x 21)]
[z (in-range y 21)]
#:when (= (+ (* x x) (* y y)) (* z z)))
(list x y z))
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Translate this program into C# but keep the logic exactly as in Racket. | #lang racket
(for*/list ([x (in-range 1 21)]
[y (in-range x 21)]
[z (in-range y 21)]
#:when (= (+ (* x x) (* y y)) (* z z)))
(list x y z))
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Convert this Racket snippet to C++ and keep its semantics consistent. | #lang racket
(for*/list ([x (in-range 1 21)]
[y (in-range x 21)]
[z (in-range y 21)]
#:when (= (+ (* x x) (* y y)) (* z z)))
(list x y z))
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Rewrite the snippet below in Java so it works the same as the original Racket code. | #lang racket
(for*/list ([x (in-range 1 21)]
[y (in-range x 21)]
[z (in-range y 21)]
#:when (= (+ (* x x) (* y y)) (* z z)))
(list x y z))
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Write the same algorithm in Python as shown in this Racket implementation. | #lang racket
(for*/list ([x (in-range 1 21)]
[y (in-range x 21)]
[z (in-range y 21)]
#:when (= (+ (* x x) (* y y)) (* z z)))
(list x y z))
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Can you help me rewrite this code in VB instead of Racket, keeping it the same logically? | #lang racket
(for*/list ([x (in-range 1 21)]
[y (in-range x 21)]
[z (in-range y 21)]
#:when (= (+ (* x x) (* y y)) (* z z)))
(list x y z))
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Transform the following Racket implementation into Go, maintaining the same output and logic. | #lang racket
(for*/list ([x (in-range 1 21)]
[y (in-range x 21)]
[z (in-range y 21)]
#:when (= (+ (* x x) (* y y)) (* z z)))
(list x y z))
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Transform the following REXX implementation into C, maintaining the same output and logic. |
parse arg n .
if n=='' | n=="," then n= 100
say 'Pythagorean triples (a² + b² = c², c ≤' n"):"
$=
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab<cc then leave
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end
end
end
#= words($); sat
do j=1 for #
say left('', 20) word($, j)
end
say
say # ' members listed.'
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Convert the following code from REXX to C#, ensuring the logic remains intact. |
parse arg n .
if n=='' | n=="," then n= 100
say 'Pythagorean triples (a² + b² = c², c ≤' n"):"
$=
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab<cc then leave
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end
end
end
#= words($); sat
do j=1 for #
say left('', 20) word($, j)
end
say
say # ' members listed.'
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Write the same algorithm in C++ as shown in this REXX implementation. |
parse arg n .
if n=='' | n=="," then n= 100
say 'Pythagorean triples (a² + b² = c², c ≤' n"):"
$=
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab<cc then leave
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end
end
end
#= words($); sat
do j=1 for #
say left('', 20) word($, j)
end
say
say # ' members listed.'
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Convert the following code from REXX to Java, ensuring the logic remains intact. |
parse arg n .
if n=='' | n=="," then n= 100
say 'Pythagorean triples (a² + b² = c², c ≤' n"):"
$=
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab<cc then leave
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end
end
end
#= words($); sat
do j=1 for #
say left('', 20) word($, j)
end
say
say # ' members listed.'
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Generate an equivalent Python version of this REXX code. |
parse arg n .
if n=='' | n=="," then n= 100
say 'Pythagorean triples (a² + b² = c², c ≤' n"):"
$=
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab<cc then leave
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end
end
end
#= words($); sat
do j=1 for #
say left('', 20) word($, j)
end
say
say # ' members listed.'
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Write the same algorithm in VB as shown in this REXX implementation. |
parse arg n .
if n=='' | n=="," then n= 100
say 'Pythagorean triples (a² + b² = c², c ≤' n"):"
$=
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab<cc then leave
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end
end
end
#= words($); sat
do j=1 for #
say left('', 20) word($, j)
end
say
say # ' members listed.'
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Maintain the same structure and functionality when rewriting this code in Go. |
parse arg n .
if n=='' | n=="," then n= 100
say 'Pythagorean triples (a² + b² = c², c ≤' n"):"
$=
do a=1 for n-2; aa=a*a
do b=a+1 to n-1; ab=aa + b*b
do c=b+1 to n ; cc= c*c
if ab<cc then leave
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
end
end
end
#= words($); sat
do j=1 for #
say left('', 20) word($, j)
end
say
say # ' members listed.'
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Please provide an equivalent version of this Ruby code in C. | n = 20
r = ((1..n).flat_map { |x|
(x..n).flat_map { |y|
(y..n).flat_map { |z|
[[x, y, z]].keep_if { x * x + y * y == z * z }}}})
p r
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Ensure the translated C# code behaves exactly like the original Ruby snippet. | n = 20
r = ((1..n).flat_map { |x|
(x..n).flat_map { |y|
(y..n).flat_map { |z|
[[x, y, z]].keep_if { x * x + y * y == z * z }}}})
p r
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Can you help me rewrite this code in C++ instead of Ruby, keeping it the same logically? | n = 20
r = ((1..n).flat_map { |x|
(x..n).flat_map { |y|
(y..n).flat_map { |z|
[[x, y, z]].keep_if { x * x + y * y == z * z }}}})
p r
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Can you help me rewrite this code in Java instead of Ruby, keeping it the same logically? | n = 20
r = ((1..n).flat_map { |x|
(x..n).flat_map { |y|
(y..n).flat_map { |z|
[[x, y, z]].keep_if { x * x + y * y == z * z }}}})
p r
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Convert the following code from Ruby to Python, ensuring the logic remains intact. | n = 20
r = ((1..n).flat_map { |x|
(x..n).flat_map { |y|
(y..n).flat_map { |z|
[[x, y, z]].keep_if { x * x + y * y == z * z }}}})
p r
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Convert this Ruby block to VB, preserving its control flow and logic. | n = 20
r = ((1..n).flat_map { |x|
(x..n).flat_map { |y|
(y..n).flat_map { |z|
[[x, y, z]].keep_if { x * x + y * y == z * z }}}})
p r
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Port the following code from Ruby to Go with equivalent syntax and logic. | n = 20
r = ((1..n).flat_map { |x|
(x..n).flat_map { |y|
(y..n).flat_map { |z|
[[x, y, z]].keep_if { x * x + y * y == z * z }}}})
p r
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Generate an equivalent C version of this Scala code. |
fun pythagoreanTriples(n: Int) =
(1..n).flatMap {
x -> (x..n).flatMap {
y -> (y..n).filter {
z -> x * x + y * y == z * z
}.map { Triple(x, y, it) }
}
}
fun main(args: Array<String>) {
println(pythagoreanTriples(20))
}
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Translate this program into C# but keep the logic exactly as in Scala. |
fun pythagoreanTriples(n: Int) =
(1..n).flatMap {
x -> (x..n).flatMap {
y -> (y..n).filter {
z -> x * x + y * y == z * z
}.map { Triple(x, y, it) }
}
}
fun main(args: Array<String>) {
println(pythagoreanTriples(20))
}
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Can you help me rewrite this code in C++ instead of Scala, keeping it the same logically? |
fun pythagoreanTriples(n: Int) =
(1..n).flatMap {
x -> (x..n).flatMap {
y -> (y..n).filter {
z -> x * x + y * y == z * z
}.map { Triple(x, y, it) }
}
}
fun main(args: Array<String>) {
println(pythagoreanTriples(20))
}
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Please provide an equivalent version of this Scala code in Java. |
fun pythagoreanTriples(n: Int) =
(1..n).flatMap {
x -> (x..n).flatMap {
y -> (y..n).filter {
z -> x * x + y * y == z * z
}.map { Triple(x, y, it) }
}
}
fun main(args: Array<String>) {
println(pythagoreanTriples(20))
}
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Write a version of this Scala function in Python with identical behavior. |
fun pythagoreanTriples(n: Int) =
(1..n).flatMap {
x -> (x..n).flatMap {
y -> (y..n).filter {
z -> x * x + y * y == z * z
}.map { Triple(x, y, it) }
}
}
fun main(args: Array<String>) {
println(pythagoreanTriples(20))
}
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Generate an equivalent VB version of this Scala code. |
fun pythagoreanTriples(n: Int) =
(1..n).flatMap {
x -> (x..n).flatMap {
y -> (y..n).filter {
z -> x * x + y * y == z * z
}.map { Triple(x, y, it) }
}
}
fun main(args: Array<String>) {
println(pythagoreanTriples(20))
}
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Write a version of this Scala function in Go with identical behavior. |
fun pythagoreanTriples(n: Int) =
(1..n).flatMap {
x -> (x..n).flatMap {
y -> (y..n).filter {
z -> x * x + y * y == z * z
}.map { Triple(x, y, it) }
}
}
fun main(args: Array<String>) {
println(pythagoreanTriples(20))
}
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Rewrite this program in C while keeping its functionality equivalent to the Swift version. | typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
print(pythagoreanTriples(n: 20))
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Preserve the algorithm and functionality while converting the code from Swift to C#. | typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
print(pythagoreanTriples(n: 20))
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Rewrite this program in C++ while keeping its functionality equivalent to the Swift version. | typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
print(pythagoreanTriples(n: 20))
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Change the programming language of this snippet from Swift to Java without modifying what it does. | typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
print(pythagoreanTriples(n: 20))
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Generate a Python translation of this Swift snippet without changing its computational steps. | typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
print(pythagoreanTriples(n: 20))
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Transform the following Swift implementation into VB, maintaining the same output and logic. | typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
print(pythagoreanTriples(n: 20))
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Convert this Swift snippet to Go and keep its semantics consistent. | typealias F1 = (Int) -> [(Int, Int, Int)]
typealias F2 = (Int) -> Bool
func pythagoreanTriples(n: Int) -> [(Int, Int, Int)] {
(1...n).flatMap({x in
(x...n).flatMap({y in
(y...n).filter({z in
x * x + y * y == z * z
} as F2).map({ (x, y, $0) })
} as F1)
} as F1)
}
print(pythagoreanTriples(n: 20))
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Change the programming language of this snippet from Tcl to C without modifying what it does. | package require Tcl 8.5
proc lcomp {expression args} {
if {[llength $args] < 2} {
error "wrong
?... varN listN? ?condition?\""
}
if {[llength $args] % 2 == 1} {
set condition [lindex $args end]
set args [lrange $args 0 end-1]
} else {
set condition 1
}
set varlst [list]
foreach {var lst} $args {
set varlst [concat [list $var] [list $lst] $varlst]
}
set script {lappend result [subst $expression]}
if {$condition ne "1"} {
set script [list if $condition $script]
}
foreach {var lst} $varlst {
set script [list foreach $var $lst $script]
}
set result [list]
{*}$script ;
return $result
}
set range {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
puts [lcomp {$x $y $z} x $range y $range z $range {$x < $y && $x**2 + $y**2 == $z**2}]
| for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
|
Port the provided Tcl code into C# while preserving the original functionality. | package require Tcl 8.5
proc lcomp {expression args} {
if {[llength $args] < 2} {
error "wrong
?... varN listN? ?condition?\""
}
if {[llength $args] % 2 == 1} {
set condition [lindex $args end]
set args [lrange $args 0 end-1]
} else {
set condition 1
}
set varlst [list]
foreach {var lst} $args {
set varlst [concat [list $var] [list $lst] $varlst]
}
set script {lappend result [subst $expression]}
if {$condition ne "1"} {
set script [list if $condition $script]
}
foreach {var lst} $varlst {
set script [list foreach $var $lst $script]
}
set result [list]
{*}$script ;
return $result
}
set range {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
puts [lcomp {$x $y $z} x $range y $range z $range {$x < $y && $x**2 + $y**2 == $z**2}]
| using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
|
Write the same code in C++ as shown below in Tcl. | package require Tcl 8.5
proc lcomp {expression args} {
if {[llength $args] < 2} {
error "wrong
?... varN listN? ?condition?\""
}
if {[llength $args] % 2 == 1} {
set condition [lindex $args end]
set args [lrange $args 0 end-1]
} else {
set condition 1
}
set varlst [list]
foreach {var lst} $args {
set varlst [concat [list $var] [list $lst] $varlst]
}
set script {lappend result [subst $expression]}
if {$condition ne "1"} {
set script [list if $condition $script]
}
foreach {var lst} $varlst {
set script [list foreach $var $lst $script]
}
set result [list]
{*}$script ;
return $result
}
set range {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
puts [lcomp {$x $y $z} x $range y $range z $range {$x < $y && $x**2 + $y**2 == $z**2}]
| #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
|
Rewrite the snippet below in Java so it works the same as the original Tcl code. | package require Tcl 8.5
proc lcomp {expression args} {
if {[llength $args] < 2} {
error "wrong
?... varN listN? ?condition?\""
}
if {[llength $args] % 2 == 1} {
set condition [lindex $args end]
set args [lrange $args 0 end-1]
} else {
set condition 1
}
set varlst [list]
foreach {var lst} $args {
set varlst [concat [list $var] [list $lst] $varlst]
}
set script {lappend result [subst $expression]}
if {$condition ne "1"} {
set script [list if $condition $script]
}
foreach {var lst} $varlst {
set script [list foreach $var $lst $script]
}
set result [list]
{*}$script ;
return $result
}
set range {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
puts [lcomp {$x $y $z} x $range y $range z $range {$x < $y && $x**2 + $y**2 == $z**2}]
|
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
|
Generate a Python translation of this Tcl snippet without changing its computational steps. | package require Tcl 8.5
proc lcomp {expression args} {
if {[llength $args] < 2} {
error "wrong
?... varN listN? ?condition?\""
}
if {[llength $args] % 2 == 1} {
set condition [lindex $args end]
set args [lrange $args 0 end-1]
} else {
set condition 1
}
set varlst [list]
foreach {var lst} $args {
set varlst [concat [list $var] [list $lst] $varlst]
}
set script {lappend result [subst $expression]}
if {$condition ne "1"} {
set script [list if $condition $script]
}
foreach {var lst} $varlst {
set script [list foreach $var $lst $script]
}
set result [list]
{*}$script ;
return $result
}
set range {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
puts [lcomp {$x $y $z} x $range y $range z $range {$x < $y && $x**2 + $y**2 == $z**2}]
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Write the same code in VB as shown below in Tcl. | package require Tcl 8.5
proc lcomp {expression args} {
if {[llength $args] < 2} {
error "wrong
?... varN listN? ?condition?\""
}
if {[llength $args] % 2 == 1} {
set condition [lindex $args end]
set args [lrange $args 0 end-1]
} else {
set condition 1
}
set varlst [list]
foreach {var lst} $args {
set varlst [concat [list $var] [list $lst] $varlst]
}
set script {lappend result [subst $expression]}
if {$condition ne "1"} {
set script [list if $condition $script]
}
foreach {var lst} $varlst {
set script [list foreach $var $lst $script]
}
set result [list]
{*}$script ;
return $result
}
set range {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
puts [lcomp {$x $y $z} x $range y $range z $range {$x < $y && $x**2 + $y**2 == $z**2}]
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Change the programming language of this snippet from Tcl to Go without modifying what it does. | package require Tcl 8.5
proc lcomp {expression args} {
if {[llength $args] < 2} {
error "wrong
?... varN listN? ?condition?\""
}
if {[llength $args] % 2 == 1} {
set condition [lindex $args end]
set args [lrange $args 0 end-1]
} else {
set condition 1
}
set varlst [list]
foreach {var lst} $args {
set varlst [concat [list $var] [list $lst] $varlst]
}
set script {lappend result [subst $expression]}
if {$condition ne "1"} {
set script [list if $condition $script]
}
foreach {var lst} $varlst {
set script [list foreach $var $lst $script]
}
set result [list]
{*}$script ;
return $result
}
set range {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
puts [lcomp {$x $y $z} x $range y $range z $range {$x < $y && $x**2 + $y**2 == $z**2}]
| package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
|
Can you help me rewrite this code in Rust instead of C++, keeping it the same logically? | #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
void list_comprehension( std::vector<int> & , int ) ;
int main( ) {
std::vector<int> triangles ;
list_comprehension( triangles , 20 ) ;
std::copy( triangles.begin( ) , triangles.end( ) ,
std::ostream_iterator<int>( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}
void list_comprehension( std::vector<int> & numbers , int upper_border ) {
for ( int a = 1 ; a < upper_border ; a++ ) {
for ( int b = a + 1 ; b < upper_border ; b++ ) {
double c = pow( a * a + b * b , 0.5 ) ;
if ( ( c * c ) < pow( upper_border , 2 ) + 1 ) {
if ( c == floor( c ) ) {
numbers.push_back( a ) ;
numbers.push_back( b ) ;
numbers.push_back( static_cast<int>( c ) ) ;
}
}
}
}
}
| fn pyth(n: u32) -> impl Iterator<Item = [u32; 3]> {
(1..=n).flat_map(move |x| {
(x..=n).flat_map(move |y| {
(y..=n).filter_map(move |z| {
if x.pow(2) + y.pow(2) == z.pow(2) {
Some([x, y, z])
} else {
None
}
})
})
})
}
|
Produce a language-to-language conversion: from C# to Rust, same semantics. | using System.Linq;
static class Program
{
static void Main()
{
var ts =
from a in Enumerable.Range(1, 20)
from b in Enumerable.Range(a, 21 - a)
from c in Enumerable.Range(b, 21 - b)
where a * a + b * b == c * c
select new { a, b, c };
foreach (var t in ts)
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c);
}
}
| fn pyth(n: u32) -> impl Iterator<Item = [u32; 3]> {
(1..=n).flat_map(move |x| {
(x..=n).flat_map(move |y| {
(y..=n).filter_map(move |z| {
if x.pow(2) + y.pow(2) == z.pow(2) {
Some([x, y, z])
} else {
None
}
})
})
})
}
|
Can you help me rewrite this code in Rust instead of Go, keeping it the same logically? | package main
import "fmt"
type (
seq []int
sofs []seq
)
func newSeq(start, end int) seq {
if end < start {
end = start
}
s := make(seq, end-start+1)
for i := 0; i < len(s); i++ {
s[i] = start + i
}
return s
}
func newSofs() sofs {
return sofs{seq{}}
}
func (s sofs) listComp(in seq, expr func(sofs, seq) sofs, pred func(seq) bool) sofs {
var s2 sofs
for _, t := range expr(s, in) {
if pred(t) {
s2 = append(s2, t)
}
}
return s2
}
func (s sofs) build(t seq) sofs {
var u sofs
for _, ss := range s {
for _, tt := range t {
uu := make(seq, len(ss))
copy(uu, ss)
uu = append(uu, tt)
u = append(u, uu)
}
}
return u
}
func main() {
pt := newSofs()
in := newSeq(1, 20)
expr := func(s sofs, t seq) sofs {
return s.build(t).build(t).build(t)
}
pred := func(t seq) bool {
if len(t) != 3 {
return false
}
return t[0]*t[0]+t[1]*t[1] == t[2]*t[2] && t[0] < t[1] && t[1] < t[2]
}
pt = pt.listComp(in, expr, pred)
fmt.Println(pt)
}
| fn pyth(n: u32) -> impl Iterator<Item = [u32; 3]> {
(1..=n).flat_map(move |x| {
(x..=n).flat_map(move |y| {
(y..=n).filter_map(move |z| {
if x.pow(2) + y.pow(2) == z.pow(2) {
Some([x, y, z])
} else {
None
}
})
})
})
}
|
Please provide an equivalent version of this Rust code in Python. | fn pyth(n: u32) -> impl Iterator<Item = [u32; 3]> {
(1..=n).flat_map(move |x| {
(x..=n).flat_map(move |y| {
(y..=n).filter_map(move |z| {
if x.pow(2) + y.pow(2) == z.pow(2) {
Some([x, y, z])
} else {
None
}
})
})
})
}
| [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
Transform the following Rust implementation into VB, maintaining the same output and logic. | fn pyth(n: u32) -> impl Iterator<Item = [u32; 3]> {
(1..=n).flat_map(move |x| {
(x..=n).flat_map(move |y| {
(y..=n).filter_map(move |z| {
if x.pow(2) + y.pow(2) == z.pow(2) {
Some([x, y, z])
} else {
None
}
})
})
})
}
| Module ListComp
Sub Main()
Dim ts = From a In Enumerable.Range(1, 20) _
From b In Enumerable.Range(a, 21 - a) _
From c In Enumerable.Range(b, 21 - b) _
Where a * a + b * b = c * c _
Select New With { a, b, c }
For Each t In ts
System.Console.WriteLine("{0}, {1}, {2}", t.a, t.b, t.c)
Next
End Sub
End Module
|
Rewrite this program in Rust while keeping its functionality equivalent to the Java version. |
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
.map(Arrays::asList)
.collect(toList())
;
}
}
| fn pyth(n: u32) -> impl Iterator<Item = [u32; 3]> {
(1..=n).flat_map(move |x| {
(x..=n).flat_map(move |y| {
(y..=n).filter_map(move |z| {
if x.pow(2) + y.pow(2) == z.pow(2) {
Some([x, y, z])
} else {
None
}
})
})
})
}
|
Write a version of this C function in Rust with identical behavior. | for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
| fn pyth(n: u32) -> impl Iterator<Item = [u32; 3]> {
(1..=n).flat_map(move |x| {
(x..=n).flat_map(move |y| {
(y..=n).filter_map(move |z| {
if x.pow(2) + y.pow(2) == z.pow(2) {
Some([x, y, z])
} else {
None
}
})
})
})
}
|
Produce a language-to-language conversion: from Ada to C#, same semantics. | 3.141_592_6
1.0E-12
0.13
| double d = 1;
d = 1d;
d = 1D;
d = 1.2;
d = 1.2d;
d = .2;
d = 12e-12;
d = 12E-12;
d = 1_234e-1_2;
float f = 1;
f = 1f;
f = 1F;
f = 1.2f;
f = .2f;
f = 12e-12f;
f = 12E-12f;
f = 1_234e-1_2f;
decimal m = 1;
m = 1m;
m = 1m;
m = 1.2m;
m = .2m;
m = 12e-12m;
m = 12E-12m;
m = 1_234e-1_2m;
|
Translate this program into C++ but keep the logic exactly as in Ada. | 3.141_592_6
1.0E-12
0.13
| #include <iostream>
int main()
{
auto double1 = 2.5;
auto float1 = 2.5f;
auto longdouble1 = 2.5l;
auto double2 = 2.5e-3;
auto float2 = 2.5e3f;
auto double3 = 0x1p4;
auto float3 = 0xbeefp-8f;
std::cout << "\ndouble1: " << double1;
std::cout << "\nfloat1: " << float1;
std::cout << "\nlongdouble1: " << longdouble1;
std::cout << "\ndouble2: " << double2;
std::cout << "\nfloat2: " << float2;
std::cout << "\ndouble3: " << double3;
std::cout << "\nfloat3: " << float3;
std::cout << "\n";
}
|
Change the programming language of this snippet from Ada to Java without modifying what it does. | 3.141_592_6
1.0E-12
0.13
| 1.
1.0
2432311.7567374
1.234E-10
1.234e-10
758832d
728832f
1.0f
758832D
728832F
1.0F
1 / 2.
1 / 2
|
Can you help me rewrite this code in Python instead of Ada, keeping it the same logically? | 3.141_592_6
1.0E-12
0.13
| 2.3
.3
.3e4
.3e+34
.3e-34
2.e34
|
Rewrite this program in VB while keeping its functionality equivalent to the Ada version. | 3.141_592_6
1.0E-12
0.13
| Sub Main()
Dim d As Double
Dim s As Single
d = -12.3456
d = 1000#
d = 0.00001
d = 67#
d = 8.9
d = 0.33
d = 0#
d = 2# * 10 ^ 3
d = 2E+50
d = 2E-50
s = -12.3456!
s = 1000!
s = 0.00001!
s = 67!
s = 8.9!
s = 0.33!
s = 0!
s = 2! * 10 ^ 3
End Sub
|
Translate the given Arturo code snippet into C# without altering its behavior. | pi: 3.14
print [pi "->" type pi]
| double d = 1;
d = 1d;
d = 1D;
d = 1.2;
d = 1.2d;
d = .2;
d = 12e-12;
d = 12E-12;
d = 1_234e-1_2;
float f = 1;
f = 1f;
f = 1F;
f = 1.2f;
f = .2f;
f = 12e-12f;
f = 12E-12f;
f = 1_234e-1_2f;
decimal m = 1;
m = 1m;
m = 1m;
m = 1.2m;
m = .2m;
m = 12e-12m;
m = 12E-12m;
m = 1_234e-1_2m;
|
Ensure the translated C++ code behaves exactly like the original Arturo snippet. | pi: 3.14
print [pi "->" type pi]
| #include <iostream>
int main()
{
auto double1 = 2.5;
auto float1 = 2.5f;
auto longdouble1 = 2.5l;
auto double2 = 2.5e-3;
auto float2 = 2.5e3f;
auto double3 = 0x1p4;
auto float3 = 0xbeefp-8f;
std::cout << "\ndouble1: " << double1;
std::cout << "\nfloat1: " << float1;
std::cout << "\nlongdouble1: " << longdouble1;
std::cout << "\ndouble2: " << double2;
std::cout << "\nfloat2: " << float2;
std::cout << "\ndouble3: " << double3;
std::cout << "\nfloat3: " << float3;
std::cout << "\n";
}
|
Produce a functionally identical Java code for the snippet given in Arturo. | pi: 3.14
print [pi "->" type pi]
| 1.
1.0
2432311.7567374
1.234E-10
1.234e-10
758832d
728832f
1.0f
758832D
728832F
1.0F
1 / 2.
1 / 2
|
Generate a Python translation of this Arturo snippet without changing its computational steps. | pi: 3.14
print [pi "->" type pi]
| 2.3
.3
.3e4
.3e+34
.3e-34
2.e34
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.