java 截取字符串 按长度_python 按最大长度限制截取字符串

3d68dd0fa8dc87dd9897af283e607cbf.png

python 按最大长度限制截取字符串

题目说明

Complete the function ‘consistentLineLength’ taking two parameters, a filename, and a maximum length (i.e., a strictly positive integer number). The function opens the file, reads every line, and returns a list of strings where every string represents a line that is filled as much as possible without exceeding the given maximum length.
翻译:补全函数 consistentLength ,这个函数有两个参数,一个是文件名,一个是最大长度。(例如:一个严格正整数)。这个函数打开指定的文件,读取文件里的每一行并返回一个list(list的每一个元素是一个字符串类型),这里,每一个字符串是一行,这个字符串要尽可能的长但不能超过给定的最大长度。

示例

consider a file containing the following lines from ‘Alice’s Adventures in Wonderland’.

c0ae66c98e40caf530c0e5b42a28f996.png


The corresponding output for length = 50 would be:

0d6823a735b1ed7ba421a16a41f6baf4.png

afa1558fbd5e58161d612e6fd4579bbf.png

思路

观察一下,首先处理后的结果是不能以空格结尾的,但可以以标点符号或者一个完整的单词结尾。
首先是读取文本:

def consistentLineLength(filename, length):
    fileHandle=open(filename)
    theStr=""
    for line in fileHandle.readlines():
        theStr+=line

theStr存储了所有字符串,但我们还要把换行符 n 替换成空格。然后通过以空格为切割标志就能切割出所有的单词:

theStr=theStr.replace("n"," ")
strList=theStr.split(" ")
if len(strList[-1])==0:
    strList.pop(-1)

接下来就是问题的关键了, resultList 是最后的结果变量。除此之外,还有一个字符串变量 tempStr ,他每一次就加一个空格和一个单词,如果超过了最大长度就重新变为空字符串变量。

tempStr=""
    resultList=[]
    i=0
    while i<len(strList):
        if tempStr=="":
            if len(tempStr+strList[i])<=length:
                tempStr+=strList[i]
                i+=1
                if i==len(strList): # 如果当前已经到了最后一个元素,那就直接添加
                    resultList.append(tempStr)
                    else:
                        if len(tempStr+" "+strList[i])<=length: # 如果tempStr不是空,那加入下一个单词之前先加一个空格
                            tempStr+=" "
                            tempStr+=strList[i]
                            i+=1

                            else:
                                resultList.append(tempStr)
                                tempStr=""

    return resultList

完整代码:

def consistentLineLength(filename, length):
    fileHandle=open(filename)
    theStr=""
    for line in fileHandle.readlines():
        theStr+=line
    theStr=theStr.replace("n"," ")
    strList=theStr.split(" ")
    if len(strList[-1])==0:
        strList.pop(-1)
    tempStr=""
    resultList=[]
    i=0
    while i<len(strList):
        if tempStr=="":
            if len(tempStr+strList[i])<=length:
                tempStr+=strList[i]
            i+=1
            if i==len(strList): # 如果当前已经到了最后一个元素,那就直接添加
                resultList.append(tempStr)
        else:
            if len(tempStr+" "+strList[i])<=length: # 如果tempStr不是空,那加入下一个单词之前先加一个空格
                tempStr+=" "
                tempStr+=strList[i]
                i+=1

            else:
                resultList.append(tempStr)
                tempStr=""

    return resultList

有算法online judge需求的童靴可以私信联系我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值