写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”,并且测试这个程序。

写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”,并且测试这个程序。,第1张

写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”,并且测试这个程序。
def delBString(a,b):    if not isinstance(a,str):        raise TypeError("a is not str")    if not isinstance(b,str):        raise TypeError("b is not str")    if len(a) < len(b):        raise Exception('a length must large to b length')    result = []    flag = False    i=0    la = len(a)    lb = len(b)    while i <la:        j = 0        while j < lb: if i+j < la and a[i+j] == b[j]:     j += 1 else :     j += 1     flag = False     break flag = True        if flag: i += lb        else: result.append(a[i]) i += 1    return "".join(result)

 

测试用例:class TestdelInnerStringFunctions():    def setUp(self):        pass    def tearDown(self):        pass    def test_nomorl1(self):        assert delBString('asdqwe','we') == 'asdq'    def test_nomorl2(self):        assert delBString('asdqwe','0') == 'asdqwe'    def test_nomorl3(self):        assert delBString('测试asdqwe','we') == '测试asdq'    def test_nomorl4(self):        assert delBString('测试asdqwe','测试') == 'asdqwe'    def test_nomorl5(self):        assert delBString('asdqwe','') == 'asdqwe'    def test_nomorl6(self):        with pytest.raises(TypeError): delBString('', 0)    def test_nomorl7(self):        with pytest.raises(TypeError): delBString(0, 'as')    def test_nomorl8(self):        with pytest.raises(TypeError): delBString(True)    def test_nomorl9(self):       with pytest.raises(Exception) as excinfo:delBString('acd','acde')       assert "a length must large to b length" in str(excinfo.value)       assert excinfo.type == Exception

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4876070.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-11
下一篇2022-11-11

发表评论

登录后才能评论

评论列表(0条)

    保存