编程非常严格。您不能告诉程序“输出香蕉计数”,而必须告诉它print(bananas)。
但是,当您这样做时,最终会遇到一个问题:您不知道事先拥有多少个香蕉,因此您不知道是否使用复数。
有时,程序员会走懒惰的路。他们只检查而不是检查there are X banana(s)。
但这很丑陋,因此我们需要一个程序来解决此问题。
方法)
要删除字符串中的歧义复数,请执行以下步骤:
- 将空格上的字符串分成单词列表。 
- 对于以结尾的每个单词 - (s),请执行以下操作:- 如果前面的字是a,an,1或one,除去(s)在字的结尾。
- 否则,如果该字是第一个字在字符串中或前述字不是a,an,1或one,替换(s)在与字的末尾s。
 
- 如果前面的字是
- 将单词列表重新组合成一个字符串,以保留原始空格。 
例子)
让我们来一个字符串there's a banana(s) and three apple(s)。
首先,我们将字符串分成单词列表: ["there's", "a", "banana(s)", "and", "three", "apple(s)"]
第二步,我们使用两个以(s):banana(s)和结尾的单词apple(s)。
之前的单词banana(s)是a,因此我们删除了(s)它banana。这个词之前apple(s)是three的,所以我们改(s)到s,因此变得apples。
我们现在有["there's", "a", "banana", "and", "three", "apples"]。将列表重新加入,我们得到there's a banana and three apples。这是我们的最终结果。
挑战
创建一个程序或函数,该程序或函数采用任意合理格式的模糊字符串,并返回该字符串的明确版本。
您可以假定该字符串不包含任何换行符,制表符或回车符。
我忘了指定是否对空间或空间(即,是否组织分裂了okay then两个空格应该是["okay", "then"]或["okay", "", "then"]张贴的挑战时,那么你可以假设分裂两种形式)。
测试用例)
Input                                         -> Output
there are two banana(s) and one leprechaun(s) -> there are two bananas and one leprechaun
there's a banana(s) and three apple(s)        -> there's a banana and three apples
apple(s)                                      -> apples
one apple(s)                                  -> one apple
1 banana(s)                                   -> 1 banana
banana                                        -> banana
preserve    original      whitespace(s)       -> preserve    original      whitespaces
11 banana(s)                                  -> 11 bananas
an apple(s)                                   -> an apple
this is a te(s)t                              -> this is a te(s)t
I am a (s)tranger(s)                          -> I am a (s)tranger
计分
由于这是代码高尔夫球,因此字节数最少的提交将获胜!
apple(s)测试用例应该屈服apples吗?挑战指出,Otherwise, if the word is the first word in the string . . . replace the (s) at the end of the word with s.我注意到apples在前三个修订版的沙箱中产生了这种情况,但在第四次修订版中发生了变化。
                There's a single banana(s)-> There's a single bananas。