如何返回对函数的递归调用?

时间:2019-04-26 04:32:46

标签: python python-3.x recursion palindrome

任务:

编写一个名为is_palindrome的递归函数,该函数将一个名为word的字符串作为其参数,如果word的长度小于或等于1,则返回True。如果word的长度大于1,则该函数应返回False(如果第一个)单词的字符不同于单词的最后一个字符。如果单词的长度大于1,并且字符串的第一个和最后一个字符相同,则该函数应返回is_palindrome()的结果,并删除单词的第一个和最后一个字符(例如is_palindrome(“ anna “)应该返回is_palindrome(” nn“))的结果。

我的代码:

'any contiguous part of the discontiguous union can be grouped
=SUM(COUNTIF(INDIRECT({"A1","A3","B3","B4","C5"}),"X"))=5
=SUM(COUNTIF(INDIRECT({"A1","A3:B3","B4","C5"}),"X"))=5
=SUM(COUNTIF(INDIRECT({"A1","A3","B3:B4","C5"}),"X"))=5

评分反馈:

您需要返回对is_palindrome的递归调用

1 个答案:

答案 0 :(得分:0)

检查字符串的端点(第一个和最后一个字符),如果相等,则返回对其余字符(除第一个和最后一个字符之外的所有字符)的调用:

    //Point of the method is to put variations of Baby into an array, and return that array
        Dot.Class[] MutateAndListBaby(Dot.Class Baby)
        {
            //Making the empty array
            Dot.Class[] BabyList = new Dot.Class[dots.Length];

            //For loop that goes through through the whole array
            for (int i = 1; i < BabyList.Length; i++)
            {
                //For each itteration the for loop adds the class reference to the index, then puts the standard directions into that reference, and then sets a value preventing it from being changed in another code
                BabyList[i] = new Dot.Class();
                BabyList[i].Directions = Baby.Directions;
                BabyList[i].StartupComplete = true;

                //The zero index variation when made like this, allows it to not be overriden, which would lead one to believe that how the directions are copied is he problem
                //But it shouldn't be, BabyList[i].Directions = Baby.Directions; should be fire and forget, it should just add the Directions to the array and then leave it


                BabyList[0] = new Dot.Class();
                BabyList[0].Directions = new PointF[100];
                for (int b = 0; b < BabyList[0].Directions.Length; b++)
                {
                    BabyList[0].Directions[b] = new Point (5, 10);
                }

                BabyList[0].StartupComplete = true;

                //The for loop that shuld add variation, but it seems like it somehow overrides it self, somehow
                for (int b = 0; b < BabyList[i].Directions.Length; b++)
                {

                    if (rand.Next(0, 101) >= 100)
                    {


                            int rando = rand.Next(-50, 51);
                            float mod = (float)rando / 50;
                            float x = BabyList[i].Directions[b].X;

                            x = x + mod;

                            BabyList[i].Directions[b].X = rand.Next(-5, 6);



                    }

                    if (rand.Next(0, 101) >= 100)
                    {
                        int rando = rand.Next(-50, 51);
                        float mod = (float)rando / 50;
                        float y = BabyList[i].Directions[b].Y;

                        y = y * mod;

                        BabyList[i].Directions[b].Y = rand.Next(-5, 6);

                    }




                }

                //Now one would assume this would create a unique dot that would move 100% independently right? Since it's at the end of the for loop, so nothin should change it
                // Nope, somehow it makes every other dot copy its directions...

                if (i == 5)
                {

                    for (int b = 0; b < BabyList[5].Directions.Length; b++)
                    {
                        BabyList[5].Directions[b] = new PointF(-5f, -5f);
                    }

                }

            }


            return BabyList;

        }

    }
}

示例:

def is_palindrome(word):
    if len(word) <= 1:
        return True
    if word[0] != word[-1]:
        return False
    return is_palindrome(word[1:-1])