找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2503|回复: 3

[求助] 初学python几天,两个函数的题目应该怎么实现?

1

主题

3

帖子

3

积分

贫民

积分
3
youkedy 发表于 2017-10-18 18:42:45 | 显示全部楼层 |阅读模式
def repeat_letter(words, char_positions, repl_times):
    """ (list of str, list of int, int) -> NoneType

    Precondition: (len(words) == len(char_positions)) and (repl_times > 0)

    Modify every word in words by replicating the character
    at index specified by the corresponding element in char_positions
    repl_times additional times. If the index specified in char_positions is not
    valid, leave the word unchanged.

    >>> words = ['hello', 'exercise', 'with', 'words']
    >>> repeat_letter(words, [4, 2, 10, -2], 3)
    >>> words
    ['helloooo', 'exeeeercise', 'with', 'wordddds']
    """

def sum_numbers(s):
    """ (str) -> int

    Preconditions:
    - s is not the empty string
    - s contains only non-negative integers seperated by a single space.
    - the first character of s is not a space; neither is the last one.

    Return the sum of all the integers in s.

    # Requirements: Do NOT use lists. You must use a for loop.

    >>> sum_numbers('45')
    45
    >>> sum_numbers('34 3 542 11')
    590
    """


回复

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
youkedy  楼主| 发表于 2017-10-19 13:07:09 | 显示全部楼层
自己研究出来了   



def repeat_letter(words, char_positions, repl_times):
    if len(words) != len(char_positions):
        print('Error:len(words) != len(char_positions)!')
        return
    if repl_times <= 0:
        print('Error:repl_times <= 0!')
        return
    i = 0
    while i < len(words):   
        if len(words[i]) > abs(char_positions[i]):
            words[i] = words[i][0:char_positions[i]]+words[i][char_positions[i]]*repl_times+words[i][char_positions[i]:]
        else:
            words[i] = words[i]        
        i += 1


words = ['hello', 'exercise', 'with', 'words']
print("old words: %s" % words)
repl_times = 3
char_positions = [4, 2, 10, -2]
repeat_letter(words, char_positions, repl_times)
print("new words: %s" % words)

words = ['hello', 'exercise', 'with', 'words']
print("old words: %s" % words)
repl_times = 3
char_positions = [5, 8, 4, 5]
repeat_letter(words, char_positions, repl_times)
print("new words: %s" % words)
回复 支持 反对

使用道具 举报

0

主题

5

帖子

5

积分

贫民

积分
5
zhnglicho 发表于 2017-10-19 13:48:07 | 显示全部楼层
  1. def repeat_letter(words,char_positions,repl_times):
  2.     """ (list of str, list of int, int) -> NoneType

  3.     Precondition: (len(words) == len(char_positions)) and (repl_times > 0) """
  4.    
  5.     for key,value in enumerate(words):        
  6.         p = char_positions[key]
  7.         if p<len(value):            
  8.             words[key] = value[:p]+value[p]*repl_times+value[p:]


  9. def sum_numbers(s):
  10.     """ (str) -> int

  11.     Preconditions:
  12.     - s is not the empty string
  13.     - s contains only non-negative integers seperated by a single space.
  14.     - the first character of s is not a space; neither is the last one.

  15.     Return the sum of all the integers in s.

  16.     # Requirements: Do NOT use lists. You must use a for loop."""
  17.     numbers = s.split(' ')
  18.     n_sum = 0
  19.     for n in numbers:
  20.         n_sum = n_sum + int(n)

  21.     return n_sum
复制代码
回复 支持 反对

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
youkedy  楼主| 发表于 2017-10-19 16:34:30 | 显示全部楼层

高手写的果然很精炼!谢谢!
回复 支持 反对

使用道具 举报

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

本版积分规则

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