简体   繁体   English

我正在尝试通过删除索引来减少我的代码以使其更简单

[英]I am trying to reduce my code to make it simpler by removing the indexes

This is my recursive function named find(text,substring) that will return the substring if the substring is inside the text or return an empty string if substring is not inside the text.这是我名为find(text,substring)的递归函数,如果子字符串在文本内部,它将返回子字符串;如果子字符串不在文本内部,则返回空字符串。 Another function named is_string_there(text, string) to test the find function.另一个名为is_string_there(text, string)的函数用于测试find函数。 The function will return True if the string is inside the text or False if the string is not inside the text.如果字符串在文本内,该函数将返回True ,如果字符串不在文本内,则返回False

Am I able to cancel out text index and substring index to remove the two (0,0) at the print coding area to reduce my code to make it simpler and easier to understand so i can teach my students我是否可以取消文本索引和子字符串索引以删除打印编码区域的两个(0,0)以减少我的代码,使其更简单、更容易理解,以便我可以教我的学生

def find(text, substring, text_index, substring_index): 
    if text_index == len(text) and substring_index != len(substring): 
        return False

    if substring_index == len(substring): 
        return True

    if text[text_index] == substring[substring_index]: 
        return find(text, substring, text_index+1, substring_index+1) 

    return False


def oo(text, substring, text_index, substring_index): 
    if text_index == len(text): 
        return False


    if text[text_index] == substring[substring_index]: 
        if find(text, substring, text_index, substring_index): 
            return True
        else: 
            return oo(text, substring, text_index+1, substring_index) 


    return oo(text , substring, text_index+1, substring_index)

print(oo("I love pie", "pie",0,0))
print(oo("I love pie1", "pie1",0,0))

In your code, you use oo() to check if a character matches, and then calls find() to check if the rest of the characters match.在您的代码中,您使用 oo() 检查字符是否匹配,然后调用 find() 检查其余字符是否匹配。 Instead of checking character-by-character, you could use the idea of checking string equality.您可以使用检查字符串相等性的想法,而不是逐个字符地检查。 In your oo() function, you can check if the substring is equal to a splice of the original text.在您的 oo() 函数中,您可以检查子字符串是否等于原始文本的拼接。 This will reduce your code but will make your find() function obsolete, while still serving as a simple demonstration of recursion.这将减少您的代码,但会使您的 find() 函数过时,同时仍然作为递归的简单演示。

def oo(text, substring, index): 
    if index == len(text):
        return False  

    start = index
    end = start + len(substring)  

    if text[start:end] == substring: 
        return True
    else: 
        return oo(text, substring, index+1) 


print(oo("I love pie", "pie",0)) 
print(oo("I love pie1", "pie1",0))
print(oo("I love pie", "pie1",0))
print(oo("I love pie1", "pie",0))
print(oo("pie", "pie",0))
print(oo("pi", "pie1",0))
print(oo("pie1", "pie",1))

Output:输出:

True
True
False
True
True
False
False

Well if you want to remove the indexes permanently from the print commands, you could just use a class.那么如果你想从打印命令中永久删除索引,你可以只使用一个类。 This way you can even create other functions to take user input etc and take advantage of class variables for readability.这样你甚至可以创建其他函数来获取用户输入等,并利用类变量来提高可读性。 Not sure if if cases should in form if-elif-else though:不确定 case 是否应该采用 if-elif-else 形式:

class test():    
    def __init__(self):
        self.reset()
        self.run()
    def reset(self):
        self.substring_index = 0 #instead of putting in function they are now class variables
        self.text_index = 0
    def run(self):#more stuff can be done here

        print(self.oo("I love pie","pie"))
        self.reset() #reset the class variables for each step
        print(self.oo("I love pie1","pie1"))

    def find(self,text, substring): #pretty much the same code just using class variables and class methods
        if self.text_index == len(text) and self.substring_index != len(substring): 
            return False

        elif self.substring_index == len(substring): 
            return True

        elif text[self.text_index] == substring[self.substring_index]: 
            self.text_index +=1
            self.substring_index +=1
            return self.find(text, substring) 

        else:
            return False


    def oo(self,text, substring): 
        if self.text_index == len(text): 
            return False


        elif text[self.text_index] == substring[self.substring_index]: 
            if self.find(text, substring): 
                return True
            else: 
                self.text_index +=1
                return oo(text, substring) 
        else:
            self.text_index +=1
        return self.oo(text , substring)

if __name__=='__main__':
    test()

Although just for reference, a super simple way for the problem is actually:虽然仅供参考,其实一个超级简单的解决问题的方法是:

def oo(text,substring):
    if substring in text:
        return True
    else:
        return False

AS mentioned in comments:如评论中所述:

def oo(text,substring):
    if text.find(substring) == 0:
        return True
    else:
        return False

Returns 0 if exists in text, -1 if not.如果文本中存在则返回 0,否则返回 -1。

Interesting expansion possible when considering uppercase or lowercase diffrences.考虑大写或小写差异时可能进行有趣的扩展。 'A' != 'a' 'A' != '一个'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM