2015-07-13 156 views
4

我有一个关于python(2.7)的任务,它要求我为字符串获取输出。 在这个问题中,它被要求提供前面的字母表。这个测试只测试句子每个单词的第一个字符。 示例:“这是一个句子”结果应该是“a”,因为它是字母表的第一个字母。python字符串循环输出错误

这里是我的代码(包括分配的以前的问题)

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 

这样做给我一句的第一个字的第一个字母,而不是在字母顺序最早的字母。

我在哪里做错了?过去两天我已经看过这个,我看不出我做错了什么。

+2

字符串是否总是小写字母和所有字母字符? –

+0

@PadraicCunningham:不要忘记空间。 :)似乎sylvain使用Python 2,因为他们在评论中提到了“raw_input”,所以我想我们可以安全地假设这些字符串是字节字符串,而不是Unicode。 OTOH,他们_might_可能包含重音字母... –

+1

顺便说一句,在Python中用常规的方式写出全部小写的普通变量名。以大写字母开头的名称通常用于类。 Stack Overflow语法突出显示了青色的类名,所以你的脚本看起来有点奇怪,并且让Python退伍军人感到震惊。 –

回答

2

如果您不能使用任何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' 

唯一的边缘的情况下将是一个字符串,你有没有字母,那么最低将是}所以你可以决定在这种情况下应该发生什么

+0

嗯,为什么你将's'作为参数并使用'raw_input'来获取它? –

+0

@ PM2Ring,刚刚传递一个字符串来测试显示输出忘记删除了Rightio的s –

+0

。从他们的示例代码中,我_think_ OP实际上想要将's'作为参数传递,而不是在函数内部调用'raw_input'。另外,如果句子以空格结束,你的代码就会崩溃,因为'[i + 1]'会引发'IndexError'。但我想这不会发生在他们正在使用的有限输入数据上...... –

6

我想你可能会过度复杂。

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

公顷,对不起,我忘了说我不能使用任何库进行任务。只有“输入”“raw_input”和“len” –

+3

没有外部库,min和isalpha是在函数/方法中构建的 –

+0

目标是强制我们学习困难的方式,但他没有精确的isalpha。他告诉我们,我们应该比较句子每个单词的每个第一个字符。每次更换最小字符时,如果比较之前是。 –