单元测试学习笔记
求出字符串中无重复字母的最长切片字符串的长度import unittest
def len_str_slice_equal_set(string):
"""求出字符串中无重复字母的最长切片字符串的长度"""
"""这样的切片字符串其集合化的长度等于自身长度"""
length = len(string)
if length == len(set(string)):
return length
max_len = 0
str_len = 0
for i in range(0, length-max_len-1):
for j in range(i+max_len+1, length+1):#为了能切下字符串中的最后一个字符,j 必须取到 length 的值
str_slice = string
str_len = j - i
set_str_slice = set(str_slice)
if (str_len == len(set_str_slice)) and (str_len > max_len):
max_len = str_len
return max_len
# ~ print(len_str_slice_equal_set(input("请输入你最喜欢的一句话:")))
class TestCase1(unittest.TestCase):
"""函数测试"""
def setUp(self):#创建参数与答案列表
self.str = ['bbbbb',
"qwwkew",
"abcabcaa",
"qwert",
"",
'a',
'求出字符串中无重复字母的最长切片字符串的长度']
self.answer = (1, 3, 3, 5, 0, 1, 13)
def test_0(self): #第0个测试,每个测试务必以test_开头,否则不执行
for i in range(len(self.str)):
answer = len_str_slice_equal_set(self.str)
self.assertEqual(answer, self.answer)
unittest.main() 不错哈
页:
[1]