题解 | #坐标移动#正则表达式
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import re
shuru = input()
actions = []; numbers = []
# 对输入进行处理
pattern = r"(?<![\w])(?P<action>[ASWD])(?P<number>\d+);"
matchIter = re.finditer(pattern=pattern, string=shuru)
for m in matchIter:
actions.append(m.group('action'))
numbers.append(int(m.group('number')))
pass
startI, startJ = 0, 0
changeDict = {
'A': (-1, 0),
'D': (1, 0),
'W': (0, 1),
'S': (0, -1)
}
for a, n in zip(actions, numbers):
startI += changeDict[a][0]*n
startJ += changeDict[a][1]*n
pass
print(str(startI)+','+str(startJ))
查看14道真题和解析
