Last active
August 29, 2015 14:23
-
-
Save david-mclean/11c727f8a214f9a1e64c to your computer and use it in GitHub Desktop.
Playing with FizzBuzz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FizzBuzz | |
{ | |
public IEnumerable<string> Execute(int max) | |
{ | |
for(int i = 1; i <= max; i++) | |
{ | |
if(i%15==0) | |
yield return "fizzbuzz"; | |
else if(i%5==0) | |
yield return "buzz"; | |
else if(i%3==0) | |
yield return "fizz"; | |
else | |
yield return i.ToString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IExecutor | |
{ | |
IEnumerable<string> Execute(int max); | |
} | |
public abstract class DivisibleExecutor : IExecutor | |
{ | |
public List<Tuple<int,string>> conditionValues = new List<Tuple<int,string>> | |
{ | |
Tuple.Create(3, "Fizz"), | |
Tuple.Create(5, "Buzz") | |
}; | |
protected Func<int, int, bool> isMatch = (i, condition) => i % condition == 0; | |
public abstract IEnumerable<string> Execute(int max); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FizzBuzzImplimentation : DivisibleExecutor | |
{ | |
// | |
public override IEnumerable<string> Execute(int max) | |
{ | |
for(int i = 1; i <= max; i++) | |
{ | |
var matchingCombs = conditionValues.Where(c => isMatch(i, c.Item1)).ToList(); | |
if (matchingCombs.Any()) | |
{ | |
yield return string.Join("", matchingCombs.Select(c => c.Item2)); | |
} else { | |
yield return i.ToString(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var fizzbuzz = new WizzWuzz(new FizzBuzzImplimentation()); | |
foreach(var res in fizzbuzz.Execute(100)) | |
{ | |
Console.WriteLine(res); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DisplyWizzWuzz on a wednesday. | |
public class WizzWuzz : IExecutor | |
{ | |
// Logic to change output on a wednesday. | |
private IDateTimeUtil dateTimeUtil = new DateTimeUtil(); | |
private DivisibleExecutor decoratedExecutor; | |
public WizzWuzz(DivisibleExecutor decoratedExecutor) | |
{ | |
this.decoratedExecutor = decoratedExecutor; | |
} | |
public IEnumerable<string> Execute(int max) | |
{ | |
//Assuming it is weds.. Would test here to check... | |
if(dateTimeUtil.DayOfWeek == DayOfWeek.Wednesday) | |
{ | |
this.decoratedExecutor.conditionValues = new List<Tuple<int,string>> | |
{ | |
Tuple.Create(3, "Wizz"), | |
Tuple.Create(5, "Wuzz") | |
}; | |
return decoratedExecutor.Execute(max); | |
} else { | |
return decoratedExecutor.Execute(max); | |
} | |
} | |
} | |
public interface IDateTimeUtil | |
{ | |
DayOfWeek DayOfWeek{get;} | |
} | |
// DateTim | |
public class DateTimeUtil : IDateTimeUtil | |
{ | |
public DayOfWeek DayOfWeek | |
{ | |
get { return DayOfWeek.Wednesday; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment