Git Product home page Git Product logo

algorithmswithgo.com's People

Contributors

joncalhoun avatar mendelmaleh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithmswithgo.com's Issues

find_two_that_sum_test.go unexpected behaviour

So, I was in the FindTwoThatSum exercise and at first I though I was doing something wrong with my approach but after watching your solution and replicating it exactly, I found that the error persists for both, your and mine solution, which points for something in the go test file or something to do with the golang version I'm using (I'm on 1.14) maybe:

Algorithms tested:
Yours:

func FindTwoThatSum(numbers []int, sum int) (int, int) {
	for i, val := range numbers {
		for j, val2 := range numbers {
			if i == j {
				continue
			}
			if val+val2 == sum {
				return val, val2
			}
		}
	}
	return -1, -1
}

Mine:

func FindTwoThatSum(numbers []int, sum int) (int, int) {
	for i, val := range numbers[:len(numbers)-1] {
		for _, val2 := range numbers[i+1:] {
			if val+val2 == sum {
				return val, val2
			}
		}
	}
	return -1, -1
}

Error (both give the same FAIL message, varying a little regarding the testing.go line):

--- FAIL: TestFindTwoThatSum (0.00s)
    --- FAIL: TestFindTwoThatSum/[1_2_3_4]_with_sum_7 (0.00s)
panic: runtime error: index out of range [4] with length 4 [recovered]
        panic: runtime error: index out of range [4] with length 4

goroutine 7 [running]:
testing.tRunner.func1.1(0x536ca0, 0xc0000185c0)
        /usr/local/go/src/testing/testing.go:941 +0x3d0
testing.tRunner.func1(0xc000124240)
        /usr/local/go/src/testing/testing.go:944 +0x3f9
panic(0x536ca0, 0xc0000185c0)
        /usr/local/go/src/runtime/panic.go:967 +0x15d
algo/module01.TestFindTwoThatSum.func1(0xc000124240)
        /home/john/go/src/github.com/joncalhoun/algorithmswithgo.com/module01/find_two_that_sum_test.go:42 +0x46e
testing.tRunner(0xc000124240, 0xc00005e520)
        /usr/local/go/src/testing/testing.go:992 +0xdc
created by testing.(*T).Run
        /usr/local/go/src/testing/testing.go:1043 +0x357
exit status 2
FAIL    algo/module01   0.003s

Getting different results in std_demo

I'm using go1.16.4 in windows 10.

when I try:

go run main.go < input.txt
im getting

0
5
0
4

but according to the video, it should be:
5
4
11
33

im using the following GCD function:

for {
if b == 0 {
return a
}
a, b = b, a%b
}

all the test cases passed for that GCD function,

can someone explain to me, what is going on?

thanks,

slightly simpler BaseToDec

Here is a slightly simpler BaseToDec:

import "strings"

func BaseToDec(value string, base int) int {
	digits := "0123456789ABCDEF"
	dec := 0
	for _, chr := range value {
		dec = base*dec + strings.Index(digits, string(chr))
	}
	return dec
}

DecToBase etc. doesn't test conversion of 0

There's an important test case missing: DecToBase(0, ...). Same goes for BaseToDec.

In case of DecToBase(0, ...) an empty String is returned, which probably should be "0" instead.

I had to modify my recursive function - it now uses a helper function and duplicates some code:

const digits = "0123456789ABCDEF"

func _decToBase(dec, base int) string {
	if dec == 0 {
		return ""
	}
	digit := dec % base
	return _decToBase(dec/base, base) + string(digits[digit])
}

func DecToBase(dec, base int) string {
	digit := dec % base
	return _decToBase(dec/base, base) + string(digits[digit])
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.