Python学习7_OnlineResource_EXE2

# D. verbing
# Given a string, if its length is at least 3,
# add 'ing' to its end.
# Unless it already ends in 'ing', in which case
# add 'ly' instead.
# If the string length is less than 3, leave it unchanged.
# Return the resulting string.
def verbing(s):
  if len(s) >= 3 :
    if s[len(s) - 3 :] == 'ing' :
      return s + 'ly'
    else :
      return s + 'ing'
  else :
    return s
'''def verbing(s):
  if len(s) >= 3 :
    if s.endswith('ing') :
      return s + 'ly'
    else :
      return s + 'ing'
  else :
    return s'''
    
# 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):
  n = s.find('not')
  b = s.find('bad')
  return s.replace(s[n : b + 3],'good') if n < b else s


# F. front_back
# Consider dividing a string into two halves.
# If the length is even, the front and back halves are the same length.
# If the length is odd, we'll say that the extra char goes in the front half.
# e.g. 'abcde', the front half is 'abc', the back half 'de'.
# Given 2 strings, a and b, return a string of the form
#  a-front + b-front + a-back + b-back
def front_back(a, b):
  la = len(a)
  lb = len(b)
  if la%2 == 0:
    if lb%2 == 0:
      '''#两个数相除
divisionNumber=9/2
print(divisionNumber)      #输出结果:4
divisionNumber=9.0/2
print(divisionNumber)      #输出结果:4.5
divisionNumber=9/2.0
print(divisionNumber)      #输出结果:4.5
#/---除数或被除数中有任意一个是小数的话,商也会保留小数,反之取整---/
#除法运算// 返回商的整数部分,抛弃余数
divisorNumber=10//3
print(divisorNumber)        #输出结果:3
      '''
      print('la/2: ',la/2,' lb/2: ',lb/2)
      print('la/2: ',la//2,' lb/2: ',lb//2)
      return a[:la//2] + b[:lb//2] + a[la//2:] + b[lb//2:]
    else:
      return a[:la//2] + b[:lb//2 + 1] + a[la//2:] + b[lb//2 + 1:]
  else:
    if lb%2 == 0:
      return a[:la//2 + 1] + b[:lb//2] + a[la//2 + 1:] + b[lb//2:]
    else:
      return a[:la//2 + 1] + b[:lb//2 + 1] + a[la//2 + 1:] + b[lb//2 + 1:] 
  return


# Simple provided test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
  if got == expected:
    prefix = ' OK '
  else:
    prefix = '  X '
  print('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))


# main() calls the above functions with interesting inputs,
# using the above test() to check if the result is correct or not.
def main():
  print('verbing')
  test(verbing('hail'), 'hailing')
  test(verbing('swiming'), 'swimingly')
  test(verbing('do'), 'do')

  print
  print('not_bad')
  test(not_bad('This movie is not so bad'), 'This movie is good')
  test(not_bad('This dinner is not that bad!'), 'This dinner is good!')
  test(not_bad('This tea is not hot'), 'This tea is not hot')
  test(not_bad("It's bad yet not"), "It's bad yet not")

  print
  print('front_back')
  test(front_back('abcd', 'xy'), 'abxcdy')
  test(front_back('abcde', 'xyz'), 'abcxydez')
  test(front_back('Kitten', 'Donut'), 'KitDontenut')

if __name__ == '__main__':
  main()

OUTPUT IS:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值