Answer to Question #328283 in C# for Anji

Question #328283

There are two types of fishes in a pond, A and B.there are N fishes of type A numbered from 1 to N and M.the following two methods are adopted by the fishes of type A to satisfy their hunger.

1)E1:Fish of typeA(n1) eats the fish of type B(n2).

2)E2:Fish of type A(n1) eats the fish of type A(n2) if size(n1)>size(n2).

In the above mentioned context,E1and E2 denote the respective methods and n1 and n2 are the fishes belonging to these types and Size(X) denotes the total number of fish within X.

Example:if size(L)=3 and size(K)=2 and L eats K then the resulting size(L) will be5.

It is feeding time now and the fishes are hungry.theyeat as per the given method and satiate their hunger.your task is to find and return an array defining whether which type A fishes do All type B fishes fall under.

Note: the size of all fishes initially is the same i.e,1.

Ex1:

i/p1:2

i/p2:3

i/p3:4

i/p4:{{1,1,1},{1,1,2},{1,2,3},{2,1,2}}

o/p:{1,1,1}

ex2:

i/p1:1

i/p2:1

i/p3:1

i/p4:{{1,1,1}}

o/p:{1}


1
Expert's answer
2022-04-15T16:36:46-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Fish> fishes = new List<Fish>()
            {
                new Fish(1, FishType.A),
                new Fish(1, FishType.B),
                new Fish(1, FishType.A)
            };


            Fish mainFish = new Fish(2, FishType.A);
            mainFish.Eat(fishes);
            Console.WriteLine(mainFish);
        }
    }


    class Fish 
    {
        public int Length { get; private set; }
        public FishType Type { get; private set; }


        public Fish(int length, FishType type)
        {
            Length = length;
            Type = type;
        }




        public void Eat(List<Fish> fishes)
        {
            if (Type == FishType.B)
                return;


            foreach (Fish fish in fishes)
            {
                if (fish.Type == FishType.A && fish.Length < Length 
                    || fish.Type == FishType.B)
                {
                    Length += fish.Length;
                    fishes.Remove(fish);
                    break;
                }
            }
        }


        public override string ToString()
        {
            return $"Length: {Length}, Type; {Type.ToString()}";
        }


    }
    public enum FishType
    {
        A,
        B
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Dhanvin N H
08.09.22, 10:00

Thanks a lot

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS