joncalhoun / algorithmswithgo.com Goto Github PK
View Code? Open in Web Editor NEWCode for the Algorithms with Go course
Home Page: https://algorithmswithgo.com
Code for the Algorithms with Go course
Home Page: https://algorithmswithgo.com
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
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,
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
}
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])
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.