Last active
May 16, 2018 13:39
-
-
Save miklund/67e78ac70eb935164411cc12ee39aa22 to your computer and use it in GitHub Desktop.
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 IEnumerable<string> GetFizzBuzz(int max) | |
{ | |
var result = new List<string>(); | |
for (int number = 1; number <= max; number++) | |
{ | |
if (number % 15 == 0) | |
{ | |
result.Add("fizzbuzz"); | |
} | |
else if (number % 3 == 0) | |
{ | |
result.Add("fizz"); | |
} | |
else if (number % 5 == 0) | |
{ | |
result.Add("buzz"); | |
} | |
else | |
{ | |
result.Add(number.ToString()); | |
} | |
} | |
return result; | |
} |
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
// sequence where every third is fizz | |
let fizzes = cycle [""; ""; "fizz"] | |
// sequence where every fifth is buzz | |
let buzzes = cycle [""; ""; ""; ""; "buzz"] | |
// combined sequence of fizzes and buzzes | |
let words = Seq.map2 (+) fizzes buzzes | |
// sequence of numbers starting with "1" | |
let numbers = Seq.initInfinite show |> Seq.skip 1 | |
// pick the largest string | |
let choice = max | |
// combined words and numbers, pick the largest string | |
let fizzbuzz = Seq.zip words numbers |> Seq.map choice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment