|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
编写一个名为get_sum_string_lengths(words)的递归函数,该函数将字符串列表作为参数。此函数计算并返回列表中字符串长度的总和。
注意:您不得使用任何形式的循环。您必须使用递归来解决此问题。
Test
words = ['This', 'is', 'a', 'test']
print(get_sum_string_lengths(words))
Result
11
- def get_sum_string_lengths(words):
- word="".join(words)
- len_word=len(word)
- if len_word==0:
- return 0
- else:
- return len_word
复制代码
本帖最后由 hrp 于 2020-9-28 23:10 编辑
- words = ['This', 'is', 'a', 'test']
- def get_sum_string_lengths(wds):
- if len(wds) == 1:
- return len(wds[0])
- if len(wds) > 1:
- return len(wds[0]) + get_sum_string_lengths(wds[1:])
- return 0
- print(get_sum_string_lengths(words))
复制代码
|
|