Exemple #1
0
        private static IEnumerable <Words> SentenceAnagramsIter(Occurrences occurrences)
        {
            var occurrencesAsList = occurrences.ToList();

            if (!occurrencesAsList.Any())
            {
                yield return(new List <Word>());

                yield break;
            }

            foreach (var combination in Combinations(occurrencesAsList))
            {
                var combinationAsList    = combination.ToList();
                var remainingOccurrences = Subtract(occurrencesAsList, combinationAsList).ToList();
                foreach (var word in WordsForCombination(combinationAsList))
                {
                    foreach (var innerSentence in _sentenceAnagramsIterFunc(remainingOccurrences))
                    {
                        var sentence = new List <Word> {
                            word
                        };
                        sentence.AddRange(innerSentence);
                        yield return(sentence);
                    }
                }
            }
        }
Exemple #2
0
        private static IEnumerable <Occurrences> Combinations(Occurrences occurrences)
        {
            var occurrencesAsList = occurrences.ToList();

            if (!occurrencesAsList.Any())
            {
                yield return(new List <Tuple <char, int> >());

                yield break;
            }

            var hd             = occurrencesAsList.First();
            var tl             = occurrencesAsList.Skip(1);
            var c              = hd.Item1;
            var n              = hd.Item2;
            var tlCombinations = Combinations(tl).ToList();

            foreach (var x in Enumerable.Range(0, n + 1))
            {
                var first = (x > 0) ? Tuple.Create(c, x) : null;
                foreach (var others in tlCombinations)
                {
                    var y = new List <Tuple <char, int> >();
                    if (first != null)
                    {
                        y.Add(first);
                    }
                    y.AddRange(others);
                    yield return(y);
                }
            }
        }