Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong output in python string loop

Tags:

python

string

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.

like image 295
sylvain pissard maillet Avatar asked Feb 09 '23 14:02

sylvain pissard maillet


2 Answers

I think you might be overcomplicating it.

>>> s = "this is a sentence"
>>> min(c for c in s if c.isalpha())
'a'
like image 177
foz Avatar answered Feb 12 '23 03:02

foz


If you cannot use any str methods and can only use a while loop, raw_input and len then your input must be restricted so this will find the lowest first letter from each word:

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

Like I said this works for restricted inputs which are letters and the words are spaced by single spaces.

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'

Obviously min(word[0] for word in s.split()) would be a lot simpler way to do it if you were not restricted.

To catch only letters with non letters and catch spaces at the end:

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

Output:

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'

The only edge case would be a string where you had no letters, then the lowest would be } so you can decide what should happen in that case

like image 21
Padraic Cunningham Avatar answered Feb 12 '23 03:02

Padraic Cunningham