WeixiangYan/CodeTransOcean huggingface dataset:
Task: Vector





Java: import java.util.Locale;

public class Test {

    public static void main(String[] args) {
        System.out.println(new Vec2(5, 7).add(new Vec2(2, 3)));
        System.out.println(new Vec2(5, 7).sub(new Vec2(2, 3)));
        System.out.println(new Vec2(5, 7).mult(11));
        System.out.println(new Vec2(5, 7).div(2));
    }
}

class Vec2 {
    final double x, y;

    Vec2(double x, double y) {
        this.x = x;
        this.y = y;
    }

    Vec2 add(Vec2 v) {
        return new Vec2(x + v.x, y + v.y);
    }

    Vec2 sub(Vec2 v) {
        return new Vec2(x - v.x, y - v.y);
    }

    Vec2 div(double val) {
        return new Vec2(x / val, y / val);
    }

    Vec2 mult(double val) {
        return new Vec2(x * val, y * val);
    }

    @Override
    public String toString() {
        return String.format(Locale.US, "[%s, %s]", x, y);
    }
}




VB: Type vector
    x As Double
    y As Double
End Type
Type vector2
    phi As Double
    r As Double
End Type
Private Function vector_addition(u As vector, v As vector) As vector
    vector_addition.x = u.x + v.x
    vector_addition.y = u.y + v.y
End Function
Private Function vector_subtraction(u As vector, v As vector) As vector
    vector_subtraction.x = u.x - v.x
    vector_subtraction.y = u.y - v.y
End Function
Private Function scalar_multiplication(u As vector, v As Double) As vector
    scalar_multiplication.x = u.x * v
    scalar_multiplication.y = u.y * v
End Function
Private Function scalar_division(u As vector, v As Double) As vector
    scalar_division.x = u.x / v
    scalar_division.y = u.y / v
End Function
Private Function to_cart(v2 As vector2) As vector
    to_cart.x = v2.r * Cos(v2.phi)
    to_cart.y = v2.r * Sin(v2.phi)
End Function
Private Sub display(u As vector)
    Debug.Print "( " & Format(u.x, "0.000") & "; " & Format(u.y, "0.000") & ")";
End Sub
Public Sub main()
    Dim a As vector, b As vector, c As vector2, d As Double
    c.phi = WorksheetFunction.Pi() / 3
    c.r = 5
    d = 10
    a = to_cart(c)
    b.x = 1: b.y = -2
    Debug.Print "addition             : ";: display a: Debug.Print "+";: display b
    Debug.Print "=";: display vector_addition(a, b): Debug.Print
    Debug.Print "subtraction          : ";: display a: Debug.Print "-";: display b
    Debug.Print "=";: display vector_subtraction(a, b): Debug.Print
    Debug.Print "scalar multiplication: ";: display a: Debug.Print " *";: Debug.Print d;
    Debug.Print "=";: display scalar_multiplication(a, d): Debug.Print
    Debug.Print "scalar division      : ";: display a: Debug.Print " /";: Debug.Print d;
    Debug.Print "=";: display scalar_division(a, d)
End Sub
Task: Vector




Go: package main

import "fmt"

type vector []float64

func (v vector) add(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi + v2[i]
    }
    return r
}

func (v vector) sub(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi - v2[i]
    }
    return r
}

func (v vector) scalarMul(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi * s
    }
    return r
}

func (v vector) scalarDiv(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi / s
    }
    return r
}

func main() {
    v1 := vector{5, 7}
    v2 := vector{2, 3}
    fmt.Println(v1.add(v2))
    fmt.Println(v1.sub(v2))
    fmt.Println(v1.scalarMul(11))
    fmt.Println(v1.scalarDiv(2))
}




Python: v1 = PVector(5, 7)
v2 = PVector(2, 3)

println('{} {} {} {}\n'.format( v1.x, v1.y, v1.mag(), v1.heading()))


println(v1 + v2) 
println(v1 - v2) 
println(v1 * 11) 
println(v1 / 2)  
println('')


println(v1.sub(v1))  
println(v1.add(v2))  
println(v1.mult(10)) 
println(v1.div(10))
Task: Vector




Go: package main

import "fmt"

type vector []float64

func (v vector) add(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi + v2[i]
    }
    return r
}

func (v vector) sub(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi - v2[i]
    }
    return r
}

func (v vector) scalarMul(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi * s
    }
    return r
}

func (v vector) scalarDiv(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi / s
    }
    return r
}

func main() {
    v1 := vector{5, 7}
    v2 := vector{2, 3}
    fmt.Println(v1.add(v2))
    fmt.Println(v1.sub(v2))
    fmt.Println(v1.scalarMul(11))
    fmt.Println(v1.scalarDiv(2))
}





VB: Type vector
    x As Double
    y As Double
End Type
Type vector2
    phi As Double
    r As Double
End Type
Private Function vector_addition(u As vector, v As vector) As vector
    vector_addition.x = u.x + v.x
    vector_addition.y = u.y + v.y
End Function
Private Function vector_subtraction(u As vector, v As vector) As vector
    vector_subtraction.x = u.x - v.x
    vector_subtraction.y = u.y - v.y
End Function
Private Function scalar_multiplication(u As vector, v As Double) As vector
    scalar_multiplication.x = u.x * v
    scalar_multiplication.y = u.y * v
End Function
Private Function scalar_division(u As vector, v As Double) As vector
    scalar_division.x = u.x / v
    scalar_division.y = u.y / v
End Function
Private Function to_cart(v2 As vector2) As vector
    to_cart.x = v2.r * Cos(v2.phi)
    to_cart.y = v2.r * Sin(v2.phi)
End Function
Private Sub display(u As vector)
    Debug.Print "( " & Format(u.x, "0.000") & "; " & Format(u.y, "0.000") & ")";
End Sub
Public Sub main()
    Dim a As vector, b As vector, c As vector2, d As Double
    c.phi = WorksheetFunction.Pi() / 3
    c.r = 5
    d = 10
    a = to_cart(c)
    b.x = 1: b.y = -2
    Debug.Print "addition             : ";: display a: Debug.Print "+";: display b
    Debug.Print "=";: display vector_addition(a, b): Debug.Print
    Debug.Print "subtraction          : ";: display a: Debug.Print "-";: display b
    Debug.Print "=";: display vector_subtraction(a, b): Debug.Print
    Debug.Print "scalar multiplication: ";: display a: Debug.Print " *";: Debug.Print d;
    Debug.Print "=";: display scalar_multiplication(a, d): Debug.Print
    Debug.Print "scalar division      : ";: display a: Debug.Print " /";: Debug.Print d;
    Debug.Print "=";: display scalar_division(a, d)
End Sub
Task: Vector

C: #include<stdio.h>
#include<math.h>

#define pi M_PI

typedef struct{
	double x,y;
}vector;

vector initVector(double r,double theta){
	vector c;
	
	c.x = r*cos(theta);
	c.y = r*sin(theta);
	
	return c;
}

vector addVector(vector a,vector b){
	vector c;
	
	c.x = a.x + b.x;
	c.y = a.y + b.y;
	
	return c;
}

vector subtractVector(vector a,vector b){
	vector c;
	
	c.x = a.x - b.x;
	c.y = a.y - b.y;
	
	return c;
}

vector multiplyVector(vector a,double b){
	vector c;
	
	c.x = b*a.x;
	c.y = b*a.y;
	
	return c;
}

vector divideVector(vector a,double b){
	vector c;
	
	c.x = a.x/b;
	c.y = a.y/b;
	
	return c;
}

void printVector(vector a){
	printf("%lf %c %c %lf %c",a.x,140,(a.y>=0)?'+':'-',(a.y>=0)?a.y:fabs(a.y),150);
}

int main()
{
	vector a = initVector(3,pi/6);
	vector b = initVector(5,2*pi/3);
	
	printf("\nVector a : ");
	printVector(a);
	
	printf("\n\nVector b : ");
	printVector(b);
	
	printf("\n\nSum of vectors a and b : ");
	printVector(addVector(a,b));
	
	printf("\n\nDifference of vectors a and b : ");
	printVector(subtractVector(a,b));
	
	printf("\n\nMultiplying vector a by 3 : ");
	printVector(multiplyVector(a,3));
	
	printf("\n\nDividing vector b by 2.5 : ");
	printVector(divideVector(b,2.5));
	
	return 0;
}




Go: package main

import "fmt"

type vector []float64

func (v vector) add(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi + v2[i]
    }
    return r
}

func (v vector) sub(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi - v2[i]
    }
    return r
}

func (v vector) scalarMul(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi * s
    }
    return r
}

func (v vector) scalarDiv(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi / s
    }
    return r
}

func main() {
    v1 := vector{5, 7}
    v2 := vector{2, 3}
    fmt.Println(v1.add(v2))
    fmt.Println(v1.sub(v2))
    fmt.Println(v1.scalarMul(11))
    fmt.Println(v1.scalarDiv(2))
}
Task: Vector

C: #include<stdio.h>
#include<math.h>

#define pi M_PI

typedef struct{
	double x,y;
}vector;

vector initVector(double r,double theta){
	vector c;
	
	c.x = r*cos(theta);
	c.y = r*sin(theta);
	
	return c;
}

vector addVector(vector a,vector b){
	vector c;
	
	c.x = a.x + b.x;
	c.y = a.y + b.y;
	
	return c;
}

vector subtractVector(vector a,vector b){
	vector c;
	
	c.x = a.x - b.x;
	c.y = a.y - b.y;
	
	return c;
}

vector multiplyVector(vector a,double b){
	vector c;
	
	c.x = b*a.x;
	c.y = b*a.y;
	
	return c;
}

vector divideVector(vector a,double b){
	vector c;
	
	c.x = a.x/b;
	c.y = a.y/b;
	
	return c;
}

void printVector(vector a){
	printf("%lf %c %c %lf %c",a.x,140,(a.y>=0)?'+':'-',(a.y>=0)?a.y:fabs(a.y),150);
}

int main()
{
	vector a = initVector(3,pi/6);
	vector b = initVector(5,2*pi/3);
	
	printf("\nVector a : ");
	printVector(a);
	
	printf("\n\nVector b : ");
	printVector(b);
	
	printf("\n\nSum of vectors a and b : ");
	printVector(addVector(a,b));
	
	printf("\n\nDifference of vectors a and b : ");
	printVector(subtractVector(a,b));
	
	printf("\n\nMultiplying vector a by 3 : ");
	printVector(multiplyVector(a,3));
	
	printf("\n\nDividing vector b by 2.5 : ");
	printVector(divideVector(b,2.5));
	
	return 0;
}





Java: import java.util.Locale;

public class Test {

    public static void main(String[] args) {
        System.out.println(new Vec2(5, 7).add(new Vec2(2, 3)));
        System.out.println(new Vec2(5, 7).sub(new Vec2(2, 3)));
        System.out.println(new Vec2(5, 7).mult(11));
        System.out.println(new Vec2(5, 7).div(2));
    }
}

class Vec2 {
    final double x, y;

    Vec2(double x, double y) {
        this.x = x;
        this.y = y;
    }

    Vec2 add(Vec2 v) {
        return new Vec2(x + v.x, y + v.y);
    }

    Vec2 sub(Vec2 v) {
        return new Vec2(x - v.x, y - v.y);
    }

    Vec2 div(double val) {
        return new Vec2(x / val, y / val);
    }

    Vec2 mult(double val) {
        return new Vec2(x * val, y * val);
    }

    @Override
    public String toString() {
        return String.format(Locale.US, "[%s, %s]", x, y);
    }
}
Task: Vector


C++: #include <iostream>
#include <cmath>
#include <cassert>
using namespace std;

#define PI 3.14159265359

class Vector
{
public:
    Vector(double ix, double iy, char mode)
    {
        if(mode=='a')
        {
            x=ix*cos(iy);
            y=ix*sin(iy);
        }
        else
        {
            x=ix;
            y=iy;
        }
    }
    Vector(double ix,double iy)
    {
        x=ix;
        y=iy;
    }
    Vector operator+(const Vector& first)
    {
        return Vector(x+first.x,y+first.y);
    }
    Vector operator-(Vector first)
    {
        return Vector(x-first.x,y-first.y);
    }
    Vector operator*(double scalar)
    {
        return Vector(x*scalar,y*scalar);
    }
    Vector operator/(double scalar)
    {
        return Vector(x/scalar,y/scalar);
    }
    bool operator==(Vector first)
    {
        return (x==first.x&&y==first.y);
    }
    void v_print()
    {
        cout << "X: " << x << " Y: " << y;
    }
    double x,y;
};

int main()
{
    Vector vec1(0,1);
    Vector vec2(2,2);
    Vector vec3(sqrt(2),45*PI/180,'a');
    vec3.v_print();
    assert(vec1+vec2==Vector(2,3));
    assert(vec1-vec2==Vector(-2,-1));
    assert(vec1*5==Vector(0,5));
    assert(vec2/2==Vector(1,1));
    return 0;
}



Go: package main

import "fmt"

type vector []float64

func (v vector) add(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi + v2[i]
    }
    return r
}

func (v vector) sub(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi - v2[i]
    }
    return r
}

func (v vector) scalarMul(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi * s
    }
    return r
}

func (v vector) scalarDiv(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi / s
    }
    return r
}

func main() {
    v1 := vector{5, 7}
    v2 := vector{2, 3}
    fmt.Println(v1.add(v2))
    fmt.Println(v1.sub(v2))
    fmt.Println(v1.scalarMul(11))
    fmt.Println(v1.scalarDiv(2))
}
Task: Vector


C++: #include <iostream>
#include <cmath>
#include <cassert>
using namespace std;

#define PI 3.14159265359

class Vector
{
public:
    Vector(double ix, double iy, char mode)
    {
        if(mode=='a')
        {
            x=ix*cos(iy);
            y=ix*sin(iy);
        }
        else
        {
            x=ix;
            y=iy;
        }
    }
    Vector(double ix,double iy)
    {
        x=ix;
        y=iy;
    }
    Vector operator+(const Vector& first)
    {
        return Vector(x+first.x,y+first.y);
    }
    Vector operator-(Vector first)
    {
        return Vector(x-first.x,y-first.y);
    }
    Vector operator*(double scalar)
    {
        return Vector(x*scalar,y*scalar);
    }
    Vector operator/(double scalar)
    {
        return Vector(x/scalar,y/scalar);
    }
    bool operator==(Vector first)
    {
        return (x==first.x&&y==first.y);
    }
    void v_print()
    {
        cout << "X: " << x << " Y: " << y;
    }
    double x,y;
};

int main()
{
    Vector vec1(0,1);
    Vector vec2(2,2);
    Vector vec3(sqrt(2),45*PI/180,'a');
    vec3.v_print();
    assert(vec1+vec2==Vector(2,3));
    assert(vec1-vec2==Vector(-2,-1));
    assert(vec1*5==Vector(0,5));
    assert(vec2/2==Vector(1,1));
    return 0;
}






Python: v1 = PVector(5, 7)
v2 = PVector(2, 3)

println('{} {} {} {}\n'.format( v1.x, v1.y, v1.mag(), v1.heading()))


println(v1 + v2) 
println(v1 - v2) 
println(v1 * 11) 
println(v1 / 2)  
println('')


println(v1.sub(v1))  
println(v1.add(v2))  
println(v1.mult(10)) 
println(v1.div(10))
Task: Vector


C++: #include <iostream>
#include <cmath>
#include <cassert>
using namespace std;

#define PI 3.14159265359

class Vector
{
public:
    Vector(double ix, double iy, char mode)
    {
        if(mode=='a')
        {
            x=ix*cos(iy);
            y=ix*sin(iy);
        }
        else
        {
            x=ix;
            y=iy;
        }
    }
    Vector(double ix,double iy)
    {
        x=ix;
        y=iy;
    }
    Vector operator+(const Vector& first)
    {
        return Vector(x+first.x,y+first.y);
    }
    Vector operator-(Vector first)
    {
        return Vector(x-first.x,y-first.y);
    }
    Vector operator*(double scalar)
    {
        return Vector(x*scalar,y*scalar);
    }
    Vector operator/(double scalar)
    {
        return Vector(x/scalar,y/scalar);
    }
    bool operator==(Vector first)
    {
        return (x==first.x&&y==first.y);
    }
    void v_print()
    {
        cout << "X: " << x << " Y: " << y;
    }
    double x,y;
};

int main()
{
    Vector vec1(0,1);
    Vector vec2(2,2);
    Vector vec3(sqrt(2),45*PI/180,'a');
    vec3.v_print();
    assert(vec1+vec2==Vector(2,3));
    assert(vec1-vec2==Vector(-2,-1));
    assert(vec1*5==Vector(0,5));
    assert(vec2/2==Vector(1,1));
    return 0;
}







VB: Type vector
    x As Double
    y As Double
End Type
Type vector2
    phi As Double
    r As Double
End Type
Private Function vector_addition(u As vector, v As vector) As vector
    vector_addition.x = u.x + v.x
    vector_addition.y = u.y + v.y
End Function
Private Function vector_subtraction(u As vector, v As vector) As vector
    vector_subtraction.x = u.x - v.x
    vector_subtraction.y = u.y - v.y
End Function
Private Function scalar_multiplication(u As vector, v As Double) As vector
    scalar_multiplication.x = u.x * v
    scalar_multiplication.y = u.y * v
End Function
Private Function scalar_division(u As vector, v As Double) As vector
    scalar_division.x = u.x / v
    scalar_division.y = u.y / v
End Function
Private Function to_cart(v2 As vector2) As vector
    to_cart.x = v2.r * Cos(v2.phi)
    to_cart.y = v2.r * Sin(v2.phi)
End Function
Private Sub display(u As vector)
    Debug.Print "( " & Format(u.x, "0.000") & "; " & Format(u.y, "0.000") & ")";
End Sub
Public Sub main()
    Dim a As vector, b As vector, c As vector2, d As Double
    c.phi = WorksheetFunction.Pi() / 3
    c.r = 5
    d = 10
    a = to_cart(c)
    b.x = 1: b.y = -2
    Debug.Print "addition             : ";: display a: Debug.Print "+";: display b
    Debug.Print "=";: display vector_addition(a, b): Debug.Print
    Debug.Print "subtraction          : ";: display a: Debug.Print "-";: display b
    Debug.Print "=";: display vector_subtraction(a, b): Debug.Print
    Debug.Print "scalar multiplication: ";: display a: Debug.Print " *";: Debug.Print d;
    Debug.Print "=";: display scalar_multiplication(a, d): Debug.Print
    Debug.Print "scalar division      : ";: display a: Debug.Print " /";: Debug.Print d;
    Debug.Print "=";: display scalar_division(a, d)
End Sub
Task: Vector



C#: using System;
using System.Collections.Generic;
using System.Linq;

namespace RosettaVectors
{
    public class Vector
    {
        public double[] store;
        public Vector(IEnumerable<double> init)
        {
            store = init.ToArray();
        }
        public Vector(double x, double y)
        {
            store = new double[] { x, y };
        }
        static public Vector operator+(Vector v1, Vector v2)
        {
            return new Vector(v1.store.Zip(v2.store, (a, b) => a + b));
        }
        static public Vector operator -(Vector v1, Vector v2)
        {
            return new Vector(v1.store.Zip(v2.store, (a, b) => a - b));
        }
        static public Vector operator *(Vector v1, double scalar)
        {
            return new Vector(v1.store.Select(x => x * scalar));
        }
        static public Vector operator /(Vector v1, double scalar)
        {
            return new Vector(v1.store.Select(x => x / scalar));
        }
        public override string ToString()
        {
            return string.Format("[{0}]", string.Join(",", store));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var v1 = new Vector(5, 7);
            var v2 = new Vector(2, 3);
            Console.WriteLine(v1 + v2);
            Console.WriteLine(v1 - v2);
            Console.WriteLine(v1 * 11);
            Console.WriteLine(v1 / 2);
            
            var lostVector = new Vector(new double[] { 4, 8, 15, 16, 23, 42 });
            Console.WriteLine(lostVector * 7);
            Console.ReadLine();
        }
    }
}


Go: package main

import "fmt"

type vector []float64

func (v vector) add(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi + v2[i]
    }
    return r
}

func (v vector) sub(v2 vector) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi - v2[i]
    }
    return r
}

func (v vector) scalarMul(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi * s
    }
    return r
}

func (v vector) scalarDiv(s float64) vector {
    r := make([]float64, len(v))
    for i, vi := range v {
        r[i] = vi / s
    }
    return r
}

func main() {
    v1 := vector{5, 7}
    v2 := vector{2, 3}
    fmt.Println(v1.add(v2))
    fmt.Println(v1.sub(v2))
    fmt.Println(v1.scalarMul(11))
    fmt.Println(v1.scalarDiv(2))
}
Task: Vector







Python: v1 = PVector(5, 7)
v2 = PVector(2, 3)

println('{} {} {} {}\n'.format( v1.x, v1.y, v1.mag(), v1.heading()))


println(v1 + v2) 
println(v1 - v2) 
println(v1 * 11) 
println(v1 / 2)  
println('')


println(v1.sub(v1))  
println(v1.add(v2))  
println(v1.mult(10)) 
println(v1.div(10))  


VB: Type vector
    x As Double
    y As Double
End Type
Type vector2
    phi As Double
    r As Double
End Type
Private Function vector_addition(u As vector, v As vector) As vector
    vector_addition.x = u.x + v.x
    vector_addition.y = u.y + v.y
End Function
Private Function vector_subtraction(u As vector, v As vector) As vector
    vector_subtraction.x = u.x - v.x
    vector_subtraction.y = u.y - v.y
End Function
Private Function scalar_multiplication(u As vector, v As Double) As vector
    scalar_multiplication.x = u.x * v
    scalar_multiplication.y = u.y * v
End Function
Private Function scalar_division(u As vector, v As Double) As vector
    scalar_division.x = u.x / v
    scalar_division.y = u.y / v
End Function
Private Function to_cart(v2 As vector2) As vector
    to_cart.x = v2.r * Cos(v2.phi)
    to_cart.y = v2.r * Sin(v2.phi)
End Function
Private Sub display(u As vector)
    Debug.Print "( " & Format(u.x, "0.000") & "; " & Format(u.y, "0.000") & ")";
End Sub
Public Sub main()
    Dim a As vector, b As vector, c As vector2, d As Double
    c.phi = WorksheetFunction.Pi() / 3
    c.r = 5
    d = 10
    a = to_cart(c)
    b.x = 1: b.y = -2
    Debug.Print "addition             : ";: display a: Debug.Print "+";: display b
    Debug.Print "=";: display vector_addition(a, b): Debug.Print
    Debug.Print "subtraction          : ";: display a: Debug.Print "-";: display b
    Debug.Print "=";: display vector_subtraction(a, b): Debug.Print
    Debug.Print "scalar multiplication: ";: display a: Debug.Print " *";: Debug.Print d;
    Debug.Print "=";: display scalar_multiplication(a, d): Debug.Print
    Debug.Print "scalar division      : ";: display a: Debug.Print " /";: Debug.Print d;
    Debug.Print "=";: display scalar_division(a, d)
End Sub
Task: Alternade words


C++: #include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>

int main(int argc, char** argv) {
    const char* filename(argc < 2 ? "unixdict.txt" : argv[1]);
    std::ifstream in(filename);
    if (!in) {
        std::cerr << "Cannot open file '" << filename << "'.\n";
        return EXIT_FAILURE;
    }
    std::string line;
    std::set<std::string> dictionary;
    while (getline(in, line))
        dictionary.insert(line);
    std::cout << std::left;
    for (const std::string& word : dictionary) {
        size_t len = word.size();
        if (len < 6)
            continue;
        std::string word1, word2;
        for (size_t i = 0; i < len; i += 2) {
            word1 += word[i];
            if (i + 1 < len)
                word2 += word[i + 1];
        }
        if (dictionary.find(word1) != dictionary.end()
            && dictionary.find(word2) != dictionary.end()) {
            std::cout << std::setw(10) << word
                    << std::setw(6) << word1 << word2 << '\n';
        }
    }
    return EXIT_SUCCESS;
}



Go: package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "unicode/utf8"
)

func main() {
    b, err := ioutil.ReadFile("unixdict.txt")
    if err != nil {
        log.Fatal("Error reading file")
    }
    bwords := bytes.Fields(b)
    dict := make(map[string]bool, len(bwords))
    words := make([]string, len(bwords))
    for i, bword := range bwords {
        word := string(bword)
        dict[word] = true
        words[i] = word
    }
    fmt.Println("'unixdict.txt' contains the following alternades of length 6 or more:\n")
    count := 0
    for _, word := range words {
        if utf8.RuneCountInString(word) < 6 {
            continue
        }
        var w1 = ""
        var w2 = ""
        for i, c := range word {
            if i%2 == 0 {
                w1 += string(c)
            } else {
                w2 += string(c)
            }
        }
        _, ok1 := dict[w1]
        _, ok2 := dict[w2]
        if ok1 && ok2 {
            count++
            fmt.Printf("%2d: %-8s -> %-4s %-4s\n", count, word, w1, w2)
        }
    }
}