コード例 #1
0
ファイル: Morse.cs プロジェクト: pjantrania/we-get-signal
        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);
                }
            }
        }
コード例 #2
0
ファイル: Morse.cs プロジェクト: pjantrania/we-get-signal
        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;
                }
            }
        }