Checking if a string's characters are ascending alphabetically and its ascent is evenly spaced python

前端 未结 4 1821
名媛妹妹
名媛妹妹 2020-12-12 01:09

So need to check if a string\'s characters are ascending alphabetically and if that ascent is evenly spaced.

a = \"abc\"
b = \"ceg\"

So a i

4条回答
  •  情深已故
    2020-12-12 01:19

    >>> from itertools import product
    >>> from string import lowercase
    >>> a="abc"
    >>> any(a in lowercase[i::j+1] for i,j in product(range(26),repeat=2))
    True
    >>> b="ceg"
    >>> any(b in lowercase[i::j+1] for i,j in product(range(26),repeat=2))
    True
    >>> c="longer"
    >>> any(c in string.lowercase[i::j+1] for i,j in product(range(26),repeat=2))
    False
    >>> d="bdfhj"
    >>> any(d in string.lowercase[i::j+1] for i,j in product(range(26),repeat=2))
    True
    

    It's not necessary to use product, and a little more efficient to do it this way

    >>> any(a in string.lowercase[i::j+1] for i in range(26) for j in range(26-i))
    True
    

提交回复
热议问题