SlideShare a Scribd company logo
1 of 17
Download to read offline
Code Katas
FizzBuzz | 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/

More Related Content

More from Mike Harris

A Divine Data Comedy
A Divine Data ComedyA Divine Data Comedy
A Divine Data ComedyMike Harris
 
Combinators - Lightning Talk
Combinators - Lightning TalkCombinators - Lightning Talk
Combinators - Lightning TalkMike Harris
 
Hiking through the Functional Forest with Fizz Buzz
Hiking through the Functional Forest with Fizz BuzzHiking through the Functional Forest with Fizz Buzz
Hiking through the Functional Forest with Fizz BuzzMike Harris
 
All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#Mike Harris
 
There and Back Again
There and Back AgainThere and Back Again
There and Back AgainMike Harris
 
The Marvelous Land of Higher Order Functions
The Marvelous Land of Higher Order FunctionsThe Marvelous Land of Higher Order Functions
The Marvelous Land of Higher Order FunctionsMike Harris
 

More from Mike Harris (8)

A Divine Data Comedy
A Divine Data ComedyA Divine Data Comedy
A Divine Data Comedy
 
Combinators - Lightning Talk
Combinators - Lightning TalkCombinators - Lightning Talk
Combinators - Lightning Talk
 
C# 7
C# 7C# 7
C# 7
 
Hiking through the Functional Forest with Fizz Buzz
Hiking through the Functional Forest with Fizz BuzzHiking through the Functional Forest with Fizz Buzz
Hiking through the Functional Forest with Fizz Buzz
 
Coding f#un
Coding f#unCoding f#un
Coding f#un
 
All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#
 
There and Back Again
There and Back AgainThere and Back Again
There and Back Again
 
The Marvelous Land of Higher Order Functions
The Marvelous Land of Higher Order FunctionsThe Marvelous Land of Higher Order Functions
The Marvelous Land of Higher Order Functions
 

Recently uploaded

Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...ScyllaDB
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch TuesdayIvanti
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe中 央社
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...ScyllaDB
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...FIDO Alliance
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideStefan Dietze
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Patrick Viafore
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Skynet Technologies
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfFIDO Alliance
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Hiroshi SHIBATA
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctBrainSell Technologies
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...FIDO Alliance
 

Recently uploaded (20)

Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 

LCNUG - Code Kata 2014 May 08

  • 1. Code Katas FizzBuzz | Coin Changer
 
 Mike Harris
  • 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.”
  • 5. FizzBuzz FizzBuzz(2) -> “2” FizzBuzz(3) -> “Fizz” FizzBuzz(5) -> “Buzz” FizzBuzz(15) -> “FizzBuzz”
  • 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. 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. 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. 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; } }
  • 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. Coin Changer Given coins of different values Find the number of coins given back for a given amount
  • 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. 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. 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. Hope you had fun.