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

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

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

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.