Python 练习实例17

Python 练习实例17

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

 

# **********************************************************************************************************************
# coding: UTF-8 -*-
#
# 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
#
# 程序分析:利用 while 或 for 语句,条件为输入的字符不为 '\n'。
#
# **********************************************************************************************************************
import random
import operator

KW_KEY_SPACE = "space"
KW_KEY_DIGIT = "digit"
KW_KEY_ALPHI = "alpha"
KW_KEY_OTHERS = "others"
KW_KEY_TESTSTR = "test words"
KW_KEY_SEQ = (KW_KEY_SPACE, KW_KEY_DIGIT, KW_KEY_ALPHI, KW_KEY_OTHERS, KW_KEY_TESTSTR )


# **********************************************************************************************************************
# 统计出其中英文字母、空格、数字和其它字符的个数
#

#
# TestCaseList:   [in/out] --> list型, 输入为空List
# **********************************************************************************************************************
def StatString(TestRslt: dict):
    test_str = TestRslt[KW_KEY_TESTSTR]
    str_len = len(test_str)
    char_pos = 0
    TestRslt[KW_KEY_SPACE] = 0
    TestRslt[KW_KEY_DIGIT] = 0
    TestRslt[KW_KEY_ALPHI] = 0
    TestRslt[KW_KEY_OTHERS] = 0
    while char_pos < str_len:
        char_sym = test_str[char_pos]
        char_pos += 1
        if char_sym.isalpha():
            TestRslt[KW_KEY_ALPHI] += 1
        elif char_sym.isspace():
            TestRslt[KW_KEY_SPACE] += 1
        elif char_sym.isdigit():
            TestRslt[KW_KEY_DIGIT] += 1
        else:
            TestRslt[KW_KEY_OTHERS] += 1

# **********************************************************************************************************************
# 生成一个测试用例
#
# ASCII TABLE
# 32~126(共95个)是字符
# 32是空格
# 48~57为0到9十个阿拉伯数字。
# 65~90为26个大写英文字母,
# 97~122号为26个小写英文字母,
# 其余为一些标点符号、运算符号等。
#
# TestRslt:
# **********************************************************************************************************************
def CreateOneTestCase(TestCase: dict):

    sym_typ = 0
    test_str = ""
    space_num = 0
    digit_num = 0
    alphi_num = 0
    others_num = 0

    str_len = random.randint(0, 100)
    # print("Test String Length: %04d" %(str_len))
    while str_len != 0:
        sym_typ = random.randint(0, 95)          # 32~126(共95个)是字符
        if sym_typ < 32 - 32 + 1:                # 32是空格
            test_str += chr(32)
            space_num += 1
        elif sym_typ < 47 - 32 + 1 :              # 33~47 其余为一些标点符号、运算符号等。
            test_str += chr(random.randint(33, 47))
            others_num += 1
        elif sym_typ < 57 - 32 + 1:              # 48~57为0到9十个阿拉伯数字。
            test_str += chr(random.randint(48, 57))
            digit_num += 1
        elif sym_typ < 64 -32 + 1:              # 58~64 其余为一些标点符号、运算符号
            test_str += chr(random.randint(58, 64))
            others_num += 1
        elif sym_typ < 90 -32 + 1:              # 65~90为26个大写英文字母,
            test_str += chr(random.randint(65, 90))
            alphi_num += 1
        elif sym_typ < 96 -32 + 1:              # 91~96其余为一些标点符号、运算符号等。
            test_str += chr(random.randint(91, 96))
            others_num += 1
        elif sym_typ < 122 -32 + 1:              # 97~122号为26个小写英文字母,
            test_str += chr(random.randint(97, 122))
            alphi_num += 1
        else:                           # 123~126其余为一些标点符号、运算符号等。
            test_str += chr(random.randint(123, 126))
            others_num += 1
        str_len -= 1

        TestCase[KW_KEY_SPACE] = space_num
        TestCase[KW_KEY_DIGIT] = digit_num
        TestCase[KW_KEY_ALPHI] = alphi_num
        TestCase[KW_KEY_OTHERS] = others_num
        TestCase[KW_KEY_TESTSTR] = test_str

    # print(TestCase)
    return


# **********************************************************************************************************************
# 生成测试用例
#
# TestCaseList:   [in/out] --> list型, 输入为空List
# **********************************************************************************************************************
def CreateTestCase(test_case_num: int, TestCases: list):
    test_case = {}
    test_case = test_case.fromkeys(KW_KEY_SEQ)
    TestCases.clear()
    for cnt in range(test_case_num):
        # print("TestCase %03d" %(cnt), end="--->>>\t")
        CreateOneTestCase(test_case)
        TestCases.append(test_case.copy())

# **********************************************************************************************************************
# Main
# **********************************************************************************************************************
test_cases_num = 100
test_cases = []
test_case = {}
test_rslt = {}
test_rslt = test_rslt.fromkeys(KW_KEY_SEQ)
cnt = 0

print("Create test case......")
CreateTestCase(test_cases_num, test_cases)

print("Test is running......")
for test_case in test_cases:
    cnt += 1
    test_rslt[KW_KEY_TESTSTR] = test_case[KW_KEY_TESTSTR]
    StatString(test_rslt)
    if operator.eq(test_rslt, test_case):
        # operator.eq此方法的返回类型为bool ,如果x等于y ,则返回True ,否则返回False 。
        print("TestCase%03d is OK!" %(cnt))
        print(test_rslt)
    else:
        print("TestCase%03d is NG and the program will be terminated!" %(cnt))
        print("Test Case:")
        print(test_case)
        print("Test Result:")
        print(test_rslt)
        break
else:
    print("All Testcases have passed!!!!")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值