Transcript
Page 1: LCNUG - Code Kata 2014 May 08

Code KatasFizzBuzz | Coin Changer

Mike Harris

Page 2: LCNUG - Code Kata 2014 May 08

Agenda

FizzBuzz

Coin Changer

Page 3: LCNUG - Code Kata 2014 May 08

– 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.”

Page 4: LCNUG - Code Kata 2014 May 08

FizzBuzz

Page 5: LCNUG - Code Kata 2014 May 08

FizzBuzz

FizzBuzz(2) -> “2”

FizzBuzz(3) -> “Fizz”

FizzBuzz(5) -> “Buzz”

FizzBuzz(15) -> “FizzBuzz”

Page 6: LCNUG - Code Kata 2014 May 08

FizzBuzz

If value is divisible by 3 then Fizz

If value is divisible by 5 then Buzz

If neither then give value as string

Page 7: LCNUG - Code Kata 2014 May 08

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; } } } }

Page 8: LCNUG - Code Kata 2014 May 08

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; }

Page 9: LCNUG - Code Kata 2014 May 08

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; } }

Page 10: LCNUG - Code Kata 2014 May 08

Coin Changer

Page 11: LCNUG - Code Kata 2014 May 08

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]

Page 12: LCNUG - Code Kata 2014 May 08

Coin Changer

Given coins of different values

Find the number of coins given back for a given amount

Page 13: LCNUG - Code Kata 2014 May 08

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; } } } }

Page 14: LCNUG - Code Kata 2014 May 08

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; }

Page 15: LCNUG - Code Kata 2014 May 08

class State { public ICollection<int> Result { get; set; } public int Amount { get; set; } public State(ICollection<int> result, int amount) { Result = result; Amount = amount; } }

Page 16: LCNUG - Code Kata 2014 May 08

Hope you had fun.

Page 17: LCNUG - Code Kata 2014 May 08

Mike Harris@MikeMKH http://comp-phil.blogspot.com/


Top Related