コード例 #1
0
        static void Main()
        {
            OrderedBag <Product> products = new OrderedBag <Product>(new ProductPriceComparer());

            SeedProducts(products);

            Stopwatch sw = new Stopwatch();

            decimal priceOne = (decimal)rnd.Next(MaxPriceInCents) / 100;
            decimal priceTwo = (decimal)rnd.Next(MaxPriceInCents) / 100;

            var firstProduct = products.Where(p => p.Price >= Math.Min(priceOne, priceTwo)).FirstOrDefault();
            var endProduct   = products.Where(p => p.Price <= Math.Max(priceOne, priceTwo)).LastOrDefault();

            sw.Start();


            for (int i = 0; i < 20000; i++)
            {
                var productRange = GetProductsInRange(
                    products,
                    firstProduct,
                    endProduct,
                    20);
            }

            sw.Stop();
            Console.WriteLine("Elapsed={0}", sw.Elapsed);
        }
コード例 #2
0
ファイル: Day6.cs プロジェクト: mabargiel/AdventOfCode
        public int Part2()
        {
            var meObjectOrbit    = _map.First(x => x.Name == "YOU").OrbitsOn;
            var santaOrbitObject = _map.First(x => x.Name == "SAN").OrbitsOn;

            var vertexes = new OrderedBag <Vertex>();

            foreach (var oribitngObject in _map.Where(x => x.Name != "YOU" && x.Name != "SAN"))
            {
                vertexes.Add(new Vertex(oribitngObject, oribitngObject.Equals(meObjectOrbit) ? 0 : int.MaxValue));
            }

            while (vertexes.Any())
            {
                var u          = vertexes.RemoveFirst();
                var neighbours = vertexes.Where(x =>
                                                (x.OribitngObject.OrbitsOn.Equals(u.OribitngObject) || x.OribitngObject.Equals(u.OribitngObject.OrbitsOn)) && u.Cost + 1 < x.Cost).ToList();

                if (u.OribitngObject.Equals(santaOrbitObject))
                {
                    return(u.Cost);
                }

                foreach (var neighbour in neighbours.Where(neighbour => u.Cost + 1 < neighbour.Cost))
                {
                    neighbour.Cost = u.Cost + 1;
                }

                vertexes = new OrderedBag <Vertex>(vertexes);
            }

            return(-1);
        }
コード例 #3
0
        public List <IEntity> GetAllByType(string type)
        {
            // var myType = data[0].GetType().Name;
            if (type == "Invoice" || type == "StoreClient" || type == "User")
            {
                // var currentType = "_02.Data.Models." + type;
                return(data.Where(x => x.GetType().Name == type).ToList());
            }

            throw new InvalidOperationException("Invalid type: " + type);
        }
コード例 #4
0
ファイル: Judge.cs プロジェクト: vani4ka66/Data-Structures
    public IEnumerable <int> ContestsByUserIdOrderedByPointsDescThenBySubmissionId(int userId)
    {
        HashSet <int> set    = new HashSet <int>();
        var           result = submits.Where(x => x.UserId.Equals(userId));

        foreach (var submission in result)
        {
            set.Add(submission.ContestId);
        }
        return(set);

        /*var result = byIdSubmitions[userId];
         *
         * foreach (var submission in result)
         * {
         *  yield return submission.ContestId;
         * }*/
    }
コード例 #5
0
        public static void Main(string[] args)
        {
            Dictionary <string, Product> listByName = new Dictionary <string, Product>();

            Dictionary <string, OrderedBag <Product> > listFilteredByType = new Dictionary <string, OrderedBag <Product> >();

            OrderedBag <Product> allProducts = new OrderedBag <Product>();

            string command;

            while ((command = Console.ReadLine()) != "end")
            {
                string[] subcommand = command.Split();

                switch (subcommand[0])
                {
                case "add":
                {
                    string name  = subcommand[1];
                    double price = double.Parse(subcommand[2]);
                    string type  = subcommand[3];

                    Product product = new Product();
                    product.Name  = name;
                    product.Type  = type;
                    product.Price = price;

                    if (listByName.ContainsKey(name))
                    {
                        Console.WriteLine("Error: Product {0} already exists", name);
                    }
                    else
                    {
                        listByName.Add(name, product);
                        if (listFilteredByType.ContainsKey(type))
                        {
                            listFilteredByType[type].Add(product);
                        }
                        else
                        {
                            listFilteredByType.Add(type, new OrderedBag <Product>()
                                {
                                    product
                                });
                        }
                        allProducts.Add(product);
                        Console.WriteLine("Ok: Product {0} added successfully", name);
                    }
                    break;
                }

                case "filter":
                {
                    switch (subcommand[2])
                    {
                    case "type":
                    {
                        string type = subcommand[3];

                        if (listFilteredByType.ContainsKey(type))
                        {
                            StringBuilder builder = new StringBuilder();
                            builder.Append("Ok: ");
                            builder.Append(string.Join("", listFilteredByType[type].Take(10)));
                            if (builder[builder.Length - 2] == ',')
                            {
                                builder.Remove(builder.Length - 2, 2);
                            }
                            Console.WriteLine(builder.ToString());
                        }
                        else
                        {
                            Console.WriteLine("Error: Type {0} does not exists", type);
                        }
                        break;
                    }

                    case "price":
                    {
                        StringBuilder builder = new StringBuilder();
                        builder.Append("Ok: ");
                        switch (subcommand[3])
                        {
                        case "from":
                        {
                            double minPrice = double.Parse(subcommand[4]);

                            switch (subcommand.Length)
                            {
                            case 7:
                            {
                                double maxPrice = double.Parse(subcommand[6]);

                                builder.Append(string.Join("", allProducts.Where(x => minPrice <= x.Price && x.Price <= maxPrice).Take(10)));
                            }
                            break;

                            case 5:
                            {
                                builder.Append(string.Join("", allProducts.Where(x => minPrice <= x.Price).Take(10)));
                            }
                            break;
                            }
                            break;
                        }

                        case "to":
                        {
                            double maxPrice = double.Parse(subcommand[4]);

                            builder.Append(string.Join("", allProducts.Where(x => x.Price <= maxPrice).Take(10)));
                        }
                        break;
                        }

                        if (builder[builder.Length - 2] == ',')
                        {
                            builder.Remove(builder.Length - 2, 2);
                        }
                        Console.WriteLine(builder.ToString());

                        break;
                    }
                    }
                    break;
                }
                }
            }
        }
コード例 #6
0
        private static void ExecuteCommand(string[] command)
        {
            if (command[0] == "add")
            {
                if (!productNames.Contains(command[1]))
                {
                    var product = new Product(command[1], double.Parse(command[2]), command[3]);
                    products.Add(product);
                    productNames.Add(command[1]);

                    if (!typeProducts.ContainsKey(command[3]))
                    {
                        typeProducts.Add(command[3], new OrderedBag <Product>());
                    }

                    typeProducts[command[3]].Add(product);

                    Console.WriteLine("Ok: Product {0} added successfully", product.Name);
                }
                else
                {
                    Console.WriteLine("Error: Product {0} already exists", command[1]);
                }
            }
            else if (command[0] == "filter" && command.Length == 7)
            {
                var items = products.Where(x => x.Price >= double.Parse(command[4]) && x.Price <= double.Parse(command[6])).Take(10);
                if (items.Count() == 0)
                {
                    Console.WriteLine("Ok: ");
                }
                else
                {
                    Console.WriteLine("Ok: " + string.Join(", ", items));
                }
            }
            else if (command[0] == "filter" && command.Length == 5)
            {
                if (command[3] == "from")
                {
                    var items = products.Where(x => x.Price >= double.Parse(command[4])).Take(10);
                    if (items.Count() == 0)
                    {
                        Console.WriteLine("Ok: ");
                    }
                    else
                    {
                        Console.WriteLine("Ok: " + string.Join(", ", items));
                    }
                }
                else if (command[3] == "to")
                {
                    var items = products.Where(x => x.Price <= double.Parse(command[4])).Take(10);
                    if (items.Count() == 0)
                    {
                        Console.WriteLine("Ok: ");
                    }
                    else
                    {
                        Console.WriteLine("Ok: " + string.Join(", ", items));
                    }
                }
            }
            else if (command[0] == "filter" && command.Length == 4)
            {
                if (typeProducts.ContainsKey(command[3]))
                {
                    var    items  = typeProducts[command[3]].Take(10);
                    string output = string.Join(", ", items);
                    Console.WriteLine("Ok: " + output);
                }
                else
                {
                    Console.WriteLine("Error: Type {0} does not exists", command[3]);
                }
            }
        }
コード例 #7
0
 public void FilterFromMinToMax(decimal min, decimal max)
 {
     Console.WriteLine("{0}", string.Join("\n", byPrice.Where(x => x.Price >= min && x.Price <= max).OrderBy(x => x.Price).ThenBy(x => x.Consumer)));
 }