Created
July 8, 2016 20:53
-
-
Save davidvthecoder/c37156f908a20275ab6196293f0674a7 to your computer and use it in GitHub Desktop.
code challenge fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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