Upcoming SlideShare
×

# LCNUG - Code Kata 2014 May 08

489 views
283 views

Published on

How do you get to be better at coding? You could read more, go to conferences, take some online classes, .... but ultimately it comes down to mindful practicing.

Practice you say, what about just doing our jobs?!? Yes, that is important but do you really want to try something new and crazy with your source of livelihood?

That is were Code Katas come into play. The idea with a Code Kata is work on a simple coding problem so that you can focus on how you are solving it.

Always wanted to try BDD? Try it in a FizzBuzz kata.

Want to try out functional programming? Use it on the Coin Changer kata.

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
489
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
2
0
Likes
1
Embeds 0
No embeds

No notes for slide

### LCNUG - Code Kata 2014 May 08

1. 1. Code Katas FizzBuzz | Coin Changer    Mike Harris
2. 2. Agenda FizzBuzz Coin Changer
3. 3. – Micah Martin “Katas can stretch our abilities and, similar to how a kata would teach a martial artist to become comfortable with the uncomfortable, they help us write code we may not normally write.”
4. 4. FizzBuzz
5. 5. FizzBuzz FizzBuzz(2) -> “2” FizzBuzz(3) -> “Fizz” FizzBuzz(5) -> “Buzz” FizzBuzz(15) -> “FizzBuzz”
6. 6. FizzBuzz If value is divisible by 3 then Fizz If value is divisible by 5 then Buzz If neither then give value as string
7. 7. using System; using System.Collections.Generic; using System.Linq; namespace FizzBuzz { public class FizzBuzzer { public string Translate(int value) { var result = new List<Translator> { new Translator(() => value%3 == 0, "Fizz"), new Translator(() => value%5 == 0, "Buzz") }.Aggregate(string.Empty, (s, t) => s += t.Translate()); return string.IsNullOrEmpty(result) ? value.ToString() : result; } class Translator { Func<bool> Test { get; set; } string Translation { get; set; } public Translator(Func<bool> test, string translation) { Test = test; Translation = translation; } public string Translate() { return Test() ? Translation : string.Empty; } } } }
8. 8. public string Translate(int value) { var result = new List<Translator> { new Translator(() => value%3 == 0, "Fizz"), new Translator(() => value%5 == 0, "Buzz") }.Aggregate( string.Empty, (s, t) => s += t.Translate()); return string.IsNullOrEmpty(result) ? value.ToString() : result; }
9. 9. class Translator { Func<bool> Test { get; set; } string Translation { get; set; } public Translator( Func<bool> test, string translation) { Test = test; Translation = translation; } public string Translate() { return Test() ? Translation : string.Empty; } }
10. 10. Coin Changer
11. 11. Coin Changer Changer.coins <- {pennies}  Changer.for(3) -> [3] Changer.coins <- {dimes, nickels, pennies}  Changer.for(17) -> [1, 1, 2] Changer.coins <- {quarters, dimes, nickels, pennies}  Changer.for(99) -> [3, 2, 0, 4] Changer.coins <- {nickels, pennies}  Changer.for(99) -> [19, 4]
12. 12. Coin Changer Given coins of different values Find the number of coins given back for a given amount
13. 13. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CoinChanger { public class Changer { public ICollection<int> Coins { get; set; } public Changer() { Coins = new List<int>(); } public ICollection<int> For(int amount) { return Coins.Aggregate( new State(new List<int>(), amount), (working, coin) => { working.Result.Add(working.Amount/coin); return new State(working.Result, working.Amount%coin); }).Result; } class State { public ICollection<int> Result { get; set; } public int Amount { get; set; } public State(ICollection<int> result, int amount) { Result = result; Amount = amount; } } } }
14. 14. public ICollection<int> For(int amount) { return Coins.Aggregate( new State(new List<int>(), amount), (working, coin) => { working.Result.Add(working.Amount/coin); return new State( working.Result, working.Amount%coin); }).Result; }
15. 15. class State { public ICollection<int> Result { get; set; } public int Amount { get; set; } public State(ICollection<int> result, int amount) { Result = result; Amount = amount; } }
16. 16. Hope you had fun.
17. 17. Mike Harris  @MikeMKH http://comp-phil.blogspot.com/