栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

写一段程序,删除字符串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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号