I have an assignment on python(2.7) which ask me to get output for a string. In the 4 question it is asked to give the letter of the alphabet that come earlier. This test only the first character of each word of the sentence. Example: "this is a sentence" the result should be "a" as it is the first letter of the alphabet.
Here is my code (including the previous questions of the assignment)
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
Doing so gives me the first letter of the first word of the sentence and not the earliest letter in the alphabet order.
Where did I do wrong? I have looked at this for the past 2 days, and I can't see where I am doing wrong.