当前位置: 首页 > 面试题库 >

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

东方俊材
2023-03-14
本文向大家介绍写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”,并且测试这个程序。相关面试题,主要包含被问及写一段程序,删除字符串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
 类似资料:
  • 问题内容: 我是python编程的新手,我有点困惑。我尝试从字符串中获取字节以进行哈希和加密,但是我得到了 字符串前面的b字符,如以下示例所示。有什么办法可以避免这种情况吗?有人可以提供解决方案吗?对不起这个愚蠢的问题 输出: 问题答案: 解码是多余的 首先,您对这种情况有误解,这是因为对所发生的事情有误解。 您会得到,因为您已对其进行编码,现在它是一个字节对象。 修正: 您可以先打印字符串 编码

  • 问题内容: 我想删除字符串的第一个字符。 例如,我的字符串以a开头,而我只想删除它。字符串中有几次不应删除。 我正在用Python编写代码。 问题答案: python 2.x python 3.x 两张画

  • 我想知道下面的程序出了什么问题,因为它没有返回反向字符串。

  • 题目描述 给定两个分别由字母组成的字符串A和字符串B,字符串B的长度比字符串A短。请问,如何最快地判断字符串B中所有字母是否都在字符串A里? 为了简单起见,我们规定输入的字符串只包含大写英文字母,请实现函数bool StringContains(string &A, string &B) 比如,如果是下面两个字符串: String 1:ABCD String 2:BAD 答案是true,即Stri

  • 问题内容: 有没有一种简单的方法来测试Python字符串“ xxxxABCDyyyy”,以查看其中是否包含“ ABCD”? 问题答案: if “ABCD” in “xxxxABCDyyyy”: # whatever

  • 问题内容: 可以说我有这个单词列表: 比我有文字 是否有匹配stopWords并在忽略大小写时将其删除的方法;像这样的地方?: 结果: 如果您了解正则表达式,效果很好,但我真的更喜欢像Commons解决方案这样的东西,它更注重性能。 顺便说一句,现在我正在使用此通用方法,该方法缺少适当的不区分大小写的处理: 问题答案: 这是不使用正则表达式的解决方案。我认为它不如我的其他答案,因为它更长且不清楚,