Writing FizzBuzz

后端 未结 30 1913
庸人自扰
庸人自扰 2020-12-04 08:50

Reading the coding horror, I just came across the FizzBuzz another time.

The original post is here: Coding Horror: Why Can\'t Programmers.. Program?

For thos

相关标签:
30条回答
  • 2020-12-04 09:16

    With the input of Rob H and Jacob Krall here is what I have at the moment. Perhaps I will play around with that in future... just wanted to provide it.

    public void DoFizzBuzz()
    {
        // expect this to come in as parameter
        var combinations = new Tuple<int, string>[] 
        { 
            new Tuple<int, string> (3, "Fizz"), 
            new Tuple<int, string> (5, "Buzz"), 
        };
    
        Func<int, int, bool> isMatch = (i, comb) => i % comb == 0;
    
        // expect the borders 1, 100 to come in as parameters
        for (int i = 1; i <= 100; ++i)
        {
            var matchingCombs = combinations.Where(c => isMatch(i, c.Item1)).DefaultIfEmpty(new Tuple<int, string>(i, i.ToString())).Aggregate((v, w) => new Tuple<int, string>(v.Item1, v.Item2 + w.Item2)).Item2;
            Console.WriteLine(matchingCombs);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:16

    The null-coalescing operator is really useful:

    string output = null;
    for (int i = 1; i <= 100; i++)
    {
         if (i % 3 == 0) output += "fizz";
         if (i % 5 == 0) output += "buzz";
         Console.WriteLine(output ?? i.ToString());
         output = null;
    }
    Console.ReadKey();
    
    0 讨论(0)
  • 2020-12-04 09:17

    In my opinion, the FizzBuzz problem is always presented as a challenge to the interviwee to make the word FizzBuzz appear without explicitly printing it. Here is my solution in C#.

    internal void PrintFizzBuzzAlternative(int num)
    {
        if (num % 5 == 0)
            Console.Write("Fizz");
        if (num % 3 == 0)
            Console.Write("Buzz");
        if (num % 5 != 0 && num % 3 != 0)
            Console.Write(num);
        Console.WriteLine();
    }
    
    0 讨论(0)
  • 2020-12-04 09:18

    You want probably make it configurable, but the question is what should be made configurable - we don't know that. Maybe we should make configurable all the cycle (FizzBuzz has the cycle). Here is very small and fun version with configurable cycle:

    string[] fizzBuzzCycle = 
        "FizzBuzz,{0},{0},Fizz,{0},Buzz,Fizz,{0},{0},Fizz,Buzz,{0},Fizz,{0},{0}"
        .Split(',');
    
    for (int i = 1; i <= 100; i++)
        Console.WriteLine(fizzBuzzCycle[i%fizzBuzzCycle.Length], i);
    

    So if the strings or whole cycle should be changed it is easy to change. But you just don't know what to make configurable. Maybe condition will change: "for prime numbers print Pizz" and for this modification the solution by @ThomasLevesque is better, because it is easier to change.

    0 讨论(0)
  • 2020-12-04 09:18

    Relatively simple solution using a for loop.

    No Linq or anything - just basic shorthand if statements

    for(int x=1;x<101;x++)
        Console.WriteLine(x%3==0?"Fizz"+(x%5==0?"Buzz":""):x%5==0?"Buzz":x+"");
    

    The Linq solution which is a lot like csells (sans string interpolation) and fits on one line would be:

    Enumerable.Range(1,100).ToList().ForEach(x=>Console.WriteLine(x%3==0?"Fizz"+(x%5==0?"Buzz":""):x%5==0?"Buzz":x+""));
    
    0 讨论(0)
  • 2020-12-04 09:19

    This my effort mixing Func with IEnumerable

     class Program
    {
        static void Main(string[] args)
        {
            foreach (var i in FizzBuzz(100))
            {
                Console.WriteLine(i);
            }
        }
    
        private static IEnumerable<string> FizzBuzz(int maxvalue)
        {
            int count = 0;
            //yield return count.ToString();
            Func<int, string> FizzBuzz = (x) => ((x % 5 == 0 && x % 3 == 0) ? "FizzBuzz" : null);
            Func<int, string> Buzz = (x) => ((x % 5 == 0) ? "Buzz" : null);
            Func<int, string> Fizz = (x) => ((x % 3 == 0) ? "Fizz" : null);
            Func<int, string> Number = (x) => x.ToString();
    
            while (count < maxvalue)
            {
                count++;
    
                yield return FizzBuzz(count) ?? Buzz(count) ?? Fizz(count) ?? Number(count);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题