简体   繁体   English

在两个单词之间找到一个共同的字母,找不到我的代码中的错误

[英]Finding a common letter between two words, Can't find what's the bug in my code

I'm supposed to write a code which gets two strings as an input and outputs True if there is a common letter and false if there isn't, I'm supposed to do this using recursion. 我应该编写一个代码,该代码获取两个字符串作为输入,如果有一个普通的字母,则输出True;否则,则返回false,我应该使用递归来实现。

Running examples: 运行示例:

any_char_present("adsf","") #returns False
any_char_present("a","sfgserta") #returns true

My code gives me a correct answer for most cases, but for this case it gave me the wrong answer: 在大多数情况下,我的代码为我提供了正确的答案,但是对于这种情况,它给了我错误的答案:

>>> any_char_present("abcefghijklmno","pqrstuvwxyza")
False

I can't figure out what's the bug in my code, here it is: 我无法弄清楚代码中的错误是什么,这里是:

def any_char_present(s1,s2):
    if not s1 or not s2:
        return False
    if s1[0]==s2[0]:
        return True
    elif s1[0]!=s2[0]:
        s1=s1[1:]
        return any_char_present(s1,s2) or any_char_present(s2,s1)

This is because in your code, at the end of the first call, you are setting s1 to be: 'bcefghijklmno' and any comparisons made after that point will be different therefore it gives you False . 这是因为在您的代码中,在第一个调用结束时,您将s1设置为: 'bcefghijklmno'并且在此之后进行的任何比较都会有所不同,因此给您False

If this is a programming exercise, I will let you figure out the solution for yourself since that is probably why you are doing this exercise in the first place. 如果这是编程练习,那么我将让您自己找到解决方案,因为这可能就是您首先进行此练习的原因。

Other wise, you can do it as: 否则,您可以按照以下方式进行操作:

def any_char_present(s1, s2):
    return len(list(set(s1) & set(s2))) > 0

This will create a list of letters that are present in both strings and will then return True if the length of that list is greater than 0, ie there is atleast one common letter and False otherwise. 这将创建两个字符串中都存在的字母列表,如果该列表的长度大于0,则返回True ,即,至少有一个普通字母,否则返回False

Hope that helps. 希望能有所帮助。

I will assume that's homework so I will only guide you in the right direction. 我会假设这是家庭作业,所以我只会指导您正确的方向。 Before that, just a thought: your code is not very efficient as you will test several times the same "subwords" to be convince, use a print s1,s2 before the first if: 在此之前,请考虑一下:您的代码不是很有效,因为您将测试几次相同的“子词”以说服,如果出现以下情况print s1,s2在第一个字符前使用print s1,s2

>>> any_char_present("abc","def")
abc def
bc def
c def
 def
def
def c
ef c   
f c    #this
 c
c
c f    #is here
 f
f
c ef
 ef
ef
def bc
ef bc
f bc
 bc
bc
bc f
c f   #is here again
 f
f
f c   #and here 
 c
c
bc ef
c ef    
 ef
ef
ef c    
f c  #and also there
 c
c
c f #or there
 f
f

Now, for your problem: during the first iteration, you check s1[0] with s2[0 ], if they are different, you try to check the rest of s1 with s2 . 现在,针对您的问题:在第一次迭代中,您用s2[0 ]检查s1[0] s2[0 ],如果它们不同,则尝试用s2检查rest of s1 But you haven't check s1[0] with the rest of s2 yet. 但是您尚未将s1[0]rest of s2rest of s2一起检查。

So if the first letter of s1 isn't the first letter of s2 , but is the second letter of s2 , your code will fail. 因此,如果第一个字母s1是不是第一个字母s2 ,但第二个字母s2 ,你的代码将失败。

PS: if your homework isn't about recursion, check the in operator: PS:如果您的作业与递归无关,请检查in运算符:

>>> 'c' in 'abcd'
True

I'd suggest a better and efficient solution as follows 我建议如下更好和有效的解决方案

def any_char_present(s1,s2):
  list1 = list(s1)  
  list2 = list(s2)  
  return set(list1).intersection(list2)

The last statement would return a list of chars which are common to both strings. 最后一条语句将返回两个字符串共有的字符列表。

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

相关问题 在NLTK中找到两个文本语料库之间的常用词 - Finding the common words between two text corpus in NLTK 学习PyQt5。 在我的代码片段中找不到错误 - learning PyQt5. Can't find the bug in my code snippet 谁能告诉我快速排序代码中的错误是什么 - can anyone tell what's the bug in my quick sort code 在我的 noob python 代码中找不到命令“cat”的错误 - Can't find the bug with the command "cat" in my noob python code (排列/ Anagrm)单词在python 2.72中找到(需要帮助来查找我的代码有什么问题) - (permutation/Anagrm) words find in python 2.72 (need help to find what's wrong with my code) 无法找出我的代码有什么问题? (2D 矩阵卷积与 numpy) - Can't find out what's wrong with my code? (2D Matrix convolution with numpy) 无法发现我的python代码中的错误 - Can't spot a bug in my python code 我可以使用什么来在两个列表中查找名称单词? Python - What can I use for finding names words in two list? Python 如何从包含特定字母的列表中打印出单词? 我知道如何处理以特定字母开头但不以 BETWEEN 开头的单词 - How can I print out words from a list that contains a particular letter? I know what to do with words starting with specific letter but not in BETWEEN 我的 WordCloud 缺少单词末尾的字母“s” - My WordCloud is missing the letter 's' at the end of words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM