|
发表于 2022-12-5 09:37:39
|
显示全部楼层
本楼为最佳答案
- def posToIndex(s):
- n = [ x for x in "12345678"]
- dicta = {"a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7,\
- "A":0, "B":1, "C":2, "D":3, "E":4, "F":5, "G":6, "H":7}
- ss = s.strip()
- if len(ss)==2 or ss[1:(len(ss)-1)].isspace:
- if ss[0] in n and ss[-1] in dicta.keys() :
- x = n.index(ss[0])
- y = dicta[ss[-1]]
- return (max(x,y),min(x,y))
- elif ss[0] in dicta.keys() and ss[-1] in n:
- x = dicta[ss[0]]
- y = n.index(ss[-1])
- return (max(x,y),min(x,y))
- raise ValueError
- raise ValueError
复制代码- posToIndex("C4")
- (3, 2)
- posToIndex("4c")
- (3, 2)
- posToIndex(" 4 c")
- (3, 2)
- posToIndex(" x c")
- Traceback (most recent call last):
- File "<pyshell#49>", line 1, in <module>
- posToIndex(" x c")
- File "<pyshell#45>", line 15, in posToIndex
- raise ValueError
- ValueError
复制代码 |
|