public void CalculateNumberOfSlices(PizzaSliceInputEntity pizzaInput, PizzaSliceOutputEntity pizzaOutput)
        {
            var listToCalculate = new List <PizzaSliceInputWithIndexEntity>();

            var numberOfSlicesToOrder = new List <long>();

            numberOfSlicesToOrder.AddRange(pizzaInput.NumberOfSlicesToOrder);

            var withIndex = pizzaInput.NumberOfSlicesToOrder.Select((item, indexf) => new
            {
                Value = item,
                Index = indexf
            })
                            .ToList();

            listToCalculate = withIndex.Select(ass => new PizzaSliceInputWithIndexEntity
            {
                Index = ass.Index,
                Value = ass.Value
            })
                              .ToList();

            var listDescToCalculate = listToCalculate.OrderByDescending(n => n.Value).ToList();

            pizzaOutput.PizzaSummedNumbers = CalculateSumRecursion(listDescToCalculate, pizzaInput.PizzaToOrder).OrderBy(o => o).ToList();

            pizzaOutput.NumberOfPizzaTypes = pizzaOutput.PizzaSummedNumbers.Count;
        }
        public void ToPizzaEntity(string[] text, PizzaSliceInputEntity pizzaItem)
        {
            if (!text.Any())
            {
                return;
            }

            int pizzaOrder;
            int pizzaTypes;

            var firstLine  = text.FirstOrDefault().Split(new char[0]);
            var slicesLine = SplitSlicesLinesInFile(text.Skip(1).ToArray());

            if (Int32.TryParse(firstLine.FirstOrDefault(), out pizzaOrder))
            {
                pizzaItem.PizzaToOrder = pizzaOrder;
            }
            if (Int32.TryParse(firstLine.LastOrDefault(), out pizzaTypes))
            {
                pizzaItem.PizzaTypesToOrder = pizzaTypes;
            }

            foreach (var item in slicesLine)
            {
                int pizzaSlices;

                if (Int32.TryParse(item, out pizzaSlices))
                {
                    pizzaItem.NumberOfSlicesToOrder.Add(pizzaSlices);
                }
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            PizzaSliceInputEntity  pizzaInputEntity = new PizzaSliceInputEntity();
            PizzaSliceOutputEntity pizzaSliceOutput = new PizzaSliceOutputEntity();
            PizzaHandler           handler          = new PizzaHandler();

            var filePath = Path.Combine(Directory.GetCurrentDirectory(), INPUT_FILE_NAME);

            string fullText = File.ReadAllText(filePath);

            var fullTextSplitted = PizzaHelper.SplitTextInput(fullText, separator);

            handler.ToPizzaEntity(fullTextSplitted, pizzaInputEntity);

            //calculate logic
            handler.CalculateNumberOfSlices(pizzaInputEntity, pizzaSliceOutput);

            var outputText = PizzaHelper.CreateOutputText(pizzaSliceOutput);

            if (File.Exists(OUTPUT_FILE_NAME))
            {
                File.Delete(OUTPUT_FILE_NAME);
            }

            File.WriteAllText(OUTPUT_FILE_NAME, outputText);
        }