Пример #1
0
        public static void Main()
        {
            TrippleSum.Do();
            ConnectNodesAtTheSameLevel.Do();
            JumpingNumbers.Do();
            LongestValidSubstring.Do();
            SizeOfSubarrayWithMaximumSum.Do();
            SubarrayWithSumNonNegative.Do();
            SubArrayWithNegativeSum.Do();
            SmallestPositiveInteger.Do();
            CommonAncestor.Do();
            BinaryTreeIsBST.Do();
            BalancedBinaryTree.Do();
            ReverseList.Do();
            SumToGivenValue.Do();
            Rectangle.Do();
            Parentheses.Do();

            FindTripplets.Do();
            SubTree.Do();
            NumberOfIntInArray.Do();
            MinimumArrayOfSumGreater.Do();
            ReplaceNumbers.Do();
            SubArrayWithSum.Do();
            SmallestPositiveInteger.Do();
        }
Пример #2
0
        public void CommonAncestorTest(int[] arr, int target1, int target2, int expected)
        {
            (var _, var n1, var n2) = CommonAncestor.FromArray(arr, target1, target2);
            var actual = CommonAncestor.GetCommonAncestor(n1, n2);

            if (expected == -1)
            {
                Assert.Null(actual);
            }
            else
            {
                Assert.Equal(expected, actual.Data);
            }
        }
Пример #3
0
        /// <summary>
        /// Learning a `Lift`, i.e. node lifter, that is consistent with the specification.
        /// This method only identifies the lowest scope that covers all examples.
        /// </summary>
        /// <param name="spec">Specification of the form: input -> node to be referenced.</param>
        /// <param name="sourceSpec">Constraint on the `source` of `Lift`: input -> source node.</param>
        /// <param name="source">The program which constructs the `source`.</param>
        /// <param name="scopeSpec">Output. Specification for learning `scope`: input -> scope root.</param>
        /// <returns>Consistent program (if exist) or nothing.</returns>
        private Optional <ProgramNode> LearnLift(PremSpec <TInput, SyntaxNode> spec, PremSpec <TInput, Leaf> sourceSpec,
                                                 ProgramNode source, out PremSpec <TInput, Node> scopeSpec)
        {
            scopeSpec = spec.MapOutputs((i, o) => CommonAncestor.LCA(o, sourceSpec[i]));

            while (scopeSpec.Forall((i, o) => o != null))
            {
                Log.Tree("iter scopeSpec = {0}", scopeSpec);

                Label label;
                int   k;
                if (scopeSpec.Identical((i, o) => o.label, out label))
                {
                    if (scopeSpec.Identical((i, o) => sourceSpec[i].CountAncestorWhere(
                                                n => n.label.Equals(label), o.id), out k))
                    {
                        return(Lift(source, label, k).Some());
                    }

                    // else: lift more, simultaneously
                    scopeSpec = scopeSpec.MapOutputs((i, o) => o.parent);
                    continue;
                }

                // labels are different: try lift all of them to the highest
                var highest = scopeSpec.ArgMin(p => p.Value.depth);
                label = highest.Value.label;
                if (!scopeSpec.Forall((i, o) => i.Equals(highest.Key) ? true :
                                      o.Ancestors().Any(n => n.label.Equals(label))))
                {
                    // Log.Debug("Lift: impossible to lift all of them to the highest");
                    return(Optional <ProgramNode> .Nothing);
                }

                scopeSpec = scopeSpec.MapOutputs((i, o) => i.Equals(highest.Key) ? o :
                                                 o.Ancestors().First(n => n.label.Equals(label)));
            }

            Log.Debug("Lift: loop ends");
            return(Optional <ProgramNode> .Nothing);
        }