Created
June 11, 2014 16:36
-
-
Save mheap/9d6be84682fe5b0702f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func oddNumberGenerator(n int) []int { | |
vals := make([]int, n); | |
origN := n; | |
// In PHP I'd do while count($arr) < $n, but as we initialise | |
// an array of size n that won't work in go | |
for i := 0; n > 0; i++ { | |
if (i % 2 == 1) { | |
vals[(origN - n)] = i; | |
n--; | |
} | |
} | |
return vals; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@caius: Aye, that's easier to read than me. I was thinking more generally about the problem though.
e.g.: I need an array of N items that satisfy condition X. Odd numbers seemed like an easy condition to test with, but obviously that can be simplified to [i-1]+2, so doesn't really show off the whole condition based side of pushing into the array