给定一个大写字母和一个整数数字,分别表示罗盘指针当前所指的朝向(N,NE,E,SE,S,SW,W,NW)和需要转动的角度数(45度的倍数)。如果角度是正数,表示顺时针旋转;如果角度是负数,则代表逆时针旋转。请编写一个函数,接收朝向和角度值,返回转动后的最终朝向。
备注:
朝向:N表示North,正北方向;NE表示NorthEast,东北方向。
角度:45°的倍数,范围是-1080°~1080°。
示例:
输入:“S”, 180,返回:“N”
解释:S表示South正南,旋转180度结果为正北,即N。
题目难度:简单
题目来源:CodeWars:Turn with a Compass
def solution(direction: str, degree: int) -> str:
# your code
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
nobugs
2021 年12 月 14 日 02:35
2
def solution(direction: str, degree: int) -> str:
direction_dict = {
'N': 0,
'NE': 45,
'E': 90,
'SE': 135,
'S': 180,
'SW': 225,
'W': 270,
'NW': 315,
}
if degree % 45 != 0:
raise ValueError("角度:45°的倍数,范围是-1080°~1080°")
return {v: k for k, v in direction_dict.items()}[abs(direction_dict[direction] + degree) % 360]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction,degree):
direction_list=["N","NE","E","SE","S","SW","W","NW"]
num = direction_list.index(direction)
if degree % 45 != 0:
raise ValueError("角度:45°的倍数,范围是-1080°~1080°")
if abs(degree)>360:
temp_n=degree%360
n=temp_n/45
else:
n=degree/45
result_n=num+n
if abs(result_n)>=len(direction_list):
result_n=result_n%len(direction_list)
return direction_list[int(result_n)]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
public String solution(String s, int i){
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("N", "NE", "E", "SE", "S", "SW", "W", "NW"));
// 步数
int step = Math.abs(i) / 45;
// 找到当前方向在数组中的位置
int index = arrayList.indexOf(s);
if(i>0){
// 右移
return arrayList.get((index+step)%arrayList.size());
}else {
// 左移
return arrayList.get(Math.abs(index-step)%arrayList.size());
}
}
Kawi
2021 年12 月 14 日 05:33
5
direction_dict = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']
def solution(direction: str, degree: int) -> str:
num = direction_dict.index(direction)
if degree % 45 != 0:
raise ValueError("角度:45°的倍数,范围是-1080°~1080°")
i = int(abs(degree / 45))
if degree < 0:
num = (num - i) % 8
return direction_dict[num]
else:
num = (num + i) % 8
return direction_dict[num]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int) -> str:
# your code
list_1 = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
list_2 = [0, 45, 90, 135, 180, 225, 270, 315]
a = list_1.index(direction)
if -1080 < degree < 1080 and degree % 45 == 0:
return list_1[list_2.index((list_2[a] + degree) % 360)]
else:
raise ValueError("角度:45°的倍数,范围是-1080°~1080°")
assert solution("S", 90) == "W"
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction:str,degree:int) -> str :
direc_dict={‘N’:0,’NE’:45,’E’:90,’SE’:135,’S’:180,’SW’:225,’W’:270,’NW’:315}
for k, v in direc_dict.items():
if v ==(lambda c: c if c>= 0 else c+360)((direc_dict.get(direction)+degree)% 360):
return k
lekaixin
(ALe阿乐)
2021 年12 月 14 日 07:27
9
def solution(direction: str, degree: int) -> str:
dic={'N':0,'NE':45,'E':90,'SE':135,'S':180,'SW':225,'W':270,'NW':315}
D=dic[direction]+degree
D1=D%360
for i in dic.keys():
if dic[i] == D1:
return i
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
Mooii
2021 年12 月 14 日 07:42
10
def solution(direction: str, degree: int) -> str:
direction_dict = {
'N': 0,
'NE': 45,
'E': 90,
'SE': 135,
'S': 180,
'SW': 225,
'W': 270,
'NW': 315,
}
if -1080 <= degree <= 1080 and degree % 45 == 0:
val = (direction_dict[direction]+degree) % 360
for i in direction_dict.items():
if val == i[1]:
return i[0]
else:
raise ValueError("角度错误;角度需为45°的倍数,范围是-1080°~1080°")
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int) -> str:
data = {
'N':0,
'NE':45,
'E':90,
'SE':135,
'S':180,
'SW':225,
'W':270,
'NW':315,
}
#对输入的值判断异常
if degree%45 != 0 and degree < -1080 and degress > 1080:
print("请输入45°的倍数,范围是-1080°~1080°")
#对输入的值分类
num = data[direction] + degree
if num > 0:
for k,v in data.items():
if v == num%360:
print(k, v)
return k
if num < 0:
num = 360 - num%360
for k, v in data.items():
if v == num:
print(k, v)
return k
if degree == 0:
print(direction, degree)
return direction
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def move_compass(direction: str, degree: int)->int:
dic = {"N": 0, "S": 180, "NE":45, "E":90,"SE":135,"SW":225,"W":270,"NW": 315 }
n = dic[direction]+degree
while n<0:
n+=360
while n>= 360:
n-=360
for i ,v in dic.items():
if v == n:
return i
def solution(direction: str, degree: int) -> str:
data_dict = {'N': 0, 'NE': 45, 'E': 90, 'SE': 135, 'S': 180, 'SW': 225, 'W': 270, 'NW': 315}
if degree >= -1080 and degree <= 1080 and degree % 45 == 0:
value = (data_dict[direction] + degree) % 360
for k, v in data_dict.items():
if v == value:
return k
else:
raise ValueError("45°的倍数,范围是-1080°~1080°")
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
assert solution("N", -45) == "NW"
zhonghc
(格度)
2021 年12 月 15 日 01:33
14
def solution(direction: str, degree: int) -> str:
direct_list = ['N','NE', 'E', 'SE', 'S','SW','W','NW']
index = direct_list.index(direction)
number = degree / 45
plus = int((number+index) % len(direct_list))
return direct_list[plus]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int) → str:
direct_list = [‘N’,‘NE’, ‘E’, ‘SE’, ‘S’,‘SW’,‘W’,‘NW’]
index = direct_list.index(direction)
number = degree / 45
plus = int((number+index) % len(direct_list))
return direct_list[plus]
assert solution(“S”, 180) == “N”
assert solution(“SE”, -45) == “E”
assert solution(“W”, 495) == “NE”
public String solution(String str, int num){
ArrayList<String> list = new ArrayList<>(Arrays.asList("N", "NE", "E", "SE", "S", "SW", "W", "NW"));
String direction;
int a = num/45;
int b = list.indexOf(str);
// 旋转后的下标
int c = b+a;
// 旋转后超出数组下标
if (c+1>list.size()){
direction = list.get(c%8);
// 反方向旋转
}else if (c<0){
direction = list.get(list.size()+c%8);
}else {
direction = list.get(c);
}
return direction;
}
Huis
2021 年12 月 15 日 08:37
16
def solution(direction: str, degree: int) -> str:
souths = ('N','NE','E','SE','S','SW','W','NW')
return souths[(souths.index(direction)+degree//45)%len(souths)]
def solution(direction: str, degree: int) -> str:
dList = ["N","NE","E","SE","S","SW","W","NW"]
postion = [(i + int(degree/45) -len(dList))%8 for i in range(len(dList)) if dList[i] == direction ][0]
return dList[postion]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"