Last active
April 13, 2021 10:32
-
-
Save shalomb/fbd8b0efaa51537cc4545b3dd960e4b8 to your computer and use it in GitHub Desktop.
Learning go the fizzybuzzy way
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 | |
// [Fizzbuzz: One Simple Interview Question](https://www.youtube.com/watch?v=QPZ0pIK_wsc) | |
import "fmt" | |
func fizzbuzz(i int) (text string) { | |
fizz := i%3 == 0 | |
buzz := i%5 == 0 | |
if fizz && buzz { | |
return "fizzbuzz" | |
} else if fizz { | |
return "fizz" | |
} else if buzz { | |
return "buzz" | |
} else { | |
return fmt.Sprintf("%d", i) | |
} | |
} | |
func main() { | |
for i := 0; i <= 35; i++ { | |
fmt.Printf("%s\n", fizzbuzz(i)) | |
} | |
} |
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 | |
// [Fizzbuzz: One Simple Interview Question](https://www.youtube.com/watch?v=QPZ0pIK_wsc) | |
import "fmt" | |
func fizzbuzz(i int) (trigger bool, text string) { | |
fizz := i%3 == 0 | |
buzz := i%5 == 0 | |
if fizz && buzz { | |
text = "fizzbuzz" | |
} else if fizz { | |
text = "fizz" | |
} else if buzz { | |
text = "buzz" | |
} | |
return fizz || buzz, text | |
} | |
func main() { | |
for i := 0; i <= 100; i++ { | |
trigger, text := fizzbuzz(i) // :| can't be used as a value in if ... {} | |
if trigger { | |
fmt.Println(text) | |
} else { | |
fmt.Println(i) | |
} | |
} | |
} |
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 "fmt" | |
// [Fizzbuzz: One Simple Interview Question](https://www.youtube.com/watch?v=QPZ0pIK_wsc) | |
type FizzyBuzzy int | |
func (b FizzyBuzzy) String() (text string) { | |
fizz := b%3 == 0 | |
buzz := b%5 == 0 | |
if buzz && fizz { | |
return "fizzbuzz" | |
} else if fizz { | |
return "fizz" | |
} else if buzz { | |
return "buzz" | |
} else { | |
return fmt.Sprintf("%d", b) | |
} | |
} | |
func main() { | |
for i := 0; i <= 36; i++ { | |
fmt.Printf("%v\n", FizzyBuzzy(i)) | |
} | |
} |
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 | |
// [Fizzbuzz: One Simple FizzyInterview Question](https://www.youtube.com/watch?v=QPZ0pIK_wsc) | |
import ( | |
"fmt" | |
) | |
type FizzyBuzzy interface { | |
Analyze(v int) | |
Value() int | |
String() string // explicitly required | |
} | |
type EvenInt struct { | |
value int | |
repr string | |
} | |
type FizzyInt struct { | |
value int | |
repr string | |
fizz bool | |
buzz bool | |
} | |
// Analyze an even integer | |
func (i *EvenInt) Analyze(v int) { | |
i.value = v | |
i.repr = "even" | |
} | |
// Analyze a number and determine its fizzy/buzzy-ness | |
func (i *FizzyInt) Analyze(v int) { | |
i.value = v | |
i.fizz = i.value%3 == 0 | |
i.buzz = i.value%5 == 0 | |
if i.fizz && i.buzz { | |
i.repr = "fizzbuzz" | |
} else if i.fizz { | |
i.repr = "fizz" | |
} else if i.buzz { | |
i.repr = "buzz" | |
} else { | |
// if we've ruled out even numbers | |
// this number is then a candidate prime number? | |
// due to the inadvertent Sieve of Eratosthenes | |
// off course, doesn't catch the fizzy primes | |
i.repr = "prime?" | |
} | |
} | |
// candidate for generics? | |
func (fi EvenInt) String() (text string) { | |
return fi.repr | |
} | |
// candidate for generics? | |
func (fi FizzyInt) String() (text string) { | |
return fi.repr | |
} | |
// candidate for generics? | |
func (fi EvenInt) Value() int { | |
return fi.value | |
} | |
// candidate for generics? | |
func (fi FizzyInt) Value() int { | |
return fi.value | |
} | |
// interface method | |
func repr(fb FizzyBuzzy) (text string) { | |
return fmt.Sprintf("%2d is %s", fb.Value(), fb.String()) | |
} | |
func main() { | |
for i := 0; i <= 36; i++ { | |
var fb FizzyBuzzy | |
if i%2 == 0 { | |
fb = new(EvenInt) | |
} else { | |
fb = new(FizzyInt) | |
} | |
fb.Analyze(i) | |
fmt.Printf("%v\n", repr(fb)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment