Last active
August 29, 2015 14:14
-
-
Save sbrady/cc4d973346627b804f2c to your computer and use it in GitHub Desktop.
The evolution of a FizzBuzz in Golang
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
//Print the numbers 1..100 | |
//For multiples of 3, print "Fizz" instead of the number | |
//For multiples of 5, print "Buzz" instead of the number | |
//For multiples of 3 and 5, print "FizzBuzz" instead of the number | |
package main | |
import "fmt" | |
import "strconv" | |
func main() { | |
for i := FizzBuzz(1); i <= 100; i++ { | |
fmt.Println(i.GetPrintable()); | |
} | |
} | |
type FizzBuzz int | |
func (fb FizzBuzz) GetPrintable() string { | |
if fb.getFizzBuzz() != "" { | |
return fb.getFizzBuzz() | |
} | |
return fb.printableNumber() | |
} | |
func (fb FizzBuzz)printableNumber() string { | |
return strconv.Itoa(int(fb)) | |
} | |
func (fb FizzBuzz)getFizzBuzz() string { | |
return fb.getFizz() + fb.getBuzz() | |
} | |
func (fb FizzBuzz)getBuzz() string { | |
if (fb.isBuzzable()) { | |
return "Buzz" | |
} | |
return "" | |
} | |
func (fb FizzBuzz)getFizz() string { | |
if (fb.isFizzable()) { | |
return "Fizz" | |
} | |
return "" | |
} | |
func (fb FizzBuzz)isFizzable() bool { | |
return fb.isMultipleOf(3); | |
} | |
func (fb FizzBuzz)isBuzzable() bool { | |
return fb.isMultipleOf(5); | |
} | |
func (fb FizzBuzz)isMultipleOf(multiple FizzBuzz) bool { | |
return 0 == fb % multiple | |
} |
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
//Changes | |
//Print the numbers 1..10000 | |
//For multiples of 3, print "Fizz" instead of the number | |
//For multiples of 5, print "Buzz" instead of the number | |
//For multiples of 7, print "Baz" instead of the number | |
//For multiples of 3, 5 and 7, print "FizzBuzzBaz" instead of the number | |
//other combinations should not be printed | |
package main | |
import "fmt" | |
import "strconv" | |
func main() { | |
for i := FizzBuzz(1); i <= 10000; i++ { | |
fmt.Println(i.GetPrintable()); | |
} | |
} | |
type FizzBuzz int | |
func (fb FizzBuzz) GetPrintable() string { | |
if word:= fb.getPrintableWord(); word != "" { | |
return word | |
} | |
return fb.printableNumber() | |
} | |
func (fb FizzBuzz)printableNumber() string { | |
return strconv.Itoa(int(fb)) | |
} | |
func (fb FizzBuzz)getPrintableWord() (fullWord string) { | |
foundWords := allWords.findAllByMultiple(fb) | |
if len(foundWords) == 1 { | |
return foundWords[0].value | |
} | |
if foundWords.hasAllWords() { | |
return foundWords.joinAllValues() | |
} | |
return | |
} | |
func (fb FizzBuzz)isMultipleOf(multiple int) bool { | |
return 0 == fb % FizzBuzz(multiple) | |
} | |
type Words []Word | |
type Word struct { | |
value string | |
multipleOf int | |
} | |
var allWords = Words{ | |
Word{value: "Fizz", multipleOf: 3}, | |
Word{value: "Buzz", multipleOf: 5}, | |
Word{value: "Baz", multipleOf: 7}, | |
} | |
func (words Words) findAllByMultiple(multiple FizzBuzz) (foundWords Words) { | |
for _,word := range words { | |
if multiple.isMultipleOf(word.multipleOf) { | |
foundWords = append(foundWords,word) | |
} | |
} | |
return | |
} | |
func(words Words) joinAllValues() (fullWord string){ | |
for _,word := range words { | |
fullWord = fullWord + word.value | |
} | |
return | |
} | |
func(words Words) hasAllWords() bool{ | |
return len(words) == len(allWords) | |
} |
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
//Changes | |
//Print the numbers 1..100000 | |
//For multiples of 3, print "Fizz" instead of the number | |
//For multiples of 5, print "Buzz" instead of the number | |
//For multiples of 7, print "Baz" instead of the number | |
//For multiples of 11, print "Foo" instead of the number | |
//For multiples of 15, print "Bar" instead of the number | |
//For multiples of 19, print "Bam" instead of the number | |
//For multiples of 3,5,7,11,15,19 print "FizzBuzzBazFooBarBam" instead of the number | |
//other combinations should not be printed | |
package main | |
import "fmt" | |
import "strconv" | |
const numberOfPrintOuts = 100000 | |
func main() { | |
for i := FizzBuzz(1); i <= numberOfPrintOuts; i++ { | |
fmt.Println(i.GetPrintable()); | |
} | |
} | |
type FizzBuzz int | |
func (fb FizzBuzz) GetPrintable() string { | |
if word:= fb.getPrintableWord(); word != "" { | |
return word | |
} | |
return fb.printableNumber() | |
} | |
func (fb FizzBuzz)printableNumber() string { | |
return strconv.Itoa(int(fb)) | |
} | |
func (fb FizzBuzz)getPrintableWord() (fullWord string) { | |
foundWords := allWords.findAllByMultiple(fb) | |
if len(foundWords) == 1 { | |
return foundWords[0].value | |
} | |
if foundWords.hasAllWords() { | |
return foundWords.joinAllValues() | |
} | |
return | |
} | |
func (fb FizzBuzz)isMultipleOf(multiple int) bool { | |
return 0 == fb % FizzBuzz(multiple) | |
} | |
type Words []Word | |
type Word struct { | |
value string | |
multipleOf int | |
} | |
var allWords = Words{ | |
Word{value: "Fizz", multipleOf: 3}, | |
Word{value: "Buzz", multipleOf: 5}, | |
Word{value: "Baz", multipleOf: 7}, | |
Word{value: "Foo", multipleOf: 11}, | |
Word{value: "Bar", multipleOf: 15}, | |
Word{value: "Bam", multipleOf: 19}, | |
} | |
func (words Words) findAllByMultiple(multiple FizzBuzz) (foundWords Words) { | |
for _,word := range words { | |
if multiple.isMultipleOf(word.multipleOf) { | |
foundWords = append(foundWords,word) | |
} | |
} | |
return | |
} | |
func(words Words) joinAllValues() (fullWord string){ | |
for _,word := range words { | |
fullWord = fullWord + word.value | |
} | |
return | |
} | |
func(words Words) hasAllWords() bool{ | |
return len(words) == len(allWords) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment