Invalid output in python string chaining

I have an assignment in python (2.7) that asks me to get output for a string. In question 4, they are asked to give the letter of the alphabet that was before. This test contains only the first character of each sentence word. Example: “this sentence”, the result should be “a”, since this is the first letter of the alphabet.

Here is my code (including previous assignment questions)

def GetNumWords ( Sentence ):
    Count = 0
    Length = len( Sentence )
    Index = 0
    while Index < Length:
        Char = Sentence [ Index ]
        if Char != ' ':
            Count += 1
            while Char != ' ' and Index < Length:
                Char = Sentence [ Index ]
                Index += 1
        else:
            Index += 1
    return Count

def GetWordNum ( Sentence, WordNum ):
    Count = 0
    Length = len( Sentence )
    Index = 0
    Word = ''   
    while Index < Length:
        Char = Sentence [ Index ]
        if Char != ' ':
            Count += 1
            while Char != ' ' and Index < Length:
                Char = Sentence [ Index ]
                Index += 1
                if Count == WordNum:
                    Word = Word + Char
        else:
            Index += 1
    if Word == '':
        return ''
    else:
        return Word

def GetFirstLetter ( Sentence, SpecificNum):
    TheWord = GetWordNum ( Sentence, SpecificNum )
    if TheWord == '':
        return ''
    else:
        FirstLetter = TheWord [ 0 ]
        return FirstLetter

def GetEarliestLetter ( Sentence ):
    CurrentMinNum = 1
    CurrentMin = GetFirstLetter ( Sentence, CurrentMinNum )
    LastWord = GetNumWords ( Sentence )
    if CurrentMin == '':
        return ''
    else:
        while CurrentMinNum <=  LastWord:
            FirstLetter = CurrentMin
            if FirstLetter < CurrentMin:
                CurrentMin = FirstLetter
                CurrentMinNum += 1
            else:
                break
        return CurrentMin

This gives me the first letter of the first word of the sentence, and not the earliest letter in alphabetical order.

Where am I wrong? I have been looking at this for the last two days, and I don’t see what I am doing wrong.

+4
source share
2 answers

- str- while, raw_input len, , :

def first_alpha():
    s = raw_input()
    mn, curr = s[0], s[0]
    i = 1
    while i < len(s):
        if s[i] == " ":
            if curr < mn:
                mn = curr
            curr = s[i+1]
        i += 1
    return mn if curr > mn else curr

, , , .

In [5]: first_alpha()
this is a sentence
Out[5]: 'a'    
In [6]: first_alpha()
lowest char is trailing a
Out[6]: 'a'    
In [7]: first_alpha()
lowest char is upper A
Out[7]: 'A'

, min(word[0] for word in s.split()) , .

:

def first_alpha():
    s = raw_input()
    # ord("{") == 123
    mn, curr = "{", "{"
    i = 0
    # catch line ending in a space
    ln = len(s) if s[-1] != " " else len(s) - 1
    while i < ln:
        if s[i] == " ": 
            if curr <= mn:
                mn = curr
            ch = s[i+1]
            # make sure we have a letter
            if "a" <= ch <= "z" or "A" <= ch <= "Z":
                curr = ch
        i += 1
    return mn if curr > mn else curr

:

In [29]: first_alpha()
this is sentence where the lowest is ! but we return a
Out[29]: 'a'
In [30]: first_alpha()
lots of   spaces and    but we return a     
Out[30]: 'a'

, , }, ,

+2

, , , .

>>> s = "this is a sentence"
>>> min(c for c in s if c.isalpha())
'a'
+6