我是靠谱客的博主 平淡水蜜桃,最近开发中收集的这篇文章主要介绍计算文本文件中特定字符的数量(Python)题目描述输入/输出描述解题思路代码代码走读 测试用例传送门,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

题目描述

输入/输出描述

解题思路

代码

代码走读

 测试用例

1. 正常场景

2. 鲁棒性测试场景

传送门


题目描述

随机生成一个txt文件。要求文件的行数随机1 -- 100行。每一行随机1 -- 100个字符。字符随机从数字和大小写字母中选取。

输入一个字符,计算该字符在随机生成的txt文件中出现的次数。如果输入的是字母,不区分大小写,来计算其出现的次数。

条件限制:从txt文件中读取内容时,为了避免占用过多的内存,要求限制在100字节/次的读取速度。

输入/输出描述

输入:想要检测的字符(只能是数字或英文大小写字母)

输出:该字符在随机文件中和出现的次数。(不区分大小写)

解题思路

1、预置输出文件或清空文件

2、生成随机字符并写入文件

3、计算次数

代码

import string
import random

def rollback(url):
    open(url, "w").write("")
    return


def read_file(url, ele, size=100):
    count = 0
    with open(url, "r") as f:
        while True:
            line = f.read(size)
            if not line:
                break

            count += process(line, ele)
    f.close()
    return count


def process(line, ele):
    lower = line.lower()
    return lower.count(ele.lower())


pools = string.digits + string.ascii_letters
path = "example.txt"
logo = input("Please input a char to count numbers in file: ")
if len(logo) > 1:
    logo = logo[0]

if logo not in pools:
    print("Invalid Char input.")
    exit()

# 预置输出文件或清空文件
rollback(path)

# 按要求生成随机内容并写入文件
lines_num = random.randint(1, 100)
for i in range(lines_num):
    char_num = random.randint(1, 100)
    write_str = ""

    for j in range(char_num):
        rs = random.choice(pools)
        write_str += rs
    write_str += "n"
    open(path, "a").write(write_str)

print("count char %s: %s" % (logo, read_file(path, logo)))

代码走读

import string
import random

# 资源预置。创建或清空txt文件
def rollback(url):
    open(url, "w").write("")
    return


# 读文件,读取速度限制在100字节/次,返回计算出字符出现的次数的结果
def read_file(url, ele, size=100):
    count = 0
    with open(url, "r") as f:
        while True:
            line = f.read(size)
            if not line:
                break

            count += process(line, ele)
    f.close()
    return count


# 真正计算次数的函数,将读取到的字符片段转化为小写(因为要求不区分大小写),在统一进行计算
def process(line, ele):
    lower = line.lower()
    return lower.count(ele.lower())


# 代码开始的位置。pools表示随机字符池,包含数字字符0-9和大小写字母。
pools = string.digits + string.ascii_letters
# txt文件的路径
path = "example.txt"
# 输入待检测次数的字符。如果输入多个字符,则只检测第一个输入的字符。如果第一个输入的字符不在pools中,则报错并终止程序
logo = input("Please input a char to count numbers in file: ")
if len(logo) > 1:
    logo = logo[0]

if logo not in pools:
    print("Invalid Char input.")
    exit()

# 调用自定义的rollback()函数预置输出文件或清空文件
rollback(path)

# 按要求生成随机内容并写入文件
# lines_num随机行数。char_num每一行的随机字符数
lines_num = random.randint(1, 100)
for i in range(lines_num):
    char_num = random.randint(1, 100)
    write_str = ""

    for j in range(char_num):
        rs = random.choice(pools)
        write_str += rs
    write_str += "n"
# 将随机字符拼接后的write_str写入txt文件中
    open(path, "a").write(write_str)

# 打印结果
print("count char %s: %s" % (logo, read_file(path, logo)))

 测试用例

1. 正常场景

输入数字字符

运行结果:

Please input a char to count numbers in file: 6
count char 6: 58

随机生成的txt文件:

l1Mbo3ZeBk8Z3MnoQWMHUbg1D66m3rkRgWzJvOwZGMM80nKFaDFjTlUUKoY9Tg16DelvjEdu0v
oT7nchW
S7rHwYeaQqsC2wf7dzXqWLAMz7Eb4lONGJquyKRElH0ScunfmxppU2
cqoD5kfEwdpsRU3EpecuBZkwCdWYzLzX44WppZ0Fnk3q7bMXEeckM3
7Usyjnxm6hhdR1jDMt8ohEHFO631agEjWFMgLSCQjFZjYNvzAqiyef1
D2Z4GHSCpmkZfl5l3Vr5DM0w7DglSHNzN0eU9oSdlMV06OJoAz3Da2HeMSXCNZ1gTdYVhkmbJVkV
o1GDuKeBecs2nuuddB0f2b6cqwJokapN
Dk4eMkjAG98gyobUjVsBncKVfCZcH78nTJZylEmsEDPuXqmf0k2aOqMymaaXmeh4QnFkpJAEzxvwKpU3nqg
X
ZgZYZZgXvgyhotkL5JMiM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Jb5Dq6JTciUWB
VFvNl27ssFN46ORHGmUq
nxWV3CX6Ck4SolDgcf3zkEKQJo5hvv3PH9WENfGUOERQmpqmp5x5XfbySv91Sb
9FmzDCVVvlIvGMnlxgrQq7Ym4ML4sUjvneauW1a3D942WrVIEuhg9MfZ6P8oU1EzVoidaHDGKOUyOkp7
5XDS2cO8qN0QTJGBfp396EbZrE1sfd4DZtk
yqwShvIpTCVYDe1y
WjZ62GnXPYGbr8r1H2lTXUU4iOevRs62BTs3PIbwPT6We3Vk
DwF
vAnPI1PVFwjxGr0ZJiMMhyD6H2G4ndYhs9RaB94Mh4nDRXqQ6l7eqhAdgEMnVa1ags9Poxg7ykyBUJgEBzBi5Z
z86t3r5US9dDPwmr7nn
gw
UOMkLey2izwvnMqPkPTBTFqHuGs0EHQVx4IuRAxFyczssTqbZQRVuo8gOTIzKjSHqMnEsD1KOhrOIqoJhRjR
c2RCCtT90eMBreS7EA3umIqBgAMPBTlhWEVyron
YupK9cbJKn9vsC54WGivd3VUHYmQ
7IxrV99uPyV6k4zyJw
JM0rDkD12VnYjnPoG8ZSPDszAG5pmGGp2Tl8nikhG
d1YEqH
fv4

输入大写字母

运行结果:

Please input a char to count numbers in file: K
count char K: 172

随机生成的txt文件:

B7BcBQ2RcsqURFURtPPDstODP7VilVp0mauM82ouofASxAyzPVAengRn3SXZNdeX5OLD
Q7Szd7ktChwSeRTlDSu2e4YNLhT7M01EpMVG4y5qaLo
p5qDF4Y2k6UInEPREkodLJnSUY
81jxooalylZI575LeNg52WCEw7emHyUuZ6H97bpj2nu8
0j3PECuEM0lzfmFYY6v90og0j8DGiEYMU8xnvheClD5r1qABkIxw6T3CVhGsMd3fFAzsoLTZsgDL2sOGHzJZa9ffz
PqepQkXljATZXeddbZREv8A1l824izkzPXCLfDZrUiPMSoBkbSCwVe
0XUxvf5dWCrX7s35kD8ZNDuUn0Vko
jk7fZUWdhxc8jktgZ
zV1abEz3kO9JiMua8joh1wrpTq9s98kYJt
8rX1N5Gg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NwjgPAdyZwb0OWbZ
zAfsjg00Onu0X6u9nDQ2XC2wlS8SlMBlG5shrpNTuOnJ1
OJ3SqCFfuKdLWYlaUxDFuugNsLNOweT0xAJayOIpnsGIGViPTrX7u1apOPHHl2IWyR1cLZwZUPy1Y6UyDBCwa4oPR9
WJ1VXlPMdrDaZQs63yNDmwcWsYiVm2fWiSxzu9ULTJZkpFISpw1O83HmgDdGE

输入小写字母

运行结果:

Please input a char to count numbers in file: y
count char y: 72

随机生成的txt文件:

pqEL2UcEIi1VqzfSUpTGNHbFNT0UoJYTQNY08ixHb6wo0YpRQCVSbmcDU5LN9jg9eBn0CR9vcAQptrGitfWgAgaawSrc9d7cTZI
97kQDafIxjUvY10JNu
DnhSTyx
cHEHUJkDw8184S6z3jjFTt6
bBOjg8rXThHQibqz1wLa2qWprTCTC1CzYBOimL
KQRqeIDDbh9fBkZciughwHZQyS0QJMTPaEftDRCI2x7Atk4aF5ksLL1eop23OyXQCxjkhZYdlgRursCoOQ0DEP8ExpTcdx8XTUh
m4JOAqgxrVKsajtcBHWKEhZDCzfYzvO7UTVOumeGYpvYPKWRHPrYZpA1eoIZFZBAineOCqTRNDxAearFPv7BoXi
8mCUeagOMdJZU1OUaeyoJYqueIpqt16D6GuzWzZr0JzXhdVRXggbSROcVBcEBFO0CR5Ggwf7cx1qWzA97ECNMpEl5E4ZRsLsQ
1vltzFZPrgUP6md6N6UBv6fBdhQwZAItLMFGh0OcisMJ0A9dekx2aN
Q3anJpX
p07enPyzn28BSt2BPYT2MLcei8YNOjdAXEnFqeWtLdwmKuEtauFinOtVQZzeoGBpKe8pQ6PETatBtLkAkCmq1f3qfU2IR
r3KZvw7HgOE7g
na3iKya5ew
Z9mpH3ypVUECYC9lq1lYVv5StLktFTtEfWSpj1JsztBJ6pnJugJp0JxUdC9TmRngFc44dUjMbqqQzoeorAjDNRyUe
aeqCqaVL3GkCM8mhRjFqsZkXaqEyF9IHdsM4z3Nm5Lu1yFC5wN5bmtc
0NuJtqWlu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fZUx89RPg8zZiO2usp4GIuuozlF5TiefX0HuXmNd4I2
bfA7aMAT12POizhGoFvPLQPRInhEFnYS4xjBj2xawpkXtzMGctn9n6k6s8zKW6JJCSFXqOpuOKjIhPSMbv4EO5UtLTP51mG0ZY
y89sgGp61akHfp2Kj
P5Gx8
GsklN4XuMybK1RDTajTmkx2KJsPT1ebyCwYh
EBcJH2eHVA3ZPmWUnDLOjrbF532MschNaAclCyP4KlxRQUWcdEobUjjF3Mw5XL0fZn4XdL3oadSTv2WVSNzdb7xmYYcjOnmydw

2. 鲁棒性测试场景

输入多个字符

运行结果:

Please input a char to count numbers in file: myapps
count char m: 38

由此看出,程序面对多输入场景,只计算输入的首字符功能OK.

输入非pools内的字符(即不是数字0-9和英文大小写字母的字符)

运行结果:

Please input a char to count numbers in file: &
Invalid Char input.

    由此看出,程序面对非法字符的场景中能够识别并及时终止程序,避免无意义的资源浪费(生成文件并查找造成的时间和空间消耗)

传送门

1. input()函数

Python input函数_TCatTime的博客-CSDN博客

2. len()函数

Python len函数_TCatTime的博客-CSDN博客

3. print()函数

Python print函数:将内容打印到标准输出_TCatTime的博客-CSDN博客

4. exit()函数

Python exit函数_TCatTime的博客-CSDN博客

5. range()函数

Python range函数_TCatTime的博客-CSDN博客

最后

以上就是平淡水蜜桃为你收集整理的计算文本文件中特定字符的数量(Python)题目描述输入/输出描述解题思路代码代码走读 测试用例传送门的全部内容,希望文章能够帮你解决计算文本文件中特定字符的数量(Python)题目描述输入/输出描述解题思路代码代码走读 测试用例传送门所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部