首页 > 编程语言 > 详细

Python快速入门(2)练习题

时间:2014-01-25 16:18:47      阅读:524      评论:0      收藏:0      [点我收藏+]
# C. fix_start
# Given a string s, return a string
# where all occurences of its first char have
# been changed to ‘*‘, except do not change
# the first char itself.
# e.g. ‘babble‘ yields ‘ba**le‘
# Assume that the string is length 1 or more.
# Hint: s.replace(stra, strb) returns a version of string s
# where all instances of stra have been replaced by strb.
		def fix_start(s):
		  # +++your code here+++
		  # LAB(begin solution)
		  front = s[0]
		  back = s[1:]
		  fixed_back = back.replace(front, ‘*‘)
		  return front + fixed_back


# E. not_bad
# Given a string, find the first appearance of the
# substring ‘not‘ and ‘bad‘. If the ‘bad‘ follows
# the ‘not‘, replace the whole ‘not‘...‘bad‘ substring
# with ‘good‘.
# Return the resulting string.
# So ‘This dinner is not that bad!‘ yields:
# This dinner is good!

		def not_bad(s):
		  # +++your code here+++
		  # LAB(begin solution)
		  n = s.find(‘not‘)
		  b = s.find(‘bad‘)
		  if n != -1 and b != -1 and b > n:
			s = s[:n] + ‘good‘ + s[b+3:]
		  return s


Python快速入门(2)练习题

原文:http://blog.csdn.net/tao_sun/article/details/18743387

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!