【问题标题】:Python word counterPython字数计数器
【发布时间】:2015-03-12 06:58:15
【问题描述】:

我正在学校学习 Python 2.7 课程,他们告诉我们创建以下程序:

假设 s 是一串小写字符。

编写一个程序,打印 s 中字母按字母顺序出现的最长子串。

例如,如果 s = azcbobobegghakl ,那么你的程序应该打印

按字母顺序排列的最长子串是:beggh

在平局的情况下,打印第一个子字符串。

例如,如果 s = 'abcbcd',那么你的程序应该打印

按字母顺序排列的最长子串是:abc

我写了以下代码:

s = 'czqriqfsqteavw'

string = ''

tempIndex = 0
prev = ''
curr = ''

index = 0
while index < len(s):
    curr = s[index]
    if index != 0:
        if curr < prev:
            if len(s[tempIndex:index]) > len(string):
               string = s[tempIndex:index]
            tempIndex=index
        elif index == len(s)-1:
            if len(s[tempIndex:index]) > len(string):
               string = s[tempIndex:index+1]
    prev = curr
    index += 1

print 'Longest substring in alphabetical order is: ' + string

老师还给了我们一系列的测试字符串来试一试:

onyixlsttpmylw  
pdxukpsimdj  
yamcrzwwgquqqrpdxmgltap  
dkaimdoviquyazmojtex  
abcdefghijklmnopqrstuvwxyz  
evyeorezmslyn  
msbprjtwwnb  
laymsbkrprvyuaieitpwpurp  
munifxzwieqbhaymkeol   
lzasroxnpjqhmpr    
evjeewybqpc   
vzpdfwbbwxpxsdpfak    
zyxwvutsrqponmlkjihgfedcba  
vzpdfwbbwxpxsdpfak     
jlgpiprth  
czqriqfsqteavw 

除了最后一个,它们都可以正常工作,它会产生以下答案:

按字母顺序排列的最长子串是:cz

但它应该说:

按字母顺序最长的子串是:avw

我检查了代码一千次,没有发现错误。你能帮帮我吗?

【问题讨论】:

标签: python string python-2.7


【解决方案1】:

我的解决方案类似于 Nim J 的解决方案,但它执行的迭代次数更少。

res = ""

for n in range(len(s)):
     for i in range(1, len(s)-n+1):
        if list(s[n:n+i]) == sorted(s[n:n+i]):
            if len(list(s[n:n+i])) > len(res):
                res = s[n:n+i]

print("Longest substring in alphabetical order is:", res)

【讨论】:

    【解决方案2】:

    我自己也遇到过这个问题,并认为我会分享我的答案。

    我的解决方案 100% 有效。

    问题是帮助新的 Python 编码人员理解循环,而无需深入研究其他复杂的解决方案。这段代码比较扁平化,使用变量名方便新程序员阅读。

    我添加了 cmets 来解释代码步骤。没有 cmets 它非常干净且可读。

    s = 'czqriqfsqteavw'
    
    test_char = s[0]
    temp_str = str('')
    longest_str = str('')
    
    for character in s:
    
        if temp_str == "":                   # if empty = we are working with a new string
            temp_str += character            # assign first char to temp_str
            longest_str = test_char          # it will be the longest_str for now
    
        elif character >= test_char[-1]:     # compare each char to the previously stored test_char
            temp_str += character            # add char to temp_str
            test_char = character            # change the test_char to the 'for' looping char
    
            if len(temp_str) > len(longest_str): # test if temp_char stores the longest found string
                longest_str = temp_str           # if yes, assign to longest_str
    
        else:
            test_char = character            # DONT SWAP THESE TWO LINES.
            temp_str = test_char             # OR IT WILL NOT WORK.
    
    print("Longest substring in alphabetical order is: {}".format(longest_str))
    

    【讨论】:

      【解决方案3】:

      指数是你的朋友。 下面是解决问题的简单代码。

      longword = ''
      
      for x in range(len(s)-1):
          for y in range(len(s)+1):
              word = s[x:y]
              if word == ''.join(sorted(word)):
                  if len(word) > len(longword):
                      longword = word
      print ('Longest substring in alphabetical order is: '+ longword)                
      

      【讨论】:

        【解决方案4】:

        我看到 user5402 很好地回答了你的问题,但是这个特殊的问题让我很感兴趣,所以我决定重新编写你的代码。 :) 下面的程序使用与您的代码基本相同的逻辑,只是做了一些小的改动。

        在实际情况下避免使用索引并直接迭代字符串(或其他容器对象)的内容被认为更符合 Pythonic。这通常会使代码更易于阅读,因为我们不必同时跟踪索引和内容。

        为了访问字符串中的当前和前一个字符,我们将输入字符串的两个副本压缩在一起,其中一个副本通过在开头插入一个空格字符来偏移。我们还在另一个副本的末尾附加了一个空格字符,这样当最长的有序子序列出现在输入字符串的末尾时,我们就不必进行特殊处理。

        #! /usr/bin/env python
        
        ''' Find longest ordered substring of a given string 
        
            From http://stackoverflow.com/q/27937076/4014959
            Written by PM 2Ring 2015.01.14
        '''
        
        data = [
            "azcbobobegghakl",
            "abcbcd",
            "onyixlsttpmylw",
            "pdxukpsimdj",
            "yamcrzwwgquqqrpdxmgltap",
            "dkaimdoviquyazmojtex",
            "abcdefghijklmnopqrstuvwxyz",
            "evyeorezmslyn",
            "msbprjtwwnb",
            "laymsbkrprvyuaieitpwpurp",
            "munifxzwieqbhaymkeol",
            "lzasroxnpjqhmpr",
            "evjeewybqpc",
            "vzpdfwbbwxpxsdpfak",
            "zyxwvutsrqponmlkjihgfedcba",
            "vzpdfwbbwxpxsdpfak",
            "jlgpiprth",
            "czqriqfsqteavw",
        ]
        
        
        def longest(s):
            ''' Return longest ordered substring of s
                s consists of lower case letters only.
            '''
            found, temp = [], []
            for prev, curr in zip(' ' + s, s + ' '):
                if curr < prev:
                    if len(temp) > len(found):
                        found = temp[:]
                    temp = []
                temp += [curr]
            return ''.join(found)
        
        
        def main():
            msg = 'Longest substring in alphabetical order is:'
            for s in data:
                print s
                print msg, longest(s)
                print
        
        
        if __name__ == '__main__':
            main()  
        

        输出

        azcbobobegghakl
        Longest substring in alphabetical order is: beggh
        
        abcbcd
        Longest substring in alphabetical order is: abc
        
        onyixlsttpmylw
        Longest substring in alphabetical order is: lstt
        
        pdxukpsimdj
        Longest substring in alphabetical order is: kps
        
        yamcrzwwgquqqrpdxmgltap
        Longest substring in alphabetical order is: crz
        
        dkaimdoviquyazmojtex
        Longest substring in alphabetical order is: iquy
        
        abcdefghijklmnopqrstuvwxyz
        Longest substring in alphabetical order is: abcdefghijklmnopqrstuvwxyz
        
        evyeorezmslyn
        Longest substring in alphabetical order is: evy
        
        msbprjtwwnb
        Longest substring in alphabetical order is: jtww
        
        laymsbkrprvyuaieitpwpurp
        Longest substring in alphabetical order is: prvy
        
        munifxzwieqbhaymkeol
        Longest substring in alphabetical order is: fxz
        
        lzasroxnpjqhmpr
        Longest substring in alphabetical order is: hmpr
        
        evjeewybqpc
        Longest substring in alphabetical order is: eewy
        
        vzpdfwbbwxpxsdpfak
        Longest substring in alphabetical order is: bbwx
        
        zyxwvutsrqponmlkjihgfedcba
        Longest substring in alphabetical order is: z
        
        vzpdfwbbwxpxsdpfak
        Longest substring in alphabetical order is: bbwx
        
        jlgpiprth
        Longest substring in alphabetical order is: iprt
        
        czqriqfsqteavw
        Longest substring in alphabetical order is: avw
        

        【讨论】:

          【解决方案5】:

          这些行:

                  if len(s[tempIndex:index]) > len(string):
                     string = s[tempIndex:index+1]
          

          彼此不同意。如果新的最佳字符串是s[tempIndex:index+1],那么这就是您应该在 if 条件中比较长度的字符串。将它们更改为彼此一致可以解决问题:

                  if len(s[tempIndex:index+1]) > len(string):
                     string = s[tempIndex:index+1]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-07-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-04
            • 2022-12-03
            • 1970-01-01
            • 2018-07-31
            相关资源
            最近更新 更多