鱼C论坛

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

[技术交流] Stringing me along

[复制链接]
发表于 2019-11-24 12:48:47 | 显示全部楼层 |阅读模式

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

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

x
Implement a function that receives a string, and lets you extend it with repeated calls. When no argument is passed you should return a string consisting of space-separated words you've received earlier.
Note: there will always be at least 1 string; all inputs will be non-empty.

For example:

  1. create_message("Hello")("World!")("how")("are")("you?")() == "Hello World! how are you?"
复制代码


我的解答:


  1. def create_message(s):
  2.     strings = []

  3.     def _msg(string=None):
  4.         if string is None:
  5.             return " ".join(strings)
  6.         strings.append(string)
  7.         return _msg
  8.    
  9.     return _msg(s)
复制代码

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2019-11-24 12:58:07 | 显示全部楼层
闭包+递归
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 11:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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