def solution(direction: str, degree: int) -> str:
direction_list = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
if degree % 45 == 0:
res_index = (degree // 45 + direction_list.index(direction)) % 8
return direction_list[res_index]
else:
return False
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
qunzi
(群子)
22
def solution(direction: str, degree: int)-> str:
compass=["N","NE","E","SE","S","SW","W","NW"]
n=degree/45 #需要移动位移长度
index_dir=compass.index(direction) ##目前字符所在的索引位置是
mo=int(index_dir+n) ##移动后得索引
if mo<8:
return compass[mo]
else:
return compass[int(mo%8)]
lekaixin
(ALe阿乐)
23
def solution(direction: str, degree: int)-> str:
dict={"N":0,"NE":45,"E":90,"SE":135,"S":180,"SW":225,"W":270,"NW":315}
degree_res = dict[direction] + degree
while degree_res<0 or degree_res>=360:
if degree_res<0:
degree_res+=360
else:
degree_res-=360
return [i for i in dict.keys() if dict[i]==degree_res][0]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"