WeixiangYan/CodeTransOcean huggingface dataset:
Task: Decimal floating point number to binary




Go: package main

import (
    "fmt"
    "math"
    "strconv"
    "strings"
)

func decToBin(d float64) string {
    whole := int64(math.Floor(d))
    binary := strconv.FormatInt(whole, 2) + "."
    dd := d - float64(whole)
    for dd > 0.0 {
        r := dd * 2.0
        if r >= 1.0 {
            binary += "1"
            dd = r - 1.0
        } else {
            binary += "0"
            dd = r
        }
    }
    return binary
}

func binToDec(s string) float64 {
    ss := strings.Replace(s, ".", "", 1)
    num, _ := strconv.ParseInt(ss, 2, 64)
    ss = strings.Split(s, ".")[1]
    ss = strings.Replace(ss, "1", "0", -1)
    den, _ := strconv.ParseInt("1" + ss, 2, 64)
    return float64(num) / float64(den)
}

func main() {
    f := 23.34375
    fmt.Printf("%v\t => %s\n", f, decToBin(f))
    s := "1011.11101"
    fmt.Printf("%s\t => %v\n", s, binToDec(s))
}
Task: Natural sorting




Go: package main

import (
    "fmt"
    "regexp"
    "sort"
    "strconv"
    "strings"
)

var tests = []struct {
    descr string
    list  []string
}{
    {"Ignoring leading spaces", []string{
        "ignore leading spaces: 2-2",
        " ignore leading spaces: 2-1",
        "  ignore leading spaces: 2+0",
        "   ignore leading spaces: 2+1",
    }},
    {"Ignoring multiple adjacent spaces", []string{
        "ignore m.a.s spaces: 2-2",
        "ignore m.a.s  spaces: 2-1",
        "ignore m.a.s   spaces: 2+0",
        "ignore m.a.s    spaces: 2+1",
    }},
    {"Equivalent whitespace characters", []string{
        "Equiv. spaces: 3-3",
        "Equiv.\rspaces: 3-2",
        "Equiv.\fspaces: 3-1",
        "Equiv.\bspaces: 3+0",
        "Equiv.\nspaces: 3+1",
        "Equiv.\tspaces: 3+2",
    }},
    {"Case Indepenent sort", []string{
        "cASE INDEPENENT: 3-2",
        "caSE INDEPENENT: 3-1",
        "casE INDEPENENT: 3+0",
        "case INDEPENENT: 3+1",
    }},
    {"Numeric fields as numerics", []string{
        "foo100bar99baz0.txt",
        "foo100bar10baz0.txt",
        "foo1000bar99baz10.txt",
        "foo1000bar99baz9.txt",
    }},
}

func main() {
    for _, test := range tests {
        fmt.Println(test.descr)
        fmt.Println("Input order:")
        for _, s := range test.list {
            fmt.Printf("   %q\n", s)
        }
        fmt.Println("Natural order:")
        l := make(list, len(test.list))
        for i, s := range test.list {
            l[i] = newNatStr(s)
        }
        sort.Sort(l)
        for _, s := range l {
            fmt.Printf("   %q\n", s.s)
        }
        fmt.Println()
    }
}


type natStr struct {
    s string 
    t []tok  
}

func newNatStr(s string) (t natStr) {
    t.s = s
    s = strings.ToLower(strings.Join(strings.Fields(s), " "))
    x := dx.FindAllString(s, -1)
    t.t = make([]tok, len(x))
    for i, s := range x {
        if n, err := strconv.Atoi(s); err == nil {
            t.t[i].n = n
        } else {
            t.t[i].s = s
        }
    }
    return t
}

var dx = regexp.MustCompile(`\d+|\D+`)


type tok struct {
    s string
    n int
}


func (f1 tok) Cmp(f2 tok) int {
    switch {
    case f1.s == "":
        switch {
        case f2.s > "" || f1.n < f2.n:
            return -1
        case f1.n > f2.n:
            return 1
        }
    case f2.s == "" || f1.s > f2.s:
        return 1
    case f1.s < f2.s:
        return -1
    }
    return 0
}

type list []natStr

func (l list) Len() int      { return len(l) }
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l list) Less(i, j int) bool {
    ti := l[i].t
    for k, t := range l[j].t {
        if k == len(ti) {
            return true
        }
        switch ti[k].Cmp(t) {
        case -1:
            return true
        case 1:
            return false
        }
    }
    return false
}
Task: Canny edge detector




Go: package main

import (
    ed "github.com/Ernyoke/Imger/edgedetection"
    "github.com/Ernyoke/Imger/imgio"
    "log"
)

func main() {
    img, err := imgio.ImreadRGBA("Valve_original_(1).png")
    if err != nil {
        log.Fatal("Could not read image", err)
    }

    cny, err := ed.CannyRGBA(img, 15, 45, 5)
    if err != nil {
        log.Fatal("Could not perform Canny Edge detection")
    }

    err = imgio.Imwrite(cny, "Valve_canny_(1).png")
    if err != nil {
        log.Fatal("Could not write Canny image to disk")
    }
}
Task: OpenWebNet password




Go: package main

import (
    "fmt"
    "strconv"
)

func ownCalcPass(password, nonce string) uint32 {
    start := true
    num1 := uint32(0)
    num2 := num1
    i, _ := strconv.Atoi(password)
    pwd := uint32(i)
    for _, c := range nonce {
        if c != '0' {
            if start {
                num2 = pwd
            }
            start = false
        }
        switch c {
        case '1':
            num1 = (num2 & 0xFFFFFF80) >> 7
            num2 = num2 << 25
        case '2':
            num1 = (num2 & 0xFFFFFFF0) >> 4
            num2 = num2 << 28
        case '3':
            num1 = (num2 & 0xFFFFFFF8) >> 3
            num2 = num2 << 29
        case '4':
            num1 = num2 << 1
            num2 = num2 >> 31
        case '5':
            num1 = num2 << 5
            num2 = num2 >> 27
        case '6':
            num1 = num2 << 12
            num2 = num2 >> 20
        case '7':
            num3 := num2 & 0x0000FF00
            num4 := ((num2 & 0x000000FF) << 24) | ((num2 & 0x00FF0000) >> 16)
            num1 = num3 | num4
            num2 = (num2 & 0xFF000000) >> 8
        case '8':
            num1 = (num2&0x0000FFFF)<<16 | (num2 >> 24)
            num2 = (num2 & 0x00FF0000) >> 8
        case '9':
            num1 = ^num2
        default:
            num1 = num2
        }

        num1 &= 0xFFFFFFFF
        num2 &= 0xFFFFFFFF
        if c != '0' && c != '9' {
            num1 |= num2
        }
        num2 = num1
    }
    return num1
}

func testPasswordCalc(password, nonce string, expected uint32) {
    res := ownCalcPass(password, nonce)
    m := fmt.Sprintf("%s  %s  %-10d  %-10d", password, nonce, res, expected)
    if res == expected {
        fmt.Println("PASS", m)
    } else {
        fmt.Println("FAIL", m)
    }
}

func main() {
    testPasswordCalc("12345", "603356072", 25280520)
    testPasswordCalc("12345", "410501656", 119537670)
    testPasswordCalc("12345", "630292165", 4269684735)
}
Task: Solve triangle solitare puzzle




Go: package main

import "fmt"

type solution struct{ peg, over, land int }

type move struct{ from, to int }

var emptyStart = 1

var board [16]bool

var jumpMoves = [16][]move{
    {},
    {{2, 4}, {3, 6}},
    {{4, 7}, {5, 9}},
    {{5, 8}, {6, 10}},
    {{2, 1}, {5, 6}, {7, 11}, {8, 13}},
    {{8, 12}, {9, 14}},
    {{3, 1}, {5, 4}, {9, 13}, {10, 15}},
    {{4, 2}, {8, 9}},
    {{5, 3}, {9, 10}},
    {{5, 2}, {8, 7}},
    {{9, 8}},
    {{12, 13}},
    {{8, 5}, {13, 14}},
    {{8, 4}, {9, 6}, {12, 11}, {14, 15}},
    {{9, 5}, {13, 12}},
    {{10, 6}, {14, 13}},
}

var solutions []solution

func initBoard() {
    for i := 1; i < 16; i++ {
        board[i] = true
    }
    board[emptyStart] = false
}

func (sol solution) split() (int, int, int) {
    return sol.peg, sol.over, sol.land
}

func (mv move) split() (int, int) {
    return mv.from, mv.to
}

func drawBoard() {
    var pegs [16]byte
    for i := 1; i < 16; i++ {
        if board[i] {
            pegs[i] = fmt.Sprintf("%X", i)[0]
        } else {
            pegs[i] = '-'
        }
    }
    fmt.Printf("       %c\n", pegs[1])
    fmt.Printf("      %c %c\n", pegs[2], pegs[3])
    fmt.Printf("     %c %c %c\n", pegs[4], pegs[5], pegs[6])
    fmt.Printf("    %c %c %c %c\n", pegs[7], pegs[8], pegs[9], pegs[10])
    fmt.Printf("   %c %c %c %c %c\n", pegs[11], pegs[12], pegs[13], pegs[14], pegs[15])
}

func solved() bool {
    count := 0
    for _, b := range board {
        if b {
            count++
        }
    }
    return count == 1 
}

func solve() {
    if solved() {
        return
    }
    for peg := 1; peg < 16; peg++ {
        if board[peg] {
            for _, mv := range jumpMoves[peg] {
                over, land := mv.split()
                if board[over] && !board[land] {
                    saveBoard := board
                    board[peg] = false
                    board[over] = false
                    board[land] = true
                    solutions = append(solutions, solution{peg, over, land})
                    solve()
                    if solved() {
                        return 
                    }
                    board = saveBoard
                    solutions = solutions[:len(solutions)-1]
                }
            }
        }
    }
}

func main() {
    initBoard()
    solve()
    initBoard()
    drawBoard()
    fmt.Printf("Starting with peg %X removed\n\n", emptyStart)
    for _, solution := range solutions {
        peg, over, land := solution.split()
        board[peg] = false
        board[over] = false
        board[land] = true
        drawBoard()
        fmt.Printf("Peg %X jumped over %X to land on %X\n\n", peg, over, land)
    }
}
Task: SHA-256 Merkle tree




Go: package main

import (
    "crypto/sha256"
    "fmt"
    "io"
    "log"
    "os"
)

func main() {
    const blockSize = 1024
    f, err := os.Open("title.png")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    var hashes [][]byte
    buffer := make([]byte, blockSize)
    h := sha256.New()
    for {
        bytesRead, err := f.Read(buffer)
        if err != nil {
            if err != io.EOF {
                log.Fatal(err)
            }
            break
        }
        h.Reset()
        h.Write(buffer[:bytesRead])
        hashes = append(hashes, h.Sum(nil))
    }
    buffer = make([]byte, 64)
    for len(hashes) > 1 {
        var hashes2 [][]byte
        for i := 0; i < len(hashes); i += 2 {
            if i < len(hashes)-1 {                
                copy(buffer, hashes[i])
                copy(buffer[32:], hashes[i+1])
                h.Reset()
                h.Write(buffer)
                hashes2 = append(hashes2, h.Sum(nil))
            } else {
                hashes2 = append(hashes2, hashes[i])
            }
        }
        hashes = hashes2
    }
    fmt.Printf("%x", hashes[0])
    fmt.Println()
}
Task: Audio overlap loop




Go: package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
    "os/exec"
    "strconv"
)

func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    fileName := "loop.wav"
    scanner := bufio.NewScanner(os.Stdin)
    reps := 0
    for reps < 1 || reps > 6 {
        fmt.Print("Enter number of repetitions (1 to 6) : ")
        scanner.Scan()
        input := scanner.Text()
        check(scanner.Err())
        reps, _ = strconv.Atoi(input)
    }

    delay := 0
    for delay < 50 || delay > 500 {
        fmt.Print("Enter delay between repetitions in microseconds (50 to 500) : ")
        scanner.Scan()
        input := scanner.Text()
        check(scanner.Err())
        delay, _ = strconv.Atoi(input)
    }

    decay := 0.0
    for decay < 0.2 || decay > 0.9 {
        fmt.Print("Enter decay between repetitions (0.2 to 0.9) : ")
        scanner.Scan()
        input := scanner.Text()
        check(scanner.Err())
        decay, _ = strconv.ParseFloat(input, 64)
    }

    args := []string{fileName, "echo", "0.8", "0.7"}
    decay2 := 1.0    
    for i := 1; i <= reps; i++ {
        delayStr := strconv.Itoa(i * delay)
        decay2 *= decay
        decayStr := strconv.FormatFloat(decay2, 'f', -1, 64)        
        args = append(args, delayStr, decayStr)        
    }
    cmd := exec.Command("play", args...)    
    err := cmd.Run()
    check(err)
}
Task: Untouchable numbers




Go: package main

import "fmt"

func sumDivisors(n int) int {
    sum := 1
    k := 2
    if n%2 == 0 {
        k = 1
    }
    for i := 1 + k; i*i <= n; i += k {
        if n%i == 0 {
            sum += i
            j := n / i
            if j != i {
                sum += j
            }
        }
    }
    return sum
}

func sieve(n int) []bool {
    n++
    s := make([]bool, n+1) 
    for i := 6; i <= n; i++ {
        sd := sumDivisors(i)
        if sd <= n {
            s[sd] = true
        }
    }
    return s
}

func primeSieve(limit int) []bool {
    limit++
    
    c := make([]bool, limit) 
    c[0] = true
    c[1] = true
    
    p := 3 
    for {
        p2 := p * p
        if p2 >= limit {
            break
        }
        for i := p2; i < limit; i += 2 * p {
            c[i] = true
        }
        for {
            p += 2
            if !c[p] {
                break
            }
        }
    }
    return c
}

func commatize(n int) string {
    s := fmt.Sprintf("%d", n)
    if n < 0 {
        s = s[1:]
    }
    le := len(s)
    for i := le - 3; i >= 1; i -= 3 {
        s = s[0:i] + "," + s[i:]
    }
    if n >= 0 {
        return s
    }
    return "-" + s
}

func main() {    
    limit := 1000000
    c := primeSieve(limit)
    s := sieve(63 * limit)
    untouchable := []int{2, 5}
    for n := 6; n <= limit; n += 2 {
        if !s[n] && c[n-1] && c[n-3] {
            untouchable = append(untouchable, n)
        }
    }
    fmt.Println("List of untouchable numbers <= 2,000:")
    count := 0
    for i := 0; untouchable[i] <= 2000; i++ {
        fmt.Printf("%6s", commatize(untouchable[i]))
        if (i+1)%10 == 0 {
            fmt.Println()
        }
        count++
    }
    fmt.Printf("\n\n%7s untouchable numbers were found  <=     2,000\n", commatize(count))
    p := 10
    count = 0
    for _, n := range untouchable {
        count++
        if n > p {
            cc := commatize(count - 1)
            cp := commatize(p)
            fmt.Printf("%7s untouchable numbers were found  <= %9s\n", cc, cp)
            p = p * 10
            if p == limit {
                break
            }
        }
    }
    cu := commatize(len(untouchable))
    cl := commatize(limit)
    fmt.Printf("%7s untouchable numbers were found  <= %s\n", cu, cl)
}
Task: Partition function P




Go: package main

import (
    "fmt"
    "math/big"
    "time"
)

var p []*big.Int
var pd []int

func partDiffDiff(n int) int {
    if n&1 == 1 {
        return (n + 1) / 2
    }
    return n + 1
}

func partDiff(n int) int {
    if n < 2 {
        return 1
    }
    pd[n] = pd[n-1] + partDiffDiff(n-1)
    return pd[n]
}

func partitionsP(n int) {
    if n < 2 {
        return
    }
    psum := new(big.Int)
    for i := 1; i <= n; i++ {
        pdi := partDiff(i)
        if pdi > n {
            break
        }
        sign := int64(-1)
        if (i-1)%4 < 2 {
            sign = 1
        }
        t := new(big.Int).Mul(p[n-pdi], big.NewInt(sign))
        psum.Add(psum, t)
    }
    p[n] = psum
}

func main() {
    start := time.Now()
    const N = 6666
    p = make([]*big.Int, N+1)
    pd = make([]int, N+1)
    p[0], pd[0] = big.NewInt(1), 1
    p[1], pd[1] = big.NewInt(1), 1
    for n := 2; n <= N; n++ {
        partitionsP(n)
    }
    fmt.Printf("p[%d)] = %d\n", N, p[N])
    fmt.Printf("Took %s\n", time.Since(start))
}
Task: Erdős-Nicolas numbers




Go: package main

import "fmt"

func main() {
	const maxNumber = 100000000
	dsum := make([]int, maxNumber+1)
	dcount := make([]int, maxNumber+1)
	for i := 0; i <= maxNumber; i++ {
		dsum[i] = 1
		dcount[i] = 1
	}
	for i := 2; i <= maxNumber; i++ {
		for j := i + i; j <= maxNumber; j += i {
			if dsum[j] == j {
				fmt.Printf("%8d equals the sum of its first %d divisors\n", j, dcount[j])
			}
			dsum[j] += i
			dcount[j]++
		}
	}
}
Task: Special divisors




Go: package main

import (
    "fmt"
    "rcu"
)

func reversed(n int) int {
    rev := 0
    for n > 0 {
        rev = rev*10 + n%10
        n = n / 10
    }
    return rev
}

func main() {
    var special []int
    for n := 1; n < 200; n++ {
        divs := rcu.Divisors(n)
        revN := reversed(n)
        all := true
        for _, d := range divs {
            if revN%reversed(d) != 0 {
                all = false
                break
            }
        }
        if all {
            special = append(special, n)
        }
    }
    fmt.Println("Special divisors in the range 0..199:")
    for i, n := range special {
        fmt.Printf("%3d ", n)
        if (i+1)%12 == 0 {
            fmt.Println()
        }
    }
    fmt.Printf("\n%d special divisors found.\n", len(special))
}
Task: Nimber arithmetic




Go: package main

import (
    "fmt"
    "strings"
)


func hpo2(n uint) uint { return n & (-n) }


func lhpo2(n uint) uint {
    q := uint(0)
    m := hpo2(n)
    for m%2 == 0 {
        m = m >> 1
        q++
    }
    return q
}


func nimsum(x, y uint) uint { return x ^ y }


func nimprod(x, y uint) uint {
    if x < 2 || y < 2 {
        return x * y
    }
    h := hpo2(x)
    if x > h {
        return nimprod(h, y) ^ nimprod(x^h, y) 
    }
    if hpo2(y) < y {
        return nimprod(y, x) 
    }
    xp, yp := lhpo2(x), lhpo2(y)
    comp := xp & yp
    if comp == 0 {
        return x * y 
    }
    h = hpo2(comp)
    
    return nimprod(nimprod(x>>h, y>>h), 3<<(h-1))
}

type fnop struct {
    fn func(x, y uint) uint
    op string
}

func main() {
    for _, f := range []fnop{{nimsum, "+"}, {nimprod, "*"}} {
        fmt.Printf(" %s |", f.op)
        for i := 0; i <= 15; i++ {
            fmt.Printf("%3d", i)
        }
        fmt.Println("\n--- " + strings.Repeat("-", 48))
        for i := uint(0); i <= 15; i++ {
            fmt.Printf("%2d |", i)
            for j := uint(0); j <= 15; j++ {
                fmt.Printf("%3d", f.fn(i, j))
            }
            fmt.Println()
        }
        fmt.Println()
    }

    a := uint(21508)
    b := uint(42689)
    fmt.Printf("%d + %d = %d\n", a, b, nimsum(a, b))
    fmt.Printf("%d * %d = %d\n", a, b, nimprod(a, b))
}
Task: Double Twin Primes




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    p := rcu.Primes(1000)
    fmt.Println("Double twin primes under 1,000:")
    for i := 1; i < len(p)-3; i++ {
        if p[i+1]-p[i] == 2 && p[i+2]-p[i+1] == 4 && p[i+3]-p[i+2] == 2 {
            fmt.Printf("%4d\n", p[i:i+4])
        }
    }
}
Task: De Polignac numbers




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    pows2 := make([]int, 20)
    for i := 0; i < 20; i++ {
        pows2[i] = 1 << i
    }
    dp := []int{1}
    dp1000, dp10000 := 0, 0
    for n, count := 3, 1; count < 10000; n += 2 {
        found := false
        for _, pow := range pows2 {
            if pow > n {
                break
            }
            if rcu.IsPrime(n - pow) {
                found = true
                break
            }
        }
        if !found {
            count++
            if count <= 50 {
                dp = append(dp, n)
            } else if count == 1000 {
                dp1000 = n
            } else if count == 10000 {
                dp10000 = n
            }
        }
    }
    fmt.Println("First 50 De Polignac numbers:")
    rcu.PrintTable(dp, 10, 5, true)
    fmt.Printf("\nOne thousandth: %s\n", rcu.Commatize(dp1000))
    fmt.Printf("\nTen thousandth: %s\n", rcu.Commatize(dp10000))
}
Task: Polynomial synthetic division




Go: package main

import (
    "fmt"
    "math/big"
)

func div(dividend, divisor []*big.Rat) (quotient, remainder []*big.Rat) {
    out := make([]*big.Rat, len(dividend))
    for i, c := range dividend {
        out[i] = new(big.Rat).Set(c)
    }
    for i := 0; i < len(dividend)-(len(divisor)-1); i++ {
        out[i].Quo(out[i], divisor[0])
        if coef := out[i]; coef.Sign() != 0 {
            var a big.Rat
            for j := 1; j < len(divisor); j++ {
                out[i+j].Add(out[i+j], a.Mul(a.Neg(divisor[j]), coef))
            }
        }
    }
    separator := len(out) - (len(divisor) - 1)
    return out[:separator], out[separator:]
}

func main() {
    N := []*big.Rat{
        big.NewRat(1, 1),
        big.NewRat(-12, 1),
        big.NewRat(0, 1),
        big.NewRat(-42, 1)}
    D := []*big.Rat{big.NewRat(1, 1), big.NewRat(-3, 1)}
    Q, R := div(N, D)
    fmt.Printf("%v / %v = %v remainder %v\n", N, D, Q, R)
}
Task: Color wheel




Go: package main

import (
    "github.com/fogleman/gg"
    "math"
)

const tau = 2 * math.Pi

func hsb2rgb(hue, sat, bri float64) (r, g, b int) {
    u := int(bri*255 + 0.5)
    if sat == 0 {
        r, g, b = u, u, u
    } else {
        h := (hue - math.Floor(hue)) * 6
        f := h - math.Floor(h)
        p := int(bri*(1-sat)*255 + 0.5)
        q := int(bri*(1-sat*f)*255 + 0.5)
        t := int(bri*(1-sat*(1-f))*255 + 0.5)
        switch int(h) {
        case 0:
            r, g, b = u, t, p
        case 1:
            r, g, b = q, u, p
        case 2:
            r, g, b = p, u, t
        case 3:
            r, g, b = p, q, u
        case 4:
            r, g, b = t, p, u
        case 5:
            r, g, b = u, p, q
        }
    }
    return
}

func colorWheel(dc *gg.Context) {
    width, height := dc.Width(), dc.Height()
    centerX, centerY := width/2, height/2
    radius := centerX
    if centerY < radius {
        radius = centerY
    }
    for y := 0; y < height; y++ {
        dy := float64(y - centerY)
        for x := 0; x < width; x++ {
            dx := float64(x - centerX)
            dist := math.Sqrt(dx*dx + dy*dy)
            if dist <= float64(radius) {
                theta := math.Atan2(dy, dx)
                hue := (theta + math.Pi) / tau
                r, g, b := hsb2rgb(hue, 1, 1)
                dc.SetRGB255(r, g, b)
                dc.SetPixel(x, y)
            }
        }
    }
}

func main() {
    const width, height = 480, 480
    dc := gg.NewContext(width, height)
    dc.SetRGB(1, 1, 1) 
    dc.Clear()
    colorWheel(dc)
    dc.SavePNG("color_wheel.png")
}
Task: Text completion




Go: package main

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

func levenshtein(s, t string) int {
    d := make([][]int, len(s)+1)
    for i := range d {
        d[i] = make([]int, len(t)+1)
    }
    for i := range d {
        d[i][0] = i
    }
    for j := range d[0] {
        d[0][j] = j
    }
    for j := 1; j <= len(t); j++ {
        for i := 1; i <= len(s); i++ {
            if s[i-1] == t[j-1] {
                d[i][j] = d[i-1][j-1]
            } else {
                min := d[i-1][j]
                if d[i][j-1] < min {
                    min = d[i][j-1]
                }
                if d[i-1][j-1] < min {
                    min = d[i-1][j-1]
                }
                d[i][j] = min + 1
            }
        }

    }
    return d[len(s)][len(t)]
}

func main() {
    search := "complition"
    b, err := ioutil.ReadFile("unixdict.txt")
    if err != nil {
        log.Fatal("Error reading file")
    }
    words := bytes.Fields(b)
    var lev [4][]string
    for _, word := range words {
        s := string(word)
        ld := levenshtein(search, s)
        if ld < 4 {
            lev[ld] = append(lev[ld], s)
        }
    }
    fmt.Printf("Input word: %s\n\n", search)
    for i := 1; i < 4; i++ {
        length := float64(len(search))
        similarity := (length - float64(i)) * 100 / length
        fmt.Printf("Words which are %4.1f%% similar:\n", similarity)
        fmt.Println(lev[i])
        fmt.Println()
    }
}