Code KatasFizzBuzz | Coin Changer
Mike Harris
Agenda
FizzBuzz
Coin Changer
– 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.”
FizzBuzz
FizzBuzz
FizzBuzz(2) -> “2”
FizzBuzz(3) -> “Fizz”
FizzBuzz(5) -> “Buzz”
FizzBuzz(15) -> “FizzBuzz”
FizzBuzz
If value is divisible by 3 then Fizz
If value is divisible by 5 then Buzz
If neither then give value as string
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; } } } }
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; } }
Coin Changer
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]
Coin Changer
Given coins of different values
Find the number of coins given back for a given amount
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; } } } }
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; } }
Hope you had fun.
Mike Harris@MikeMKH http://comp-phil.blogspot.com/