44.5k views
0 votes
Implement a function called merge that does the following in C#:

* Takes three sequences of numbers from 1 to 15

* Merge the list with members that adhere to the following requirements

* Any multiple of 3

* Is a member of all three lists

Explanation:

First get a list of multiples of three from all three lists (no repeats)

Then combine the list of threes with the intersection of all three lists (no repeats)

public class Problem2

{

public static IEnumerable merge(IEnumerable input1, IEnumerable input2, IEnumerable input3)

{

}

}

static void Main(string[] args)

{

Random rnd = new Random();

var list1 = Enumerable.Range(1,10).Select(i=>(rnd.Next()%15)+1);

var list2 = Enumerable.Range(1,10).Select(i=>(rnd.Next()%15)+1);

var list3 = Enumerable.Range(1,10).Select(i=>(rnd.Next()%15)+1);

var answer = Problem2.merge(list1,list2,list3);

foreach( int i in answer )

{

Console.WriteLine(i);

}

}

User Jacobz
by
5.1k points

1 Answer

5 votes

Answer:

Check the explanation

Step-by-step explanation:

using System;

using System.Collections.Generic;

using System.Linq;

public class Problem2

{

public static IEnumerable<int> merge (IEnumerable<int> input1, IEnumerable<int> input2, IEnumerable<int> input3)

{

IList<int> list = new List<int>();

foreach (int i in input1)

{

if(i%3==0 && ! list.Contains(i))

list.Add(i);

}

foreach (int i in input2)

{

if(i%3==0 && ! list.Contains(i))

list.Add(i);

}

foreach (int i in input3)

{

if(i%3==0 && ! list.Contains(i))

list.Add(i);

}

return list;

}

}

public class Problem2Demo{

static void Main (string[] args)

{

Random rnd = new Random ();

var list1 = Enumerable.Range (1, 10).Select (i => (rnd.Next () % 15) + 1);

var list2 = Enumerable.Range (1, 10).Select (i => (rnd.Next () % 15) + 1);

var list3 = Enumerable.Range (1, 10).Select (i => (rnd.Next () % 15) + 1);

var answer = Problem2.merge (list1, list2, list3);

foreach (int i in answer)

{

Console.WriteLine (i);

}

}

Kindly check the attached output image below.

Implement a function called merge that does the following in C#: * Takes three sequences-example-1
User Magaly
by
5.3k points