鱼C论坛

 找回密码
 立即注册
查看: 1935|回复: 1

[技术交流] 鱼C论坛Python精英挑战赛(第二季01期)评比结果

[复制链接]
发表于 2017-8-10 21:39:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 jerryxjr1220 于 2017-8-11 07:59 编辑

感谢大家热情地参与到本次精英挑战赛中来!

本次挑战赛题目:字符串格式化输出“两端对齐”

共有@冬雪雪冬 ,qitan888,土沙拉,小剑剑, 小Q学Python,python911,极品掌中宝,guoxike,小锟 ,凤凰0620,bozhen,zhengod 等12位鱼油参与答题。

其中,程序输出正确的鱼油是:冬雪雪冬,小剑剑(修改后),小锟。(设置width为10,15,20,30,40,60分别进行测试)。

程序最简洁的是冬雪雪冬,但由于优胜者从普通鱼油中产生,所以评选小剑剑和小锟并列第一(两者结果都正确,代码长度也接近,代码风格各有千秋。)

@小甲鱼 老师最后评判并颁奖!

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-8-10 21:45:33 | 显示全部楼层
来贴一下我的代码,虽然我觉得写得也不优雅

第一种:普通循环方式
  1. txt = "Hot work is one of the typical high risk work in work shop, if out of control, it will cause tragedy. We manage our hot work basing on the FM hot work permit system. Then, to make sure the fire risk are eliminated before we start hot work, what should we do? Please refer to this week's topic, hot work permit."

  2. def adjust_txt(txt, width=30):
  3.         def adjust(lst, width, count): #当一行的长度接近width后,调整该行输出
  4.                 lack = width-(count-1) #计算缺少的长度
  5.                 n_words = len(lst) #计算单词数量
  6.                 space = lack//(n_words-1)+1 if n_words > 1 else 1 #计算每个单词后至少应该用多少空格去填充
  7.                 absent = width-count-(space-1)*(n_words-1) #计算用space数量的空格填充后还缺多少空格
  8.                 if len(lst)>1: #如果单词数量>1,把剩余缺少的空格依次插入到每个单词后(除最后一个单词不用插入)
  9.                         for i in range(absent+1):
  10.                                 lst[i%(n_words-1)] += ' '
  11.                 return (' '*space).join(lst) + '\n' #返回调整好的该行文本,以回车结尾
  12.         txt_list = txt.split(' ') #分割单词
  13.         count = 0 #每行字符数计数器
  14.         output = '' #输出字符串
  15.         tmp = [] #每行的单词列表
  16.         for t in txt_list:
  17.                 if count + len(t) <= width: #若每行宽度不足width
  18.                         tmp.append(t) #把单词加入到每行的单词列表中
  19.                         count += len(t)+1 #计数器累加(外加1个空格的长度)
  20.                 else: #若宽度超过width
  21.                         output += adjust(tmp, width, count)
  22.                         tmp = [] #重置每行单词列表
  23.                         count = 0 #重置计数器
  24.                         if count + len(t) <= width:
  25.                                 tmp.append(t)
  26.                                 count += len(t)+1
  27.         else: #最后一行的处理
  28.                 output += adjust(tmp, width, count)
  29.         return output

  30. print(adjust_txt(txt, 30))
复制代码


第二种:递归写法
  1. txt = "Hot work is one of the typical high risk work in work shop, if out of control, it will cause tragedy. We manage our hot work basing on the FM hot work permit system. Then, to make sure the fire risk are eliminated before we start hot work, what should we do? Please refer to this week's topic, hot work permit."
  2. wordslist = txt.split(' ')
  3. def adjust_txt(width, wordslist, count=0, line=[], output=''): #递归主程序
  4.     if not wordslist: #如果单词列表为空,则整理最后一行,输出output
  5.         if len(line) > 1:
  6.             i = 0
  7.             while len(' '.join(line))<width:
  8.                 line[i%(len(line)-1)] += ' '
  9.                 i += 1
  10.         return output+' '.join(line)
  11.     if count+len(wordslist[0])+1 <= width: #如果添加单词列表的第1项后的长度不超过width,则把这个单词添加到line列表中,递归执行。
  12.         return adjust_txt(width, wordslist[1:], count+len(wordslist[0])+1, line+[wordslist[0]], output)
  13.     else: #不然,就整理line列表,当该行的长度等于width时,输出output,继续调用递归执行。
  14.         if len(line) == 1:
  15.             return adjust_txt(width, wordslist[1:], len(wordslist[0]), [wordslist[0]], output+' '.join(line)+'\n')
  16.         else:
  17.             i = 0
  18.             while len(' '.join(line))<width:
  19.                 line[i%(len(line)-1)] += ' '
  20.                 i += 1
  21.             return adjust_txt(width, wordslist[1:], len(wordslist[0]), [wordslist[0]], output+' '.join(line)+'\n')
  22. print(adjust_txt(30, wordslist))
复制代码


虽然,2种写法的逻辑是一样的,不过我个人还是比较喜欢递归写法。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 05:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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