示例#1
0
        static void Main(string[] args)
        {
            //NumberOperations numberOperations = Sum;
            //numberOperations += Minus;
            //numberOperations += Difference;
            //numberOperations += Multiple;
            //numberOperations.Invoke(5, 8);

            NumberOperations secondNumberOperations = Minus;
            Answer           answer;
            int money = 5000;

            Console.WriteLine($"Ваши деньги: {money}");
            Console.WriteLine("Сколько денег вы хотите снять со счёта?");
            int input = Convert.ToInt32(Console.ReadLine());

            if (input > 0 && secondNumberOperations(money, input) >= 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                answer = Answer;
                answer.Invoke(secondNumberOperations(money, input));
                Console.ResetColor();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                answer = WrongAnswer;
                answer.Invoke(secondNumberOperations(money, input));
                Console.ResetColor();
            }
        }
示例#2
0
        public void Test_GenerateSubsets()
        {
            var subSets = NumberOperations.GetSubsetsLessOrEqualThanSum(new int[] { 1, 2, 3, 4, 5 }, 5);

            List <List <int> > testList = new List <List <int> >()
            {
                new List <int> {
                    1, 2
                },
                new List <int> {
                    1
                },
                new List <int> {
                    2, 3
                },
                new List <int> {
                    2
                },
                new List <int> {
                    3
                },
                new List <int> {
                    4
                },
                new List <int> {
                    5
                }
            };

            Assert.Equal(testList, subSets);
        }
示例#3
0
        // ===============================================================================================
        // method to display output to grasshopper with some optimization in conversion
        // ===============================================================================================
        public void DisplayToGrasshopper()
        {
            if (Containment[0].Label == 's')
            {
                _container = (SurfaceContainment)Containment[0];

                var nu = NumberOperations.remap(XMin, XMax, _container.Surface.Domain(0).T0,
                                                _container.Surface.Domain(0).T1, Position.X);
                var nv = NumberOperations.remap(YMin, YMax, _container.Surface.Domain(1).T0,
                                                _container.Surface.Domain(1).T1, Position.Y);

                var vu = NumberOperations.remap(XMin, XMax, _container.Surface.Domain(0).T0,
                                                _container.Surface.Domain(0).T1, Velocity.X);
                var vv = NumberOperations.remap(YMin, YMax, _container.Surface.Domain(1).T0,
                                                _container.Surface.Domain(1).T1, Velocity.Y);

                GHPosition = new GH_Point(_container.Surface.PointAt(nu, nv));
                GHVelocity = new GH_Vector(new Vector3d(_container.Surface.PointAt(vu, vv)));
            }
            else
            {
                GHPosition = new GH_Point(Position);
                GHVelocity = new GH_Vector(Velocity);
            }
        }
示例#4
0
        public void Test_IsTriplePythagorean()
        {
            var result1 = NumberOperations.IsTriplePythagorean(3, 4, 5);
            var result2 = NumberOperations.IsTriplePythagorean(1, 2, 3);

            Assert.True(result1);
            Assert.False(result2);
        }
示例#5
0
        public void Test_SumCombinations_Should_Return_Unique_combination_MoreComplex_case()
        {
            //Given, When
            var expression = NumberOperations.GetSumCombinations(3, 0);

            //Then
            Assert.Equal(new[] { "+1+2-3 = 0", "-1-2+3 = 0" }, expression);
        }
示例#6
0
        public void Test_SumCombinations_Should_Return_Primitive_Expression()
        {
            //Given, When
            var expression = NumberOperations.GetSumCombinations(1, 1);

            //Then
            Assert.Equal(new[] { "+1 = 1" }, expression);
        }
示例#7
0
        public void Test_SumCombinations_Should_Return_EmptyEnum_when_NoCombination_is_Possible()
        {
            //Given, When
            var expression = NumberOperations.GetSumCombinations(2, 6);

            //Then
            Assert.Empty(expression);
        }
示例#8
0
        public void Test_SumCombinations_Should_Add_Minus_between_Operands()
        {
            //Given, When
            var expression = NumberOperations.GetSumCombinations(2, -1);

            //Then
            Assert.Equal(new[] { "+1-2 = -1" }, expression);
        }
示例#9
0
        public void Test_SumCombinations_For_Simple_Case()
        {
            //Given, When
            var expression = NumberOperations.GetSumCombinations(2, 3);

            //Then
            Assert.Equal(new[] { "+1+2 = 3" }, expression);
        }
示例#10
0
        public void Test_PythagoreanNumbers_for_Longer_Array()
        {
            //Given
            int[] array = { 1, 2, 3, 4, 5 };
            //When
            var subSets = NumberOperations.GetPythagoreanNumbers(array);

            //Then
            Assert.Equal(new[] { new[] { 3, 4, 5 }, new[] { 4, 3, 5 } }, subSets);
        }
示例#11
0
        public void Test_PythagoreanNumbers_Array_Has_3_elements()
        {
            //Given
            int[] array = { 3, 4, 5 };
            //When
            var subSets = NumberOperations.GetPythagoreanNumbers(array);

            //Then
            Assert.Equal(new[] { new[] { 3, 4, 5 }, new[] { 4, 3, 5 } }, subSets);
        }
示例#12
0
        public void Test_GenerateSubsets_Simple_Case()
        {
            //Given
            int[] array = { 1, 2 };
            //When
            var subSets = NumberOperations.GenerateSubsets(array, 1);

            //Then
            Assert.Equal(new[] { new int[] { 1 } }, subSets);
        }
示例#13
0
        public void Test_GenerateSubsets_No_SubSet_Should_Be_Found()
        {
            //Given
            int[] array = { 5, 10 };
            //When
            var subSets = NumberOperations.GenerateSubsets(array, 3);

            //Then
            Assert.Empty(subSets);
        }
示例#14
0
        public void Test_PythagoreanNumbers_When_Array_Has_Repeating_Elements()
        {
            //Given
            int[] array = { 0, 0, 0 };
            //When
            var subSets = NumberOperations.GetPythagoreanNumbers(array);

            //Then
            Assert.Equal(new[] { new[] { 0, 0, 0 } }, subSets);
        }
示例#15
0
        public void Test_PythagoreanNumbers_Should_Return_Empty_Enumerable_for_ShortArray()
        {
            //Given
            int[] array = { 1, 2 };
            //When
            var subSets = NumberOperations.GetPythagoreanNumbers(array);

            //Then
            Assert.Empty(subSets);
        }
示例#16
0
        public void Test_PythagoreanNumbers_Check_That_AllCombinations_are_Checked()
        {
            //Given
            int[] array = { 3, 1, 5, 4 };
            //When
            var subSets = NumberOperations.GetPythagoreanNumbers(array);

            //Then
            Assert.Equal(new[] { new[] { 3, 4, 5 }, new[] { 4, 3, 5 } }, subSets);
        }
示例#17
0
        public void Test_GetAllPythagoreanPairs_0AsValues()
        {
            var result = NumberOperations.GetAllPythagoreanPairs(new int[] { 0, 0, 0 });

            var expected = new int[1][] {
                new[] { 0, 0, 0 }
            };

            Assert.Equal(result, expected);
        }
示例#18
0
        public void Test_GenerateEquations()
        {
            var result = NumberOperations.GenerateEquations(3, 0);

            var expected = new string[] {
                "+1+2-3 = 0",
                "-1-2+3 = 0"
            };

            Assert.Equal(result, expected);
        }
示例#19
0
        public void Test_GetAllPythagoreanPairs()
        {
            var result = NumberOperations.GetAllPythagoreanPairs(new int[] { 1, 2, 3, 4, 5, 12, 13 });

            var expected = new int[4][] {
                new[] { 3, 4, 5 }, new[] { 4, 3, 5 },
                new[] { 5, 12, 13 }, new[] { 12, 5, 13 }
            };

            Assert.Equal(result, expected);
        }
示例#20
0
        public void Test_GetAllPythagoreanPairPermutations()
        {
            var result = NumberOperations.GetAllPythagoreanPairPermutations(new int[] { 5, 4, 3 });

            var expected = new int[2][] {
                new int[] { 4, 3, 5 },
                new int[] { 3, 4, 5 },
            };

            Assert.Equal(result, expected);
        }
示例#21
0
        public void Test_GetAllPlusMinusCombinations_LengthOfFour()
        {
            var result = NumberOperations.GetAllPlusMinusCombinations(4);

            var expected = new string[] {
                "++++", "+++-", "++-+", "++--", "+-++", "+-+-", "+--+", "+---",
                "-+++", "-++-", "-+-+", "-+--", "--++", "--+-", "---+", "----",
            };

            Assert.Equal(result, expected);
        }
示例#22
0
        public void Test_PythagoreanNumbers_FinalTest()
        {
            //Given
            int[] array = { 1, 2, 3, 4, 5, 12, 13 };
            //When
            var subSets = NumberOperations.GetPythagoreanNumbers(array);

            //Then
            Assert.Equal(new[] { new[] { 3, 4, 5 }, new[] { 4, 3, 5 },
                                 new[] { 5, 12, 13 }, new[] { 12, 5, 13 } }
                         , subSets);
        }
示例#23
0
 public override SyntaxNode VisitArgument(ArgumentSyntax node)
 {
     if (node.Expression.Kind().Equals(SyntaxKind.ParenthesizedExpression))
     {
         NumberOperations ne = new NumberOperations();
         ParenthesizedExpressionSyntax pe = (ParenthesizedExpressionSyntax)node.Expression;
         ExpressionSyntax es      = SyntaxFactory.ParseExpression(ne.ExpressionParser(pe.Expression.ToString()).ToString());
         ArgumentSyntax   newNode = node.WithExpression(es);
         return(base.VisitArgument(node.ReplaceNode(node, newNode)));
     }
     return(base.VisitArgument(node));
 }
示例#24
0
        public void Test_GenerateSubsets_Longer_Collection()
        {
            //Given
            int[] array = { 1, 2, 3, 5, 4 };
            //When
            IEnumerable <IEnumerable <int> > enumerable = NumberOperations.GenerateSubsets(array, 4);

            //Then
            Assert.Equal(new[]
                         { new int[] { 1, 2 }, new int[] { 1 },
                           new int[] { 2 }, new int[] { 3 }, new int[] { 4 } }
                         , enumerable);
        }
示例#25
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
// ===============================================================================================
// Read input parameters
// ===============================================================================================
            //string info;
            double         minVelocity = 4;
            double         maxVelocity = 8;
            List <Point3d> points      = new List <Point3d>();
            var            agents      = new List <FlockAgent>();
            const double   xMin        = 0;
            const double   xMax        = 30;
            const double   yMin        = 0;
            const double   yMax        = 30;

            //Get values from grasshopper
            DA.GetData("Minimum velocity", ref minVelocity);
            DA.GetData("Maximum velocity", ref maxVelocity);
            DA.GetDataList("Start point for agent", points);
// ===============================================================================================
// Applying Values to Class
// ===============================================================================================
            BoundingBox box = new BoundingBox(points);

            //Assign velocity to points
            foreach (Point3d point in points)
            {
                var nu = NumberOperations.remap(box.Min.X,
                                                box.Max.X, xMin, xMax, point.X);
                var nv = NumberOperations.remap(box.Min.Y,
                                                box.Max.Y, yMin, yMax, point.Y);
                var remappedPoint = new Point3d(nu, nv, 0);

                FlockAgent agent =
                    new FlockAgent(remappedPoint, VectorOperations.GetRandomUnitVectorXY() * minVelocity)
                {
                    StartPosition = remappedPoint,
                    MinVelocity   = minVelocity,
                    MaxVelocity   = maxVelocity
                };
                agents.Add(agent);
            }
// ===============================================================================================
// Exporting Data to Grasshopper
// ===============================================================================================
            //assigning data for export
            var a = agents;

            //Export data to grasshopper
            DA.SetDataList(0, a);
        }
示例#26
0
        public void Test_GenerateEquationsComplex()
        {
            var result = NumberOperations.GenerateEquations(7, 10);

            var expected = new string[] {
                "+1+2+3-4-5+6+7 = 10",
                "+1+2-3+4+5-6+7 = 10",
                "+1-2+3+4+5+6-7 = 10",
                "+1-2-3-4+5+6+7 = 10",
                "-1+2-3+4-5+6+7 = 10",
                "-1-2+3+4+5-6+7 = 10"
            };

            Assert.Equal(result, expected);
        }
示例#27
0
        public void Test_SumCombinations_Should_Return_More_Expressions_For_More_Complex_Case()
        {
            //Given, When
            var expression = NumberOperations.GetSumCombinations(7, 10);

            //Then
            Assert.Equal(new[]
            {
                "+1+2+3-4-5+6+7 = 10",
                "+1+2-3+4+5-6+7 = 10",
                "+1-2+3+4+5+6-7 = 10",
                "+1-2-3-4+5+6+7 = 10",
                "-1+2-3+4-5+6+7 = 10",
                "-1-2+3+4+5-6+7 = 10"
            }, expression);
        }
示例#28
0
        public void Test_GetAllConsecutiveTriplePairs()
        {
            var result = NumberOperations.GetAllDistinctTriplePairs(new int[] { 1, 2, 3, 4, 5 });

            var expected = new int[10][] {
                new int[] { 1, 2, 3 },
                new int[] { 1, 2, 4 },
                new int[] { 1, 2, 5 },
                new int[] { 1, 3, 4 },
                new int[] { 1, 3, 5 },
                new int[] { 1, 4, 5 },
                new int[] { 2, 3, 4 },
                new int[] { 2, 3, 5 },
                new int[] { 2, 4, 5 },
                new int[] { 3, 4, 5 },
            };

            Assert.Equal(result, expected);
        }
示例#29
0
        // ===============================================================================================
        // method to add behaviours to agents
        // ===============================================================================================
        public void ComputeDesiredVelocity(List <IFlockAgent> neighbours)
        {
            // First, reset the desired velocity to 0
            _desiredVelocity = new Vector3d(0.0, 0.0, 0.0);
            // Pull the agent back if it gets out of the bounding box
            foreach (var icontainment in Containment)
            {
                _desiredVelocity += icontainment.DesiredVector(Position, _desiredVelocity);
            }
            // If there are no neighbours nearby, the agent will maintain its veloctiy,
            // else it will perform the "alignment", "cohension" and "separation" behaviours
            if (neighbours.Count == 0)
            {
                _desiredVelocity += Velocity; // maintain the current velocity
            }
            else
            {
                // -------------------------------------------------------------------------------
                // "Alignment" behavior
                // -------------------------------------------------------------------------------
                _desiredVelocity += AgentBehaviours.Alignment(neighbours, _desiredVelocity, FlockSystem);
                // -------------------------------------------------------------------------------
                // "Cohesion" behavior
                // -------------------------------------------------------------------------------
                _desiredVelocity += AgentBehaviours.Cohesion(neighbours, Position, _desiredVelocity, FlockSystem);
                // -------------------------------------------------------------------------------
                // "Separation" behavior
                // -------------------------------------------------------------------------------
                _desiredVelocity += AgentBehaviours.Separation(neighbours, Position, _desiredVelocity, FlockSystem);
            }
            // -------------------------------------------------------------------------------
            //// Interactionbehaviours
            // -------------------------------------------------------------------------------
            foreach (var interaction in Interactions)
            {
                switch (interaction.BehaviourType)
                {
                //=======================================================================================
                //if interaction is repeller
                case BehaviourType.Repeller:
                    if (Containment[0].Label == 's')
                    {
                        _container = (SurfaceContainment)Containment[0];

                        foreach (var repeller in interaction.Circles)
                        {
                            // Remap repellers
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(repeller.Center, out u, out v);

                            var nu = NumberOperations.remap(SrfBoudningBox.Min.X,
                                                            SrfBoudningBox.Max.X, XMin, XMax, u);
                            var nv = NumberOperations.remap(SrfBoudningBox.Min.Y,
                                                            SrfBoudningBox.Max.Y, YMin, YMax, v);
                            Point3d remappedCenter = new Point3d(nu, nv, 0);
                            Circle  remappedCircle = new Circle(remappedCenter, repeller.Radius);
                            _surfaceRepller.Add(remappedCircle);
                        }
                        FlockSystem.Repellers = _surfaceRepller;
                    }
                    else
                    {
                        FlockSystem.Repellers = interaction.Circles;
                    }

                    break;

                //=======================================================================================
                //if interaction is Attractor
                case BehaviourType.Attractor:
                    if (Containment[0].Label == 's')
                    {
                        _container = (SurfaceContainment)Containment[0];

                        foreach (var attractor in interaction.Circles)
                        {
                            // Remap Attractors
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(attractor.Center, out u, out v);

                            var nu = NumberOperations.remap(SrfBoudningBox.Min.X,
                                                            SrfBoudningBox.Max.X, XMin, XMax, u);
                            var nv = NumberOperations.remap(SrfBoudningBox.Min.Y,
                                                            SrfBoudningBox.Max.Y, YMin, YMax, v);
                            Point3d remappedCenter = new Point3d(nu, nv, 0);
                            Circle  remappedCircle = new Circle(remappedCenter, attractor.Radius);
                            _surfaceAttractors.Add(remappedCircle);
                        }
                        FlockSystem.Attractors = _surfaceAttractors;
                    }
                    else
                    {
                        FlockSystem.Attractors = interaction.Circles;
                    }

                    var closestPoint = PointOperations.ClosestPoints(Position, FlockSystem.Attractors.Select(p => p.Center).ToList(), 1);
                    interaction.ClosestPoint = closestPoint[0];
                    break;

                //=======================================================================================
                //if interaction is Attractor curve
                case BehaviourType.AttractorCurve:

                    if (Containment[0].Label == 's')
                    {
                        //getting curve data: points and degree
                        _container = (SurfaceContainment)Containment[0];
                        var interactionAttractorCurve = (AttractorCurve)interaction;
                        var attractorcurve            = interactionAttractorCurve.Curves[0].ToNurbsCurve();
                        var controlpoints             = attractorcurve.Points;
                        var degree = attractorcurve.Degree;

                        foreach (var controlpoint in controlpoints)
                        {
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(controlpoint.Location, out u, out v);

                            var nu = NumberOperations.remap(_container.Surface.Domain(0).T0,
                                                            _container.Surface.Domain(0).T1, XMin, XMax, u);
                            var nv = NumberOperations.remap(_container.Surface.Domain(1).T0,
                                                            _container.Surface.Domain(1).T1, YMin, YMax, v);
                            Point3d remappedControlPoint = new Point3d(nu, nv, 0);
                            _remappedPoints.Add(remappedControlPoint);
                        }
                        var remappedCurve = Curve.CreateControlPointCurve(_remappedPoints, degree);
                        _remappedCurves.Add(remappedCurve);
                        FlockSystem.AttractorCurves = _remappedCurves;
                    }
                    else
                    {
                        FlockSystem.AttractorCurves = interaction.Curves;
                    }


                    var attractorCast = (AttractorCurve)interaction;
                    FlockSystem.AttractorCurvesSwitch = attractorCast.AttractorCurveSwitch;

                    //FlockSystem.AttractorCurves[0].ClosestPoint(StartPosition, out t);
                    //var curveClosestPoint = FlockSystem.AttractorCurves[0].PointAt(t);
                    Point3d curveClosestPoint;

                    if (FlockSystem.AttractorCurvesSwitch)
                    {
                        List <Point3d> curveClosestPoints = new List <Point3d>();
                        foreach (var attractorCurve in FlockSystem.AttractorCurves)
                        {
                            double t;
                            attractorCurve.ClosestPoint(StartPosition, out t);
                            curveClosestPoints.Add(attractorCurve.PointAt(t));
                        }
                        curveClosestPoint = PointOperations.ClosestPoints(StartPosition, curveClosestPoints, 1)[0];
                    }
                    else
                    {
                        List <Point3d> curveClosestPoints = new List <Point3d>();
                        foreach (var attractorCurve in FlockSystem.AttractorCurves)
                        {
                            double t;
                            attractorCurve.ClosestPoint(Position, out t);
                            curveClosestPoints.Add(attractorCurve.PointAt(t));
                        }
                        curveClosestPoint = PointOperations.ClosestPoints(Position, curveClosestPoints, 1)[0];
                    }


                    interaction.ClosestPoint = curveClosestPoint;
                    break;

                //=======================================================================================
                //if interaction is Repeller curve
                case BehaviourType.RepellerCurve:

                    if (Containment[0].Label == 's')
                    {
                        //getting curve data: points and degree
                        _container = (SurfaceContainment)Containment[0];
                        var interactionRepellerCurve = (RepellerCurve)interaction;
                        var attractorcurve           = interactionRepellerCurve.Curves[0].ToNurbsCurve();
                        var controlpoints            = attractorcurve.Points;
                        var degree = attractorcurve.Degree;

                        foreach (var controlpoint in controlpoints)
                        {
                            double u;
                            double v;
                            _container.Surface.ClosestPoint(controlpoint.Location, out u, out v);

                            var nu = NumberOperations.remap(_container.Surface.Domain(0).T0,
                                                            _container.Surface.Domain(0).T1, XMin, XMax, u);
                            var nv = NumberOperations.remap(_container.Surface.Domain(1).T0,
                                                            _container.Surface.Domain(1).T1, YMin, YMax, v);
                            Point3d remappedControlPoint = new Point3d(nu, nv, 0);
                            _remappedPoints.Add(remappedControlPoint);
                        }
                        var remappedCurve = Curve.CreateControlPointCurve(_remappedPoints, degree);
                        _remappedCurves.Add(remappedCurve);
                        FlockSystem.RepllerCurves = _remappedCurves;
                    }
                    else
                    {
                        FlockSystem.RepllerCurves = interaction.Curves;
                    }

                    //FlockSystem.RepllerCurves[0].ClosestPoint(Position, out t);
                    //var repellerCurveClosestPoint = FlockSystem.RepllerCurves[0].PointAt(t);
                    List <Point3d> repellerCurveClosestPoints = new List <Point3d>();
                    foreach (var repellerCurve in FlockSystem.RepllerCurves)
                    {
                        double t;
                        repellerCurve.ClosestPoint(Position, out t);
                        repellerCurveClosestPoints.Add(repellerCurve.PointAt(t));
                    }
                    var repellerCurveClosestPoint = PointOperations.ClosestPoints(Position, repellerCurveClosestPoints, 1)[0];

                    interaction.ClosestPoint = repellerCurveClosestPoint;
                    break;

                //=======================================================================================
                //if interaction is Follow Points
                case BehaviourType.FollowPoints:
                    FlockSystem.FollowAttractors = interaction.Circles;
                    break;

                //=======================================================================================
                //if interaction is follow curve
                case BehaviourType.FollowCurve:
                    FlockSystem.FollowCurveAttractors = interaction.Circles;
                    break;

                //=======================================================================================
                //if interaction is Wind
                case BehaviourType.Wind:
                    FlockSystem.Wind = interaction.WindVec;
                    break;
                }
                interaction.Position        = Position;
                interaction.FlockSystem     = FlockSystem;
                interaction.DesiredVelocity = _desiredVelocity;
                _desiredVelocity           += interaction.ComputeDesiredVelocity();
            }
        }
示例#30
0
        public void Test_GetAllPythagoreanPairs_ShouldReturnEmpty()
        {
            var result = NumberOperations.GetAllPythagoreanPairs(new int[] { 1, 2 });

            Assert.Empty(result);
        }