【发布时间】:2022-06-26 20:46:19
【问题描述】:
来自“从头开始学习 Python 3”中的 Educative.io 第一次考试:
检测字符串模式
在这个编码练习中,您被要求编写一个名为detect_pattern 的函数的主体,该函数根据两个字符串是否具有相同的字符模式返回真或假。更准确地说,如果两个字符串的长度相同,并且如果第一个字符串中的两个字符相等,当且仅当第二个字符串中相应位置的字符也相等时,它们具有相同的模式。
以下是一些相同模式和不同模式的示例:
| 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) 应该返回 False。
注意: 函数detect_pattern 有两个参数:要比较的两个字符串。 您可以创建新字符串,但除此之外您不得构建额外的数据结构来解决此问题(没有列表、集合、字典等)。 请记住,无论两个字符串以何种顺序传递,该方法都应返回相同的值。
祝你好运!
我的代码:
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() 方法的情况下解决整个问题。
【问题讨论】:
-
不确定正则表达式是否适合这项工作。有这个要求吗?
-
不,不是——我是 python 的初学者,我只是在寻找不使用 set() 来解决该任务的方法——这是要求,这里我们不应该使用 set()方法
-
这是在线的某个地方,以便我们可以在那里测试潜在的解决方案吗?
-
示例错误:
- - -和aaa长度不同但定义为True,ab和xy均不包含2个相等的字符但定义为@ 987654329@. -
@KellyBundy 这里是Exam 的链接——这是最后一个问题。使用我的第一个代码,我在该问题上通过了 5/10 的测试。通过本杰明的解决方案,我得到了 10/10。
标签: python string algorithm string-comparison