You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TrustPilotChallenge/WhiteRabbit/StringsProcessor.cs

87 lines
3.3 KiB

8 years ago
namespace WhiteRabbit
{
using System;
8 years ago
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
8 years ago
internal sealed class StringsProcessor
8 years ago
{
public StringsProcessor(byte[] sourceString, int maxWordsCount, IEnumerable<byte[]> words)
8 years ago
{
var filteredSource = sourceString.Where(ch => ch != 32).ToArray();
this.NumberOfCharacters = filteredSource.Length;
8 years ago
this.VectorsConverter = new VectorsConverter(filteredSource);
// Dictionary of vectors to array of words represented by this vector
this.VectorsToWords = words
.Distinct(new ByteArrayEqualityComparer())
.Select(word => new { word, vector = this.VectorsConverter.GetVector(word) })
.Where(tuple => tuple.vector != null)
.Select(tuple => new { tuple.word, vector = tuple.vector.Value })
.GroupBy(tuple => tuple.vector)
.ToDictionary(group => group.Key, group => group.Select(tuple => tuple.word).ToArray());
8 years ago
this.VectorsProcessor = new VectorsProcessor(
this.VectorsConverter.GetVector(filteredSource).Value,
maxWordsCount,
this.VectorsToWords.Keys,
8 years ago
this.VectorsConverter.GetString);
}
private VectorsConverter VectorsConverter { get; }
private Dictionary<Vector<byte>, byte[][]> VectorsToWords { get; }
8 years ago
private VectorsProcessor VectorsProcessor { get; }
private int NumberOfCharacters { get; }
public ParallelQuery<byte[]> GeneratePhrases()
8 years ago
{
8 years ago
// task of finding anagrams could be reduced to the task of finding sequences of dictionary vectors with the target sum
var sums = this.VectorsProcessor.GenerateSequences();
8 years ago
8 years ago
// converting sequences of vectors to the sequences of words...
8 years ago
var anagramsWords = sums
.Select(sum => ImmutableStack.Create(sum.Select(vector => this.VectorsToWords[vector]).ToArray()))
.SelectMany(this.Flatten)
8 years ago
.Select(stack => stack.ToArray());
return anagramsWords.Select(WordsToPhrase);
8 years ago
}
8 years ago
// Converts e.g. pair of variants [[a, b, c], [d, e]] into all possible pairs: [[a, d], [a, e], [b, d], [b, e], [c, d], [c, e]]
private IEnumerable<ImmutableStack<T>> Flatten<T>(ImmutableStack<T[]> phrase)
8 years ago
{
if (phrase.IsEmpty)
{
8 years ago
return new[] { ImmutableStack.Create<T>() };
8 years ago
}
8 years ago
T[] wordVariants;
8 years ago
var newStack = phrase.Pop(out wordVariants);
return this.Flatten(newStack).SelectMany(remainder => wordVariants.Select(word => remainder.Push(word)));
8 years ago
}
private byte[] WordsToPhrase(byte[][] words)
{
var result = new byte[this.NumberOfCharacters + words.Length - 1];
Buffer.BlockCopy(words[0], 0, result, 0, words[0].Length);
var position = words[0].Length;
for (var i = 1; i < words.Length; i++)
{
result[position] = 32;
position++;
Buffer.BlockCopy(words[i], 0, result, position, words[i].Length);
position += words[i].Length;
}
return result;
}
8 years ago
}
}