Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
/*class Car:IVehicle //the class, just to see what's needed (license plate number which is always like AAA111 - BBB222 so 3 letter and 3 number and the color) 
    {
        string lpn;
        public string LPN { get { return lpn; } }
        string color;
        public string Color { get { return color; } }
        public Car(string lpn, string color)
        {
            this.lpn = lpn;
            this.color = color;
        }
    }

List<ivehicle> vehicles=new List<ivehicle>(); //I'm using a list so that's why i'm showing that to you
List<int> penalities=new List<int>();*/

public bool Suspicious(IVehicle veh) //this method job would be if the police can't see the license plate number perfectly (first you should check if the colors are the same as you can see in the code) and with this you can check if there is a little mistake in the lpn example (in list AAA111 & red, and you run the method x.Suspicios(new Car("AAX11X","red")) it gives you back true if atleast 4 char is same in your list, so e.g. x.Suspicious(new Car("AXX11X","red")) gives back false, or if i change the color so it does give back false
        {
            foreach (Car x in vehicles)
            {
                if (x.Color == veh.Color)
                {
                    int coun = 0;
                    int i = 0;
                    while (coun < 4 && i < 6)
                    {
                        if (x.LPN[i] == veh.LPN[i])
                            db++;
                        i++;
                        if (db > 3)
                            return true;
                    }
                }
            }
            return false;
        }

//in the main
log x=new log();
x.Initiate(3);
x.NewIrregular(new Car("AAA111","red"),1000);
bool sus=x.Suspicious(new Car("AAX11X","red");//so atm it gives back true


What I have tried:

i tried this exercise with arrays but it was slower and i had like 4-5 codes before that, and that was the simplest and fastest, the resource amount is like nevermind, it should be fast...
I'm trying to do it with lambda but i'm far from expert and i can't figure it out how can i write it in lambda...
Could anyone help me, please?
Posted
Updated 19-Mar-17 11:19am
v2

You are running against a list of unknown size (vehicles), so even a single check is quick, you can not predict the final time (it is actually O(color) + O(n-color))...
As you are not interested in any car has different color from the one you passed in, you may do a pre-select of only the cars with the same color... Something like this:
C#
var CheckList = vehicles.select(car => car.Color == veh.Color);
foreach (Car x in CheckList)
{
  // ...
}

Your inner computation is also unclear...
coun seams to be unused and unnecessary...
You also can cut the check if even all the letters left are mach you will not reach the 4 boundary... (something like (db + (6 - i)) < 4)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900