C
C#3y ago
Turwaith

FizzBuzz as short as possible

With a little help from StackOverflow I came up with this:
List<string> testList = new List<string>();
testList.Add(Enumerable.Range(1, 200)
.Select(n => n % 15 == 0 ? "FizzBuzz"
: n % 3 == 0 ? "Fizz"
: n % 5 == 0 ? "Buzz"
: n.ToString()).ToString());
List<string> testList = new List<string>();
testList.Add(Enumerable.Range(1, 200)
.Select(n => n % 15 == 0 ? "FizzBuzz"
: n % 3 == 0 ? "Fizz"
: n % 5 == 0 ? "Buzz"
: n.ToString()).ToString());
Now it adds only one element though, which is "System.Linq.Enumerable+SelectRangeIterator'1[System.String]". How do I get this expression to have one list object for each number?
3 Replies
Tvde1
Tvde13y ago
string.Join(
Environment.NewLine,
Enumerable.Range(1, 200)
.Select(n => n % 15 == 0 ? "FizzBuzz"
: n % 3 == 0 ? "Fizz"
: n % 5 == 0 ? "Buzz"
: n.ToString()));
string.Join(
Environment.NewLine,
Enumerable.Range(1, 200)
.Select(n => n % 15 == 0 ? "FizzBuzz"
: n % 3 == 0 ? "Fizz"
: n % 5 == 0 ? "Buzz"
: n.ToString()));
Turwaith
TurwaithOP3y ago
Thanks, found a different solution in the meantime that works as well:
List<string> result =

Enumerable.Range(1, 200)
.Select(n => n % 3 == 0 && n % 5 == 0 ? "FizzBuzz"
: n % 3== 0 ? "Fizz"
: n % 5== 0 ? "Buzz"
: n.ToString()).ToList();
List<string> result =

Enumerable.Range(1, 200)
.Select(n => n % 3 == 0 && n % 5 == 0 ? "FizzBuzz"
: n % 3== 0 ? "Fizz"
: n % 5== 0 ? "Buzz"
: n.ToString()).ToList();
Angius
Angius3y ago
string.Join(
'\n',
Enumerable.Range(1, 200)
.Select(n => (n % 3, n % 5) switch {
(0, 0) => "FizzBuzz",
(0, _) => "Fizz",
(_, 0) => "Buzz",
_ => $"{n}"
});
string.Join(
'\n',
Enumerable.Range(1, 200)
.Select(n => (n % 3, n % 5) switch {
(0, 0) => "FizzBuzz",
(0, _) => "Fizz",
(_, 0) => "Buzz",
_ => $"{n}"
});
Want results from more Discord servers?
Add your server