예제 #1
0
        public static IEnumerable <T> FindMissingNumbersFromSequence <T>(IEnumerable <T> arr, MinMax <T> mnmx = null) where T : struct
        {
            if (!arr.Any(x => x.IsNumericType()))
            {
                Console.WriteLine("Type '{0}' can't be used as a numeric type!", typeof(T).Name);
                yield break;
            }

            if (mnmx != null)
            {
                arr.Add(mnmx.min);
                arr.Add(mnmx.max);
            }

            IOrderedEnumerable <T> list = arr.OrderBy(x => x);
            T n = default(T);

            foreach (T num in list)
            {
                T op = (dynamic)num - n;
                if ((dynamic)op > 1)
                {
                    int max = op.ConvertValue <int>();
                    for (int l = 1; l < max; ++l)
                    {
                        yield return((dynamic)n + l.ConvertValue <T>());
                    }
                }
                n = (dynamic)num;
            }
        }
예제 #2
0
        public static bool FindFirstMissingNumberFromSequence <T>(this IEnumerable <T> arr, out T n, MinMax <T> mnmx = null)
        {
            //Dupe

            if (!arr.Any(x => x.IsNumericType()))
            {
                Console.WriteLine("Type '{0}' can't be used as a numeric type!", typeof(T).Name);
                n = default(T);
                return(false);
            }

            if (mnmx != null)
            {
                arr.Add(mnmx.min);
                arr.Add(mnmx.max);
            }

            IOrderedEnumerable <T> list = arr.OrderBy(x => x);

            //End dupe

            bool b = false;

            n = default(T);

            foreach (T num in list)
            {
                b = (dynamic)num - n > 1;
                if (b)
                {
                    break;
                }
                else
                {
                    n = (dynamic)num;
                }
            }

            n = (dynamic)n + 1;

            return(b);
        }