In the beginning of every year I say to myself never regret your mistakes just learn from them, start the new year with fresh thoughts, new hopes, new dreams, being optimistic, and keep on trying till I reach the highest…

## Code Kata: Fizz Buzz Game

I stared to practice recently after reading more about how practicing is important for programmer to become professional and one way of practicing is Coding Kata. I found a lot of problem to implement to practice and perform coding kata.

This post I’ll talk about Fizz Buzz Game a.k.a. Bizz Buzz Game, it’s numerical/math game, if the number is divisible by 3 replace it with fizz, or if number divisible by 5 replace it with buzz, or if number is divisible by 3 and 5 replace it with fizzbuzz. For example if we count from 1 to 20 it’ll be like this: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz.

So how to code this game:

First I thought I can solve this game by:

If (this number is divisible by 3 and not divisible by 5) return fizz

else if (this number is not divisible by 3 and divisible by 5) return buzz

else if (this number is divisible by 3 and divisible by 5) return buzzfizz

else if(this number is not divisible by 3 and not divisible by 5) return the number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/// <summary> /// Solve fizz buzz problem /// if number divisible by 3 and not by 5 return fizz /// if number divisible by 5 and not by 3 return buzz /// if number divisible by 15 return fizzbuzz /// </summary> /// <param name="number">The number.</param> /// <returns>fizz, buzz, or fizzbuzz</returns> static public string FizzBuzz(int number) { string returnString = string.Empty; if (number % 3 == 0 && number % 5 != 0) { returnString = "fizz"; } if (number % 3 != 0 && number % 5 == 0) { returnString = "buzz"; } if (number % 3 == 0 && number % 5 == 0) { returnString = "fizzbuzz"; } if (number % 3 != 0 && number % 5 != 0) { returnString = number.ToString(); } return returnString; } |

But when I look at the code I feel like I can optimize it more by remove the and between two logical conditions, as the following.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/// <summary> /// Solve fizz buzz problem /// if number divisible by 3 and not by 5 return fizz /// if number divisible by 5 and not by 3 return buzz /// if number divisible by 15 return fizzbuzz /// </summary> /// <param name="number">The number.</param> /// <returns>fizz, buzz, or fizzbuzz</returns> static public string FizzBuzz(int number) { string returnString = string.Empty; if (number % 3 == 0) { if (number % 5 == 0) { returnString = "fizzbuzz"; } else if (number % 5 != 0) { returnString = "fizz"; } } else { if (number % 5 == 0) { returnString = "buzz"; } else if (number % 5 != 0) { returnString = number.ToString(); } } return returnString; } |

After observing the results, I found that fizzbuzz only appears when the number is divisible by 15, so I rewrite my pseudocodecode

If (this number is divisible by 15) return buzfizz

else if (this number is divisible by 3) return fizz

else if (this number is divisible by 5) return buzz

else return the number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/// <summary> /// Solve fizz buzz problem /// if number divisible by 3 and not by 5 return fizz /// if number divisible by 5 and not by 3 return buzz /// if number divisible by 15 return fizzbuzz /// </summary> /// <param name="number">The number.</param> /// <returns>fizz, buzz, or fizzbuzz</returns> static public string FizzBuzz(int number) { string returnString = string.Empty; if (number % 15 == 0) { returnString = "fizzbuzz"; } else if (number % 3 == 0) { returnString = "fizz"; } else if (number % 5 == 0) { returnString = "buzz"; } else { returnString = number.ToString(); } return returnString; } |

This Coding Kate done, I’ve enjoyed it as taook me to collage day’s when having fun solving small problems, I hope you enjoyed it as well. See you next Coding Kata.

## How to copy AOS instance from server to another

These are the steps to take a copy from AOS instance from server to another: Take copy of Dynamics AX database. (optional) Install AOS, Application Files, .NET Business Connector, and Client. Install Updates like Service Pack 1. Stop Application Object…

## Coding Kata

Recently I’m reading Uncle Bob‘s Clean Coder book and I read about how a professional programmer must always practice, like professional musicians, professional football player, and professional martial arts player. I’m learning how to play electric guitar, you need to start slowly “slow…