鱼C论坛

 找回密码
 立即注册
查看: 5110|回复: 35

Python:每日一题 380

[复制链接]
发表于 2020-4-20 11:45:48 | 显示全部楼层
# 找到所有的方括号,依次进行解码
  1. def decode(encoding_str):
  2.     def repeat(string, num):
  3.         str1 = ""
  4.         for i in range(num):
  5.             str1 += string
  6.         return str1
  7.     left_index = encoding_str.find('[')
  8.     while left_index != -1:
  9.         right_index = encoding_str.find(']')
  10.         reused_str = repeat(encoding_str[left_index+1:right_index], int(encoding_str[left_index-1]))
  11.         encoding_str = encoding_str.replace(encoding_str[left_index-1:right_index+1], reused_str)
  12.         left_index = encoding_str.find('[')
  13.     return encoding_str
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-1 12:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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