如何检查第二个字符串中相应位置的字符是否与第一个字符串中的相同位置重复?教育考试
How to check if the characters in the corresponding positions in the second string are repeating in same position like in first string? Educative EXAM
来自Educative.io“从零开始学习Python 3”的第一次考试:
正在检测字符串模式
在此编码练习中,要求您编写一个名为 detect_pattern 的函数的主体,根据两个字符串是否具有相同的字符模式,return 为真或为假。更准确地说,如果两个字符串具有相同的长度并且如果第一个字符串中的两个字符相等当且仅当第二个字符串中相应位置的字符也相等,则两个字符串具有相同的模式。
以下是相同模式和不同模式的一些示例:
1st String
2nd String
Same Pattern
“”
“”
True
“a”
“a”
True
“x”
“y”
True
“ab”
“xy”
True
“aba”
“xyz”
False
“- - -”
“xyz”
False
“- - -”
“aaa”
True
“xyzxyz”
“toetoe”
True
“xyzxyz”
“toetoa”
False
“aaabbbcccd”
“eeefffgggz”
True
“cbacbacba”
“xyzxyzxyz”
True
“abcdefghijk”
“lmnopqrstuv”
True
“asasasasas”
“xxxxxyyyyy”
False
“ascneencsa”
“aeiouaeiou”
False
“aaasssiiii”
“gggdddfffh”
False
例如,如果名为 s1 和 s2 的两个字符串包含以下字母:
s1 = "aba"
s2 = "xyz"
那么调用 detect_pattern(s1, s2) 应该 return False。
注:
函数 detect_pattern 有两个参数:要比较的两个字符串。
您可以创建新的字符串,但除此之外 您不能构造额外的数据结构来解决这个问题(没有列表、集合、字典等)。
请记住,无论两个字符串的传递顺序如何,该方法都应该 return 相同的值。
祝你好运!
我的代码:
import unittest
import re # only needed if we use hint from 6th line
''' HINT:
if we add this to the end of 13th line before ending ":":
and pattern.match(s1.replace(" ", "")) == pattern.match(s2.replace(" ", "")) and len(set(s1.replace(" ", ""))) == len(set(s2.replace(" ", "")))
it will solve more case's but we can't use set() method.
'''
pattern = re.compile("^([a-z][0-9]+)+$") # only needed if we use hint from 6th line
def detect_pattern(s1, s2):
if len(s1.replace(" ", "")) == len(s2.replace(" ", "")):
return True
else:
return False
class TestDetectPattern(unittest.TestCase):
def test_basics(self):
self.assertEqual(detect_pattern("", ""), True)
self.assertEqual(detect_pattern("a", "a"), True)
self.assertEqual(detect_pattern("x", "y"), True)
self.assertEqual(detect_pattern("ab", "xy"), True)
# self.assertEqual(detect_pattern("aba", "xyz"), False) # can be solved with hint from 6th line but in this task we can't use set() method
# self.assertEqual(detect_pattern("- - -", "xyz"), False) # can be solved with hint from 6th line but in this task we can't use set() method
self.assertEqual(detect_pattern("- - -", "aaa"), True)
self.assertEqual(detect_pattern("xyzxyz", "toetoe"), True)
# self.assertEqual(detect_pattern("xyzxyz", "toetoa"), False) # can be solved with hint from 6th line but in this task we can't use set() method
self.assertEqual(detect_pattern("aaabbbcccd", "eeefffgggz"), True)
self.assertEqual(detect_pattern("cbacbacba", "xyzxyzxyz"), True)
self.assertEqual(detect_pattern("abcdefghijk", "lmnopqrstuv"), True)
# self.assertEqual(detect_pattern("asasasasas", "xxxxxyyyyy"), False)
# self.assertEqual(detect_pattern("ascneencsa", "aeiouaeiou"), False)
# self.assertEqual(detect_pattern("aaasssiiii", "gggdddfffh"), False) # can be solved with hint from 6th line but in this task we can't use set() method
if __name__ == '__main__':
unittest.main()
有谁知道如何检查第二个字符串中相应位置的字符是否与第一个字符串中的相同位置重复? - 我认为这可以在不使用 set() 方法的情况下解决整个问题。
编辑:在 if-statement 的条件下存在一些问题,但现在应该解决这个问题。
这种方法应该有效。我对循环中的变量命名不太满意,所以请随意更改它们。我用你提供的所有例子都试过了,所以在语义上它应该是正确的。
def detect_pattern(s1: str, s2: str) -> bool:
if len(s1) != len(s2):
return False
for idx, (let_1, let_2) in enumerate(zip(s1, s2)):
for let_1_inner, let_2_inner in zip(s1[idx:], s2[idx:]):
if (let_1_inner == let_1) != (let_2_inner == let_2):
return False
return True
来自Educative.io“从零开始学习Python 3”的第一次考试:
正在检测字符串模式
在此编码练习中,要求您编写一个名为 detect_pattern 的函数的主体,根据两个字符串是否具有相同的字符模式,return 为真或为假。更准确地说,如果两个字符串具有相同的长度并且如果第一个字符串中的两个字符相等当且仅当第二个字符串中相应位置的字符也相等,则两个字符串具有相同的模式。
以下是相同模式和不同模式的一些示例:
1st String | 2nd String | Same Pattern |
---|---|---|
“” | “” | True |
“a” | “a” | True |
“x” | “y” | True |
“ab” | “xy” | True |
“aba” | “xyz” | False |
“- - -” | “xyz” | False |
“- - -” | “aaa” | True |
“xyzxyz” | “toetoe” | True |
“xyzxyz” | “toetoa” | False |
“aaabbbcccd” | “eeefffgggz” | True |
“cbacbacba” | “xyzxyzxyz” | True |
“abcdefghijk” | “lmnopqrstuv” | True |
“asasasasas” | “xxxxxyyyyy” | False |
“ascneencsa” | “aeiouaeiou” | False |
“aaasssiiii” | “gggdddfffh” | False |
例如,如果名为 s1 和 s2 的两个字符串包含以下字母:
s1 = "aba"
s2 = "xyz"
那么调用 detect_pattern(s1, s2) 应该 return False。
注: 函数 detect_pattern 有两个参数:要比较的两个字符串。 您可以创建新的字符串,但除此之外 您不能构造额外的数据结构来解决这个问题(没有列表、集合、字典等)。 请记住,无论两个字符串的传递顺序如何,该方法都应该 return 相同的值。
祝你好运!
我的代码:
import unittest
import re # only needed if we use hint from 6th line
''' HINT:
if we add this to the end of 13th line before ending ":":
and pattern.match(s1.replace(" ", "")) == pattern.match(s2.replace(" ", "")) and len(set(s1.replace(" ", ""))) == len(set(s2.replace(" ", "")))
it will solve more case's but we can't use set() method.
'''
pattern = re.compile("^([a-z][0-9]+)+$") # only needed if we use hint from 6th line
def detect_pattern(s1, s2):
if len(s1.replace(" ", "")) == len(s2.replace(" ", "")):
return True
else:
return False
class TestDetectPattern(unittest.TestCase):
def test_basics(self):
self.assertEqual(detect_pattern("", ""), True)
self.assertEqual(detect_pattern("a", "a"), True)
self.assertEqual(detect_pattern("x", "y"), True)
self.assertEqual(detect_pattern("ab", "xy"), True)
# self.assertEqual(detect_pattern("aba", "xyz"), False) # can be solved with hint from 6th line but in this task we can't use set() method
# self.assertEqual(detect_pattern("- - -", "xyz"), False) # can be solved with hint from 6th line but in this task we can't use set() method
self.assertEqual(detect_pattern("- - -", "aaa"), True)
self.assertEqual(detect_pattern("xyzxyz", "toetoe"), True)
# self.assertEqual(detect_pattern("xyzxyz", "toetoa"), False) # can be solved with hint from 6th line but in this task we can't use set() method
self.assertEqual(detect_pattern("aaabbbcccd", "eeefffgggz"), True)
self.assertEqual(detect_pattern("cbacbacba", "xyzxyzxyz"), True)
self.assertEqual(detect_pattern("abcdefghijk", "lmnopqrstuv"), True)
# self.assertEqual(detect_pattern("asasasasas", "xxxxxyyyyy"), False)
# self.assertEqual(detect_pattern("ascneencsa", "aeiouaeiou"), False)
# self.assertEqual(detect_pattern("aaasssiiii", "gggdddfffh"), False) # can be solved with hint from 6th line but in this task we can't use set() method
if __name__ == '__main__':
unittest.main()
有谁知道如何检查第二个字符串中相应位置的字符是否与第一个字符串中的相同位置重复? - 我认为这可以在不使用 set() 方法的情况下解决整个问题。
编辑:在 if-statement 的条件下存在一些问题,但现在应该解决这个问题。
这种方法应该有效。我对循环中的变量命名不太满意,所以请随意更改它们。我用你提供的所有例子都试过了,所以在语义上它应该是正确的。
def detect_pattern(s1: str, s2: str) -> bool:
if len(s1) != len(s2):
return False
for idx, (let_1, let_2) in enumerate(zip(s1, s2)):
for let_1_inner, let_2_inner in zip(s1[idx:], s2[idx:]):
if (let_1_inner == let_1) != (let_2_inner == let_2):
return False
return True