鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: zltzlt

[已解决]Python:每日一题 253(高额悬赏)

[复制链接]
 楼主| 发表于 2019-10-9 19:39:40 | 显示全部楼层
XiaoPaiShen 发表于 2019-10-8 21:37
试试下面的代码吧

恭喜通过!

执行用时:56 ms
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-9 19:41:05 | 显示全部楼层
Unicorn# 发表于 2019-10-9 00:47
重新提交......
题主您没说清楚, 我把正确的给注释掉了
重发

解答错误,与题目示例不符,答案作废
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-9 19:43:29 | 显示全部楼层

恭喜通过!

执行用时:40 ms
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-9 21:21:05 | 显示全部楼层
  1. def answer(words: list, maxWidth: int):
  2.     res = []
  3.     while len(words) != 0:  # 主循环
  4.         lenth = 0  # 单行长度初始化
  5.         count = 0  # 单行计数初始化
  6.         sentence = ''  # 单行文本初始化
  7.         temp = []  # 定义空列表
  8.         for chars in words:
  9.             lenth += len(chars)
  10.             if lenth < maxWidth - count + 1:
  11.                 count += 1
  12.                 temp.append(chars)
  13.             else:
  14.                 break
  15.         blank = maxWidth - sum(map(len, temp))  # 填充空格数量
  16.         if count == 1:
  17.             sentence += temp[0] + ' ' * blank
  18.             words.remove(temp[0])
  19.         else:
  20.             x, y = divmod(blank, count - 1)
  21.             for i in temp:
  22.                 sentence += i
  23.                 if temp[0] == i:
  24.                     sentence += ' ' * (x + y)
  25.                 elif temp[-1] == i:
  26.                     pass
  27.                 else:
  28.                     sentence += ' ' * x
  29.                 words.remove(i)
  30.         #末行检测更改句式
  31.         if len(words) == 0:
  32.             sentence = ' '.join(temp)
  33.             blank = maxWidth - len(sentence)
  34.             sentence += ' ' * blank
  35.         res.append(sentence)
  36.     return res
复制代码


没赶上交卷,能劳驾帮忙检测下通过没?@zltzlt
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-9 21:30:12 | 显示全部楼层
panheng 发表于 2019-10-9 21:21
没赶上交卷,能劳驾帮忙检测下通过没?@zltzlt

解答错误

输入:words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
输出:["Science   is what we","understand      well","enough toexplain to","a   computer. Art is","everything  else  we","do                  "]
预期结果:["Science  is  what we","understand      well","enough to explain to","a  computer.  Art is","everything  else  we","do                  "]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-9 21:45:35 | 显示全部楼层
本帖最后由 panheng 于 2019-10-10 00:16 编辑
zltzlt 发表于 2019-10-9 21:30
解答错误

输入:words = ["Science","is","what","we","understand","well","enough","to","explain", ...


哦,在插入空格那里我理解错了,应该适用抽屉原理。试试这样行不行?
  1. def answer(words: list, maxWidth: int):
  2.     res = []
  3.     while len(words) != 0:  # 主循环
  4.         lenth = 0  # 单行长度初始化
  5.         count = 0  # 单行计数初始化
  6.         sentence = ''  # 单行文本初始化
  7.         temp = []  # 定义空列表
  8.         for chars in words:
  9.             lenth += len(chars)
  10.             if lenth < maxWidth - count + 1:
  11.                 count += 1
  12.                 temp.append(chars)
  13.             else:
  14.                 break
  15.         blank = maxWidth - sum(map(len, temp))  # 填充空格数量
  16.         if count == 1:
  17.             sentence += temp[0] + ' ' * blank
  18.             words.remove(temp[0])
  19.         else:
  20.             x, y = divmod(blank, count - 1)
  21.             for i in temp:
  22.                 sentence += i
  23.                 if temp[-1] == i:
  24.                     pass
  25.                 elif y > 0:
  26.                     sentence += ' ' * (x + 1)
  27.                     y -= 1
  28.                 else:
  29.                     sentence += ' ' * x
  30.                 words.remove(i)
  31.         #末行检测更改句式
  32.         if len(words) == 0:
  33.             sentence = ' '.join(temp)
  34.             blank = maxWidth - len(sentence)
  35.             sentence += ' ' * blank
  36.         res.append(sentence)
  37.     return res
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-10 21:29:03 | 显示全部楼层
  1. def the_words(words,maxwidth):
  2.     new_words = []
  3.     while words:
  4.         sum_word = 0
  5.         del_word = []
  6.         for each in words:   #遍历words列表中的每个元素
  7.             sum_word += len(each)
  8.             del_word.append(each)
  9.             if sum_word > maxwidth:
  10.                 s = len(del_word)-1    # s 需要的字符长度
  11.                 n = maxwidth - sum([len(a) for a in words[0:s]])  
  12.                 if s == 1:  # 如果只有一个字符时,左边对齐
  13.                      
  14.                     woed_i = words[0] + (' '*n)
  15.                 else:
  16.                     a = int(n / (s - 1))  # 平均的space填充
  17.                     m = n - a*(s-1)      
  18.                     woed_i=''   # 由于是左右两边对其,所有先把多余的space填充左边, 然后join
  19.                     for i in range(m):
  20.                         woed_i += words[i] + ' '*(a+1)
  21.                     woed_i = woed_i + ((' '*a).join(words[m:s]))

  22.                 new_words.append(woed_i)
  23.                 words = words[s:]
  24.                 break
  25.             elif sum_word == maxwidth:
  26.                 s = len(del_word)
  27.                 new_words.append(' '.join(words[0:s]))
  28.                 words = words[s:]
  29.                 break
  30.             elif sum_word < maxwidth and len(' '.join(words) ) < maxwidth:
  31.                 space = maxwidth-sum(len(a) for a in words)-(len(words)-1) # 计算出最后一行的space数
  32.                 new_words.append(' '.join(words)+(' '*space))
  33.                 words = []
  34.                 break
  35.             else:
  36.                 sum_word +=1
  37.     print(new_words)
  38.     for i in new_words:
  39.         print(len(i),' - ', end='')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-12 21:58:50 | 显示全部楼层
def SpaceProductor(numb,interval):
    each = numb // interval
    rest = numb % interval
    return (each+1,)*rest+(each,)*(interval-rest)+(0,)

def Iterator(dancishuzu):
    count = 0
    cacheList = []
    length = 0
    result = []
    while True:
        if count == len(dancishuzu):
            str1 = ""
            for i in cacheList:
                str1 += i + " "
            result.append(str1.ljust(maxWidth))
            return result
        elif length+len(dancishuzu[count])+len(cacheList)-1 > maxWidth:      
            str1 = ""
            spaceTuple = SpaceProductor(maxWidth-length,len(cacheList)-1)
            for i in range(len(cacheList)):
                str1 += cacheList[i] + spaceTuple[i]*" "
            result.append(str1)
            cacheList.clear()
            length = 0
        length += len(dancishuzu[count])
        cacheList.append(dancishuzu[count])
        count += 1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-12 23:47:50 | 显示全部楼层
表示还不会c
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-22 11:57:42 | 显示全部楼层
本帖最后由 graceasyi 于 2019-11-22 13:05 编辑

为了看得清楚点,空格被我写成*了。写得有点繁。

  1. def fun253(words, max_width):
  2.     words = [word + '*' for word in words]
  3.     word_len = [len(word) for word in words]
  4.     temp, sum_len, s = [], 0, 0
  5.     for k, v in enumerate(word_len):
  6.         sum_len += v
  7.         if sum_len - 1 > max_width:      
  8.             num = k - s                 
  9.             space_num = max_width - (sum(word_len[s:k]) - 1)
  10.             if num > 1:
  11.                 sp = num - 1
  12.                 space_avg = space_num // sp
  13.                 if space_avg:      
  14.                     for i in range(s, k-1):
  15.                         words[i] += '*'*space_avg
  16.                 plus = space_num - space_avg * sp
  17.                 if plus != 0:
  18.                     for j in range(plus):
  19.                         words[s+j] += '*'
  20.             else:
  21.                 for i in range(s, k):
  22.                     words[i] += '*'*space_num

  23.             temp.append(''.join(words[s:k]))
  24.             s, sum_len = k, word_len[k]
  25.     space_num = max_width - (sum(word_len[s:]) - 1)  
  26.     temp.append(''.join(words[s:]) + '*'*space_num)
  27.     return [t[:-1] for t in temp]


  28. words = ["Science", "is", "what", "we", "understand", "well", "enough", "to", "explain",
  29.          "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"]
  30. maxWidth = 20
  31. print(fun253(words, maxWidth))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 20:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表