开发者

check if string is in abc order

So the function should开发者_如何学JAVA count the number of times the letters in uppercase are out of abc order.

>>> abc('ABBZHDL')
    2

Above, z and d are out of order.

>>> abc('ABCD')
    0

>>> abc('DCBA')
    4

My code:

 def abc(check):
 order=ABCDEFGHIJKLMNOPQRSTUVWXYZ
   for c in check:
      if check != order:
   #then I get stuck here

Pointers?


The question is ill-defined. One solution to a nearby question would be using the builtin sorted():

def abc(s):
    count = 0
    s = ''.join(i for i in s if i.isupper())
    l = sorted(s)
    for i,c in enumerate(s):
        if l[i] != c:
            count += 1
    return count

It counts all of the places where the alphabetized string does not match the original.


def abc(check):
   last = ''
   count = 0
   for letter in check:
      if not letter.isupper():
           continue
      if letter < last:
           count += 1
      last = letter
   return count 


import string

a = 'acbdefr'
b = 'abdcfe'

assert ''.join(sorted(b)) in string.ascii_letters
assert ''.join(sorted(a)) in string.ascii_letters #should fail

Its really simple everyone seems to be overcomplicating it somewhat?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜