Created
June 5, 2013 05:30
-
-
Save fuzeman/5711801 to your computer and use it in GitHub Desktop.
CodeEval Fizz Buzz (001)
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.IO; | |
using System.Linq; | |
namespace Solutions | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var lines = new List<string>(); | |
using (var reader = File.OpenText(args[0])) | |
while (!reader.EndOfStream) | |
{ | |
var line = reader.ReadLine(); | |
if(null == line) | |
continue; | |
lines.Add(line); | |
} | |
Console.Write(String.Join("\n", FizzBuzz.Solve(lines.ToArray()))); | |
} | |
} | |
public class FizzBuzz | |
{ | |
public static string[] Solve(string[] lines) | |
{ | |
var result = new string[lines.Length]; | |
for (var i = 0; i < lines.Length; i++) | |
{ | |
result[i] = SolveLine(lines[i]); | |
} | |
return result; | |
} | |
private static string SolveLine(string line) | |
{ | |
var split = line.Trim().Split(' '); | |
if (split.Length != 3) | |
throw new ArgumentException("Line does not contain 3 space-separated parts", "line"); | |
var a = int.Parse(split[0]); | |
var b = int.Parse(split[1]); | |
var n = int.Parse(split[2]); | |
var result = Enumerable.Range(1, n).Select(i => | |
{ | |
var ar = (i % a) == 0; | |
var br = (i % b) == 0; | |
if (ar && br) | |
return "FB"; | |
if (ar) | |
return "F"; | |
if (br) | |
return "B"; | |
return i.ToString(); | |
}); | |
return String.Join(" ", result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment