Ejemplo n.º 1
0
        private static IEnumerable <string> GetPossibleStrings(string prefix, byte[] code)
        {
            Queue <KeyValuePair <string, byte[]> > queue = new Queue <KeyValuePair <string, byte[]> >();

            queue.Enqueue(new KeyValuePair <string, byte[]>(prefix, code));
            while (queue.Any())
            {
                var    kvp              = queue.Dequeue();
                string prefixSoFar      = kvp.Key;
                var    remainingBytes   = kvp.Value;
                var    intermediateCode = new Morse {
                    Code = remainingBytes
                };
                for (int i = 0; i <= remainingBytes.Count(); i++)
                {
                    intermediateCode = new Morse {
                        Code = remainingBytes.Take(i).ToList()
                    };
                    if (MorseToCharacter.ContainsKey(intermediateCode))
                    {
                        queue.Enqueue(new KeyValuePair <string, byte[]>(prefixSoFar + MorseToCharacter[intermediateCode],
                                                                        remainingBytes.Skip(i).ToArray()));
                    }
                }

                if (!MorseToCharacter.ContainsKey(intermediateCode))
                {
                    yield return(prefixSoFar);
                }
            }
        }
Ejemplo n.º 2
0
        private static IEnumerable<string> GetPossibleStrings(string prefix, byte[] code)
        {
            Queue<KeyValuePair<string, byte[]>> queue = new Queue<KeyValuePair<string, byte[]>>();
            queue.Enqueue(new KeyValuePair<string, byte[]>(prefix, code));
            while (queue.Any())
            {
                var kvp = queue.Dequeue();
                string prefixSoFar = kvp.Key;
                var remainingBytes = kvp.Value;
                var intermediateCode = new Morse { Code = remainingBytes };
                for (int i = 0; i <= remainingBytes.Count(); i++)
                {
                    intermediateCode = new Morse { Code = remainingBytes.Take(i).ToList() };
                    if (MorseToCharacter.ContainsKey(intermediateCode))
                    {
                        queue.Enqueue(new KeyValuePair<string, byte[]>(prefixSoFar + MorseToCharacter[intermediateCode],
                            remainingBytes.Skip(i).ToArray()));
                    }
                }

                if (!MorseToCharacter.ContainsKey(intermediateCode))
                {
                    yield return prefixSoFar;
                }
            }
        }