+ All Categories
Home > Technology > LCNUG - Code Kata 2014 May 08

LCNUG - Code Kata 2014 May 08

Date post: 25-May-2015
Category:
Upload: mike-harris
View: 156 times
Download: 0 times
Share this document with a friend
Description:
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.
Popular Tags:
17
Code Katas FizzBuzz | Coin Changer Mike Harris
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/


Recommended