Skip to content

Instantly share code, notes, and snippets.

@davidvthecoder
Created July 8, 2016 20:53
Show Gist options
  • Save davidvthecoder/c37156f908a20275ab6196293f0674a7 to your computer and use it in GitHub Desktop.
Save davidvthecoder/c37156f908a20275ab6196293f0674a7 to your computer and use it in GitHub Desktop.
code challenge fun
package main
import "log"
func main() {
numbers := []int{23, 2, 4, 7, 2, 11}
find := 20 // should return true
// numbers := []int{1, 3, 5, 23, 2}
// find := 8 // should return true
//numbers := []int{1, 3, 5, 23, 2}
//find := 7 // should return false
log.Println(AddUp(numbers, find))
}
func AddUp(numbers []int, total int) bool {
var sum = 0
var start = 0
var stop = 0
for true { // loop continiously until match is found or until all outcomes have been exhausted
if sum < total {
if stop >= len(numbers) {
return false
}
sum += numbers[stop]
stop++
} else if sum > total {
sum -= numbers[start]
start++
} else if sum == total {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment