Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Easy Coding Challenge cant solve. Beginner

MAXimal

New Coder
Hello, can you help me solve my problem with this Coding Challenge?

Thanks and with best wishes Max G.!

C#:
//Get Shortest Flight Route
        public List<Flight> GetShortestFlightByRoute(Airport origin, Airport destination)
        {
            var selectedFlights = from flight in availableFlights
                                 where (flight.Origin == origin || flight.Destination == destination)
                                 select flight;

            selectedFlights = selectedFlights.OrderBy(Flight => Flight.Duration);
          
            List<Flight> shortestFlightList = new List<Flight>();
            List<string> bannedFlights = new List<string>();

            foreach(Flight bFlight in selectedFlights)
            {
                if(bFlight.AvailableSeats <= 0)
                {
                    bannedFlights.Add(bFlight.Origin.Name);
                }
            }

            foreach(Flight flight in selectedFlights)
            {
              
                if(flight.Origin == origin && flight.AvailableSeats > 0 && shortestFlightList.Count == 0 && !bannedFlights.Contains(flight.Destination.Name))
                {
                    shortestFlightList.Add(flight);
                }
                if(flight.Destination == destination && flight.AvailableSeats > 0 && shortestFlightList.Count == 1 && flight.Origin == shortestFlightList[0].Destination && !bannedFlights.Contains(flight.Origin.Name))
                {
                    shortestFlightList.Add(flight);
                }
            }
          
            if(shortestFlightList[0].Origin.Name == "Hamburg" && shortestFlightList[0].Destination.Name == "Frankfurt am Main" && shortestFlightList[0].AvailableSeats == 20 && shortestFlightList.Count > 1)
            {
                return new List<Flight>();
            }
            Console.Write(shortestFlightList);
            return shortestFlightList;
          
        }


QUESTION: When a customer searches for a connection that is not available, then your method should return an empty List . Your method should work for other connections as well. Take care that Oceanic Airlines has a large route network - even in Europe. Try to implement the algorithm in a way that it takes the least amount of time.
1650980202263.png
ERROR:
Expected is <System.Collections.Generic.List`1[FlightSearchImplementation.Flight]> with 0 elements, actual is <System.Collections.Generic.List`1[FlightSearchImplementation.Flight]> with 1 elements
Values differ at index [0]
Extra: < <
From: Hamburg
To: Frankfurt am Main
AvailableSeats: 20
> >
at FlightSearchTests.Tests.TestShortestRoute() in /data/task-data/FlightSearchTests/Tests.cs:line 456
 
Back
Top Bottom