zoukankan      html  css  js  c++  java
  • python3 字符串单词反转

    code

    class Solution:
        # 按照书上的方法进行编写
        # 因为Python的字符串结束没有结束符, 所以需要判断最后的pEnd是否已经指到最后一个字符
        # 如果已经指到最后一个字符, 则直接在复制之后跳出循环
        # 测试用例'I am a student.'' '''
        def ReverseSentence(self, s):
            if s == None or len(s) <= 0:
                return ''
            strList = list(s)
            strList = self.Reverse(strList)
            pBegin = 0
            pEnd = 0
            resultStr = ''
            listTemp = []
    
            while pEnd < len(s):
                # 如果字符串长度为1, 直接跳出循环
                # 如果pEnd指针指到最后一个字符, 跳出循环
                if pEnd == len(s)-1:
                    listTemp.append(self.Reverse(strList[pBegin:]))
                    break
                # 这个判断语句位置需要靠前, 用来鉴定字符串开头是否是空格的情况
                if strList[pBegin] == ' ':
                    pBegin += 1
                    pEnd += 1
                    listTemp.append(' ')
                elif strList[pEnd] == ' ':
                    listTemp.append(self.Reverse(strList[pBegin:pEnd]))
                    pBegin = pEnd
                else:
                    pEnd += 1
            # print(listTemp)
            for i in listTemp:
                resultStr += ''.join(i)
            return resultStr
        # 翻转字符list
        def Reverse(self, alist):
            if alist == None or len(alist) <= 0:
                return ''
            startIndex = 0
            endIndex = len(alist) - 1
            while startIndex < endIndex:
                alist[startIndex], alist[endIndex] = alist[endIndex], alist[startIndex]
                startIndex += 1
                endIndex -= 1
            return alist
    
        # 直接利用Python的语句进行字符串的翻转
        def ReverseSentence2(self, s):
            l = s.split(' ')
            return ' '.join(l[::-1])
    
    
    str = 'I am a student .'
    s = Solution()
    print(s.ReverseSentence2(str))
    print(s.ReverseSentence(str))

     outputs

    macname@MacdeMBP ~ % python3 test.py
    . student a am I
    . student a am I
    macname@MacdeMBP ~ % 

  • 相关阅读:
    python3+selenium框架设计04-封装测试基类
    python3+selenium框架设计02-自动化测试框架需要什么
    python3+selenium框架设计01-Page Object
    python3+selenium入门16-窗口截图
    python3+selenium入门15-执行JavaScript
    爬虫入门
    NLP整体流程的代码
    NLTK与NLP原理及基础
    NLTK词性标注解释
    [hdu4355]Party All the Time(三分)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/13766740.html
Copyright © 2011-2022 走看看