<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    xylz,imxylz

    關注后端架構、中間件、分布式和并發編程

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks

    6-12. Strings.

    (1)Create a function called findchr(), with the following declaration:

    def findchr(string, char)
    findchr() will look for character char in string and return the index of the first occurrence of char, or -1 if that char is not part of string. You cannot use string.*find() or string.*index() functions or methods.

    (2)Create another function called rfindchr() that will find the last occurrence of a character in a string. Naturally this works similarly to findchr(), but it starts its search from the end of the input string.

    (3)Create a third function called subchr() with the following declaration:

    def subchr(string, origchar, newchar)
    subchr() is similar to findchr() except that whenever origchar is found, it is replaced by newchar. The modified string is the return value.
     

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0612.py 140 2010-05-27 04:10:06Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10
    11def findchr(s,ch):
    12    """
    13    Look for character 'ch' in 's' and return the index of the first occurrence of 'ch', or -f if that 'ch' is not part of 's'
    14    """
    15    if s is None or len(s)==0: return -1
    16    for i,c in enumerate(s):
    17        if c == ch: return i
    18    return -1
    19
    20def rfindchr(s,ch):
    21    """
    22    Look for character 'ch' in 's' and return the index of the last occurrence of 'ch', or -f if that 'ch' is not part of 's'
    23    """
    24    if s is None or len(s)==0: return -1
    25    for i in range(len(s)-1,-1,-1):
    26        if s[i] == ch: return i
    27    return -1
    28
    29def subchr(s,oldch,newch):
    30    """
    31    Look for character 'oldch' in 'newch' and replace each 'oldch' with 'newch' and return the string modified.
    32    """
    33    if s is None or len(s)==0: return s
    34    ret=[]
    35    for c in s:
    36        ret.append(c if c!=oldch else newch)
    37    return ''.join(ret)
    38
    39
    40if __name__ == '__main__':
    41    assert 1 == findchr('Good','o')
    42    try:
    43        assert 0 == findchr('Good','x')
    44        raise ValueError, 'Test fail.'
    45    except AssertionError as e:
    46        print e
    47    assert 2 == rfindchr('Good','o')
    48    assert 'Gxxd' == subchr('Good','o','x')
    49
    50
    在此類的測試程序中,使用assert斷言來測試正確性,如果測試失敗會拋出一個AssertionError的異常。


    ©2009-2014 IMXYLZ |求賢若渴
    posted on 2010-05-27 12:13 imxylz 閱讀(16758) 評論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 免费福利在线播放| 日韩免费在线视频| 永久黄网站色视频免费观看| 亚洲不卡视频在线观看| 欧洲乱码伦视频免费| 亚洲av永久综合在线观看尤物| 2020久久精品国产免费| 亚洲日本人成中文字幕| 毛片网站免费在线观看| 亚洲aⅴ无码专区在线观看| 免费一级做a爰片久久毛片潮喷| 亚洲国产精品无码久久| 亚洲Av无码乱码在线znlu| 国产精品综合专区中文字幕免费播放 | 亚洲乱色熟女一区二区三区蜜臀| 午夜神器成在线人成在线人免费| 亚洲AV第一成肉网| 亚洲国产成人精品无码久久久久久综合| 日韩成人精品日本亚洲| 亚洲人妻av伦理| 无码人妻一区二区三区免费n鬼沢| 中文字幕亚洲综合久久| 成人人观看的免费毛片| 国产亚洲精品精品精品| 亚洲色偷偷综合亚洲AVYP| 9277手机在线视频观看免费| 亚洲最大的成人网站| 亚洲AV无码一区二区三区在线观看 | 99久热只有精品视频免费看| 亚洲激情视频图片| 亚洲精品无码久久久久AV麻豆| 曰批全过程免费视频在线观看无码| 7777久久亚洲中文字幕蜜桃| 免费羞羞视频网站| 中国好声音第二季免费播放| 亚洲毛片基地日韩毛片基地| 日本免费电影一区| 日本免费在线观看| 亚洲Av无码国产一区二区| 国产aⅴ无码专区亚洲av| 18禁网站免费无遮挡无码中文|