patterncsharpMinor
Test questions - Number Generator, Linq, Delegates, Sequences
Viewed 0 times
numberlinqquestionsgeneratortestdelegatessequences
Problem
I have recently been asked to do a test prior to an interview to test my C# skills. I am fairly new to C# (6 months) and I think this showed with me producing an overly complex answer to their questions.
I will post the 3 questions below as well as my answers. Please can someone give me a better solution to this. I had thought about using Linq as well to do this, but again wasn't really sure how. I also tried to add delegates into the mix, but not sure if I should have done this or not. Also I wasn't sure what tests to do in my unit testing, again any quick examples would be very appreciated.
Question 1
Write a program that prints the numbers from 1 to 100. For multiples
of three print "Three" and for the multiples of five print "Five" and
for numbers which are multiples of both three and five print
"ThreeFive".
Question 2
If we list all the whole numbers up to 15 that are multiples of 3 or
5, we get 3, 5, 6 and 9, 10, 12, 15. The sum of these multiples is 60.
Find the sum of all the multiples of 3 or 5 up to 1000.
Question 3
Calculate the sum of all the even numbers in the Fibonacci sequence
(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.) where each number in the
sequence represents the sum of the previous two, up to 4,000,000.
Bonus Points
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestPaperApp
{
public class TestPaperTestQuestions
{
public static List TestPaperList = new List();
public static List SumItUpList = new List();
public static List FibonacciList = new List();
public static string range;
public delegate void Question(int number);
public static void Main(string[] args)
{
// Write numbers 1-100 - multiples of 3 write 'Three' multiples of 5 print ‘Five’ multiples of both print 'ThreeFive'
I will post the 3 questions below as well as my answers. Please can someone give me a better solution to this. I had thought about using Linq as well to do this, but again wasn't really sure how. I also tried to add delegates into the mix, but not sure if I should have done this or not. Also I wasn't sure what tests to do in my unit testing, again any quick examples would be very appreciated.
Question 1
Write a program that prints the numbers from 1 to 100. For multiples
of three print "Three" and for the multiples of five print "Five" and
for numbers which are multiples of both three and five print
"ThreeFive".
Question 2
If we list all the whole numbers up to 15 that are multiples of 3 or
5, we get 3, 5, 6 and 9, 10, 12, 15. The sum of these multiples is 60.
Find the sum of all the multiples of 3 or 5 up to 1000.
Question 3
Calculate the sum of all the even numbers in the Fibonacci sequence
(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.) where each number in the
sequence represents the sum of the previous two, up to 4,000,000.
Bonus Points
- Unit tests your solutions
- Bases the answer to 1 and 2 on the same 'number generator'
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestPaperApp
{
public class TestPaperTestQuestions
{
public static List TestPaperList = new List();
public static List SumItUpList = new List();
public static List FibonacciList = new List();
public static string range;
public delegate void Question(int number);
public static void Main(string[] args)
{
// Write numbers 1-100 - multiples of 3 write 'Three' multiples of 5 print ‘Five’ multiples of both print 'ThreeFive'
Solution
Just some general remarks:
-
Question 1 is just a derivative of the classic FizzBuzz but using different words as mentioned in the comments.
-
None of the questions actually require you to store all the number. You are wasting a lot of space but storing them all and then post-processing them - you could pretty much do all of it on the fly.
Some basic ideas for 1 and 2 using enumerators:
Now you have a generator which creates a sequence yielding for each number if it's a multiple of 3 or 5 and the translated version of it. You can create algorithms to implement question 1 and 2 based on it.
Rather than using a
Similar you can write a generator for the Fibonacci sequence returning all the Fibonacci numbers and then you sum all the even ones (special handling for max < 2 not included):
-
Question 1 is just a derivative of the classic FizzBuzz but using different words as mentioned in the comments.
-
None of the questions actually require you to store all the number. You are wasting a lot of space but storing them all and then post-processing them - you could pretty much do all of it on the fly.
Some basic ideas for 1 and 2 using enumerators:
public IEnumerable> GenerateFizzBuzzLikeSequence(int start, int end)
{
for (int i = start; i <= end; ++i)
{
if (i % 3 == 0 && i % 5 == 0)
{
yield return Tuple.Create(i, true, "ThreeFive");
}
if (i % 3 == 0)
{
yield return Tuple.Create(i, true, "Three");
}
if (i % 5 == 0)
{
yield return Tuple.Create(i, true, "Five");
}
yield return Tuple.Create(i, false, i.ToString());
}
}Now you have a generator which creates a sequence yielding for each number if it's a multiple of 3 or 5 and the translated version of it. You can create algorithms to implement question 1 and 2 based on it.
public void Question1()
{
foreach (var item in GenerateFizzBuzzLikeSequence(1, 100))
{
Console.WriteLine(item.Item3);
}
}
public void Question2()
{
int sumOfAllMultipleOf3And5 = GenerateFizzBuzzLikeSequence(1, 1000).Where(t => t.Item2).Sum();
Console.WriteLine(sumOfAllMultipleOf3And5);
}Rather than using a
Tuple a little helper class could be created with somewhat more meaningful property names.Similar you can write a generator for the Fibonacci sequence returning all the Fibonacci numbers and then you sum all the even ones (special handling for max < 2 not included):
public IEnumerable GenerateFibonacciSequence(int max)
{
int a = 0;
int b = 1;
yield return a;
yield return b;
for (int i = 2; i <= max; ++i)
{
int number = a + b;
yield return number;
a = b;
b = number;
}
}Code Snippets
public IEnumerable<Tuple<int, bool, string>> GenerateFizzBuzzLikeSequence(int start, int end)
{
for (int i = start; i <= end; ++i)
{
if (i % 3 == 0 && i % 5 == 0)
{
yield return Tuple.Create(i, true, "ThreeFive");
}
if (i % 3 == 0)
{
yield return Tuple.Create(i, true, "Three");
}
if (i % 5 == 0)
{
yield return Tuple.Create(i, true, "Five");
}
yield return Tuple.Create(i, false, i.ToString());
}
}public void Question1()
{
foreach (var item in GenerateFizzBuzzLikeSequence(1, 100))
{
Console.WriteLine(item.Item3);
}
}
public void Question2()
{
int sumOfAllMultipleOf3And5 = GenerateFizzBuzzLikeSequence(1, 1000).Where(t => t.Item2).Sum();
Console.WriteLine(sumOfAllMultipleOf3And5);
}public IEnumerable<int> GenerateFibonacciSequence(int max)
{
int a = 0;
int b = 1;
yield return a;
yield return b;
for (int i = 2; i <= max; ++i)
{
int number = a + b;
yield return number;
a = b;
b = number;
}
}Context
StackExchange Code Review Q#49868, answer score: 4
Revisions (0)
No revisions yet.