示例#1
0
文件: M2.cs 项目: nbduke/puzzles
        private double GetExpectedPrizeIncludingAllConsonants(ILotteryTicket ticket)
        {
            uint          numExcludedVowels  = Constants.NumberOfVowels - NumberOfGivenVowels;
            List <double> prizes             = new List <double>();
            List <double> excludedWords      = new List <double>();
            double        totalExcludedWords = 0;

            foreach (var combo in Vowels.GetCombinations(numExcludedVowels))
            {
                var givenVowels   = Constants.Vowels.Except(combo);
                var allGivenChars = Constants.Consonants.Concat(givenVowels);

                double prize            = ticket.GetPrize(allGivenChars);
                uint   numExcludedWords = ticket.Crossword.CountExcludedWords(combo);

                prizes.Add(prize);
                excludedWords.Add(numExcludedWords);
                totalExcludedWords += numExcludedWords;
            }

            return(CalculateExpectedPrize(prizes, excludedWords, totalExcludedWords));
        }
示例#2
0
文件: M0.cs 项目: nbduke/puzzles
        public double GetExpectedPrize(ILotteryTicket ticket)
        {
            double expectedPrize = 0;
            double probability   = 1.0 / Combinatorics.GetNumberOfCombinations(
                Constants.AlphabetSize, ticket.NumberOfGivenCharacters);

            foreach (var combo in Alphabet.GetCombinations(ticket.NumberOfGivenCharacters))
            {
                double prize = ticket.GetPrize(combo);
                expectedPrize += prize * probability;
            }

            return(expectedPrize);
        }
示例#3
0
文件: M1.cs 项目: nbduke/puzzles
        public double GetExpectedPrize(ILotteryTicket ticket)
        {
            if (ticket.NumberOfGivenCharacters == 0)
            {
                return(0);
            }
            else if (ticket.NumberOfGivenCharacters == Constants.AlphabetSize)
            {
                return(ticket.MaxPrize);
            }

            uint          numExcludedCharacters = Constants.AlphabetSize - ticket.NumberOfGivenCharacters;
            List <double> prizes             = new List <double>();
            List <double> excludedWords      = new List <double>();
            double        totalExcludedWords = 0;

            // For each combination, compute the number of words excluded by the combination and
            // store its prize.
            foreach (var combo in Alphabet.GetCombinations(numExcludedCharacters))
            {
                var    givenCharacters  = Constants.Alphabet.Except(combo);
                double prize            = ticket.GetPrize(givenCharacters);
                uint   numExcludedWords = ticket.Crossword.CountExcludedWords(combo);

                prizes.Add(prize);
                excludedWords.Add(numExcludedWords);
                totalExcludedWords += numExcludedWords;
            }

            // Compute the expected prize
            double expectedPrize = 0;

            for (int i = 0; i < excludedWords.Count; ++i)
            {
                expectedPrize += prizes[i] * (excludedWords[i] / totalExcludedWords);
            }

            return(expectedPrize);
        }