SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler 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/

Mais conteúdo relacionado

Mais de 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
 

Mais de 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
 

Último

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中 央社
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024Stephen Perrenod
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch TuesdayIvanti
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfalexjohnson7307
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxFIDO Alliance
 
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
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
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
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdfMuhammad Subhan
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
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
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Paige Cruz
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
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
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfFIDO Alliance
 

Último (20)

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
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
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
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
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
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
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
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
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
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 

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.