Exemple #1
0
        public void Parse_MirsList_From_FileContent_Test()
        {
            string fileContent = @"1;""МИР1"";5
2;""МИР2"";4
3;""МИР3"";5
4;""МИР4"";10
5;""МИР5"";7
6;""МИР6"";12
7;""МИР7"";7
8;""Извън страната"";0";

            List <Mir> expected = new List <Mir>()
            {
                new Mir(1, "\"МИР1\"", 5),
                new Mir(2, "\"МИР2\"", 4),
                new Mir(3, "\"МИР3\"", 5),
                new Mir(4, "\"МИР4\"", 10),
                new Mir(5, "\"МИР5\"", 7),
                new Mir(6, "\"МИР6\"", 12),
                new Mir(7, "\"МИР7\"", 7),
                new Mir(8, "\"Извън страната\"", 0),
            };
            List <Mir> actual;

            actual = InputParsers.ParseMirsListFromFileContent(fileContent);
            Assert.IsTrue(CompareHelpers.AreEqualCollections <Mir>(expected, actual));
        }
Exemple #2
0
        public override string P1()
        {
            InputParsers.ForEach(parser =>
            {
                foods.Add(parser.AcceptIdents());
                parser.Burn(2);

                foreach (string allergen in parser.AcceptIdents(Symbol.Comma))
                {
                    if (allergenData.ContainsKey(allergen))
                    {
                        allergenData[allergen] = Tuple.Create((string)null, allergenData[allergen].Item2.Intersect(foods.Last()).ToList());
                    }
                    else
                    {
                        allergenData[allergen] = Tuple.Create((string)null, foods.Last().ToList());
                    }
                }
            });

            while (allergenData.Values.Where(tup => string.IsNullOrEmpty(tup.Item1)).Count() > 0)
            {
                foreach (var entry in allergenData)
                {
                    if (entry.Value.Item2?.Count == 1)
                    {
                        allergenData[entry.Key] = Tuple.Create(entry.Value.Item2[0], (List <string>)null);
                        foods.ForEach(f => f.Remove(entry.Value.Item2[0]));
                        allergenData.ForEach(a => a.Value.Item2?.Remove(entry.Value.Item2[0]));
                    }
                }
            }

            return(foods.Select(f => f.Count()).Sum().ToString());
        }
Exemple #3
0
        public void Parse_Candidate_From_String_Test()
        {
            string recordLine = "7;2;\"К7 на П2 в МИР7\""; // TODO: Initialize to an appropriate value
            //7;2;"К7 на П2 в МИР7"
            //7;3;"К1 на П3 в МИР7"
            Candidate expected = new Candidate(7, 2, "\"К7 на П2 в МИР7\""); // TODO: Initialize to an appropriate value
            Candidate actual;

            actual = InputParsers.ParseCandidateFromString(recordLine);
            Assert.AreEqual(expected, actual);
        }
Exemple #4
0
        public void Parse_Mir_From_String_Test_1()
        {
            string recordLine = @"1;“МИР 1“;10";
            // 2;“МИР 2“;5
            // 3;“Чужбина“;0";
            Mir expected = new Mir(1, @"“МИР 1“", 10); // TODO: Initialize to an appropriate value
            Mir actual;

            actual = InputParsers.ParseMirFromString(recordLine);
            Assert.AreEqual(expected, actual);
        }
Exemple #5
0
        public void Parse_Vote_From_String_Test()
        {
            string recordLine = "2;5;5500"; // TODO: Initialize to an appropriate value
            //2;5;5500
            //3;1;25678
            Vote expected = new Vote(2, 5, 5500); // TODO: Initialize to an appropriate value
            Vote actual;

            actual = InputParsers.ParseVoteFromString(recordLine);
            Assert.AreEqual(expected, actual);
        }
Exemple #6
0
        public void Parse_Party_FromString_Test_For_Type_InitiativeComittee()
        {
            string recordLine = @"1000;“Инициативен комитет в МИР 1“"; // TODO: Initialize to an appropriate value
            /// 1;“Партия 1“
            /// 2;“Коалиция 1“
            /// 1000;“Инициативен комитет в МИР 1“
            /// 1001;“Инициативен комитет в МИР 2“
            Party expected = new Party(1000, "“Инициативен комитет в МИР 1“"); // TODO: Initialize to an appropriate value
            Party actual;

            actual = InputParsers.ParsePartyFromString(recordLine);
            Assert.AreEqual(expected, actual);
        }
Exemple #7
0
        public void Parse_VotesList_From_FileContent_Test()
        {
            string fileContent = @"1;1;35121
1;2;20010
1;3;8456
1;4;18211
1;5;8200
";

            List <Vote> expected = new List <Vote>()
            {
                new Vote(1, 1, 35121),
                new Vote(1, 2, 20010),
                new Vote(1, 3, 8456),
                new Vote(1, 4, 18211),
                new Vote(1, 5, 8200),
            };
            List <Vote> actual;

            actual = InputParsers.ParseVotesListFromFileContent(fileContent);
            Assert.IsTrue(CompareHelpers.AreEqualCollections <Vote>(expected, actual));
        }
Exemple #8
0
        public void Parse_PartiesList_FromFileContent_Test()
        {
            string fileContent = @"1;""П1""
2;""П2""
3;""П3""
4;""П4""
5;""П5""
";

            List <Party> expected = new List <Party>()
            {
                new Party(1, "\"П1\""),
                new Party(2, "\"П2\""),
                new Party(3, "\"П3\""),
                new Party(4, "\"П4\""),
                new Party(5, "\"П5\""),
            };
            List <Party> actual;

            actual = InputParsers.ParsePartiesListFromFileContent(fileContent);
            Assert.IsTrue(CompareHelpers.AreEqualCollections <Party>(expected, actual));
        }
Exemple #9
0
        public void Parse_ResultsList_From_FileContent_Test()
        {
            string fileContent = @"1;1;3
1;2;1
1;4;1
2;1;1
2;2;1
2;3;1
2;4;1
3;1;1
3;2;2
3;3;1
3;4;1
4;1;4
4;2;2
4;3;3
4;4;1
5;1;2
5;2;2
5;3;2
5;4;1
6;1;4
6;2;4
6;3;3
6;4;1
7;1;3
7;2;2
7;3;1
7;4;1

";

            List <Result> expected = new List <Result>()
            {
                new Result(1, 1, 3),
                new Result(1, 2, 1),
                new Result(1, 4, 1),
                new Result(2, 1, 1),
                new Result(2, 2, 1),
                new Result(2, 3, 1),
                new Result(2, 4, 1),
                new Result(3, 1, 1),
                new Result(3, 2, 2),
                new Result(3, 3, 1),
                new Result(3, 4, 1),
                new Result(4, 1, 4),
                new Result(4, 2, 2),
                new Result(4, 3, 3),
                new Result(4, 4, 1),
                new Result(5, 1, 2),
                new Result(5, 2, 2),
                new Result(5, 3, 2),
                new Result(5, 4, 1),
                new Result(6, 1, 4),
                new Result(6, 2, 4),
                new Result(6, 3, 3),
                new Result(6, 4, 1),
                new Result(7, 1, 3),
                new Result(7, 2, 2),
                new Result(7, 3, 1),
                new Result(7, 4, 1),
            };
            List <Result> actual;

            actual = InputParsers.ParseResultsListFromFileContent(fileContent);
            Assert.IsTrue(CompareHelpers.AreEqualCollections <Result>(expected, actual));
        }
Exemple #10
0
        static void Main(string[] args)
        {
            string dir = "";

            Logger.Info("Зареждане на входните данни");
            //MIRS
            string mirsFilePath = Path.Combine(dir, "MIRs.txt");
            var    mirs         = InputParsers.ParseMirsListFromFile(mirsFilePath);

            Logger.Info(string.Format("Брой МИР:{0}", mirs.Count));

            //parties
            string partiesFilePath = Path.Combine(dir, "Parties.txt");
            var    parties         = InputParsers.ParsePartiesListFromFile(partiesFilePath);

            Logger.Info(string.Format("Брой партии:{0}", parties.Count));

            //candidates
            string candidatesFilePath = Path.Combine(dir, "Candidates.txt");
            var    candidates         = InputParsers.ParseCandidatesListFromFile(candidatesFilePath);

            Logger.Info(string.Format("Брой кандидати:{0}", candidates.Count));

            //votes
            string votesFilePath = Path.Combine(dir, "Votes.txt");
            var    votes         = InputParsers.ParseVotesListFromFile(votesFilePath);

            Logger.Info(string.Format("Брой записи за гласове:{0}", votes.Count));

            //lots
            string lotsFilePath = Path.Combine(dir, "Lot.txt");
            var    lots         = new List <Lot>();

            if (File.Exists(lotsFilePath))
            {
                lots = InputParsers.ParseLotsListFromFile(lotsFilePath);
                Logger.Info(string.Format("Брой записи за жребии:{0}", lots.Count));
            }
            else
            {
                Logger.Info("Брой записи за жребии: 0");
            }

            try
            {
                var calc    = new MandatesCalculator(mirs, parties, votes, lots);
                var results = calc.CalculateMandates();

                if (results != null && results.Count > 0)
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Result.txt"))
                    {
                        foreach (var result in results)
                        {
                            string line = string.Format("{0};{1};{2}", result.MirId, result.PartyId, result.MandatesCount);
                            file.WriteLine(line);
                        }
                    }
                }
                else
                {
                    if (calc.IsLotReachedAndNoLots)
                    {
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Result.txt"))
                        {
                            file.WriteLine("0");
                            file.WriteLine("Достигнат жребий");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.logger.Error(e.Message);
            }
        }
Exemple #11
0
        public void CalculateMandates_Test_Sample_1_Standard()
        {
            //sample input
            string mirsFileContent    = @"1;""МИР1"";5
2;""МИР2"";4
3;""МИР3"";5
4;""МИР4"";10
5;""МИР5"";7
6;""МИР6"";12
7;""МИР7"";7
8;""Извън страната"";0";
            string partiesFileContent = @"1;""П1""
2;""П2""
3;""П3""
4;""П4""
5;""П5""
";
            string votesFileContent   = @"1;1;35121
1;2;20010
1;3;8456
1;4;18211
1;5;8200
2;1;23332
2;2;19888
2;3;10200
2;4;15501
2;5;5500
3;1;25678
3;2;27200
3;3;15002
3;4;11521
3;5;1520
4;1;70213
4;2;34556
4;3;41234
4;4;10750
4;5;750
5;1;41111
5;2;32090
5;3;37083
5;4;10882
5;5;805
6;1;75203
6;2;71234
6;3;55222
6;4;11764
6;5;1750
7;1;28800
7;2;23230
7;3;21121
7;4;21411
7;5;11200
8;1;10009
8;2;7512
8;3;5234
8;4;22056
8;5;2350";

            string resultsFileContent = @"1;1;3
1;2;1
1;4;1
2;1;1
2;2;1
2;3;1
2;4;1
3;1;1
3;2;2
3;3;1
3;4;1
4;1;4
4;2;2
4;3;3
4;4;1
5;1;2
5;2;2
5;3;2
5;4;1
6;1;4
6;2;4
6;3;3
6;4;1
7;1;3
7;2;2
7;3;1
7;4;1
";
            //parse collections
            IList <Mir>        mirs            = InputParsers.ParseMirsListFromFileContent(mirsFileContent);       // TODO: Initialize to an appropriate value
            IList <Party>      parties         = InputParsers.ParsePartiesListFromFileContent(partiesFileContent); // TODO: Initialize to an appropriate value
            IList <Vote>       votes           = InputParsers.ParseVotesListFromFileContent(votesFileContent);     // TODO: Initialize to an appropriate value
            IList <Result>     expectedResults = InputParsers.ParseResultsListFromFileContent(resultsFileContent);
            IList <Lot>        lots            = new List <Lot>();
            MandatesCalculator target          = new MandatesCalculator(mirs, parties, votes, lots); // TODO: Initialize to an appropriate value

            target.CalculateMandates();
            var actualResults = target.Results;

            Assert.IsTrue(CompareHelpers.AreEqualCollections <Result>(expectedResults, actualResults));
        }