diff --git a/ex-basics/ex-fizzbuzz.tex b/ex-basics/ex-fizzbuzz.tex new file mode 100644 index 0000000..4e7468b --- /dev/null +++ b/ex-basics/ex-fizzbuzz.tex @@ -0,0 +1,17 @@ +\begin{Exercise}[title={FizzBuzz},difficulty=1] +\label{ex:fizzbuzz} +\Question \label{ex:fizzbuzz q1} Solve this simple problem called +the Fizz-Buzz \cite{fizzbuzz} problem: +\begin{quote} +Write a program that prints the numbers from 1 to 100. But for multiples +of three print "Fizz" instead of the number and for the multiples of +five print "Buzz". For numbers which are multiples of both three and +five print "FizzBuzz". +\end{quote} +\end{Exercise} + +\begin{Answer} +\Question The hard part was probably writing the thing in Go. A possible +solution to this simple problem is the following program. +\lstinputlisting[label=src:fizzbuzz,caption=Fizz-Buzz]{ex-basics/src/fizzbuzz.go} +\end{Answer} diff --git a/ex-basics/src/fizzbuzz.go b/ex-basics/src/fizzbuzz.go new file mode 100644 index 0000000..e59eda7 --- /dev/null +++ b/ex-basics/src/fizzbuzz.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + fizz := 3 + buzz := 5 + for i := 1; i < 100; i++ { + if i%fizz == 0 { + fmt.Printf("Fizz") + } else if i%buzz == 0 { + fmt.Printf("FizzBuzz") + } else { + fmt.Printf("%v", i) + } + fmt.Println() + + } +} diff --git a/go-basics.tex b/go-basics.tex index 84eb71f..ee7561d 100644 --- a/go-basics.tex +++ b/go-basics.tex @@ -585,6 +585,8 @@ \subsection{Maps} \section{Exercises} \input{ex-basics/ex-for.tex} +\input{ex-basics/ex-fizzbuzz.tex} + \input{ex-basics/ex-strings.tex} \cleardoublepage diff --git a/go.bib b/go.bib index 323ce4e..09952f7 100644 --- a/go.bib +++ b/go.bib @@ -153,3 +153,10 @@ @misc{iota howpublished = {Internet, \url{http://en.wikipedia.org/wiki/Iota}} } + +@misc{fizzbuzz, + author = "Imran On Tech", + title = "Using FizzBuzz to Find Developers who Grok Coding", + howpublished = {Internet, + \url{http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/}} +}