hogwarts
(霍格沃兹测试学院官方)
2022 年4 月 25 日 03:03
1
假设我们有一个拥有8个方位的罗盘(N, NE, E, SE, S, SW, W, NW),再给定一个需要转动的角度(45度的倍数),请 编写一个函数,求出转动后的角度。转动角度可以是正数,也可以是负数。
【示例】
输入:“S”, 180
输出:“N”
解释:S 表示正南,旋转180度后,就会指向正北,即 N
题目难度:简单
题目来源:CodeWars-Turn with a Compass
def solution(direction: str, degree: int)-> str:
# your code here
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int)-> str:
# your code here
compass_list = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
degree = degree % 360 if degree > 360 or degree < -360 else degree
new_degree = (compass_list.index(direction) + degree//45)
return compass_list[new_degree] if new_degree < len(compass_list) else compass_list[new_degree - len(compass_list)]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
assert solution("W", -495) == "SE"
def solution(direction: str, degree: int)-> str:
compass = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
n = int(degree/45)
mo = (compass.index(direction) + n) % len(compass)
return compass[mo]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int)-> str:
# your code here
b = int(degree/45)
li = ["N","NE",'E','SE','S','SW','W','NW']
a = li.index(direction) + b
while a not in range(0,8):
if a > 7:
a = a - 8
continue
if a < 7:
a = a + 8
continue
return li[a]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int) -> str:
compass = {"N": 0, "NE": 45, "E": 90, "SE": 135, "S": 180, "SW": 225, "W": 270, "NW": 315}
new_angles = compass[direction] + degree if compass[direction] + degree > 0 else 360 - abs(compass[direction] + degree)
return list(compass.keys())[list(compass.values()).index(new_angles % 360)]
Claudia
(米苏)
2022 年4 月 25 日 06:21
6
def solution(direction: str, degree: int)-> str:
direction_list = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
location_id = direction_list.index(direction)
num = (degree / 45) % len(direction_list)
num_after = int((num + location_id) % len(direction_list))
return direction_list[num_after]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int)-> str:
compass_list=list(('N','NE','E','SE', 'S', 'SW', 'W', 'NW'))
degree=degree if degree<360 else degree-360
step=int(degree/45)
if compass_list.index(direction)+step<=len(compass_list)-1:
return compass_list[compass_list.index(direction)+step]
return compass_list[step-((len(compass_list)-1)-compass_list.index(direction))-1]
_zyao
2022 年4 月 25 日 09:56
8
def solution(direction: str, degree: int)-> str:
direction_list = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
return direction_list[int(direction_list.index(direction) + degree / 45 ) % len(direction_list )]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def test_solution(direction: str, degree: int)-> str:
# your code here
ls = ['N','NE','E','SE','S','SW','W','NW']
return ls[(int(degree/45)+ls.index(direction))%len(ls)]
if __name__ == '__main__':
assert test_solution("S", 180) == "N"
assert test_solution("SE", -45) == "E"
assert test_solution("W", 495) == "NE"
assert test_solution("NW", -45) == "W"
assert test_solution("NW", 45) == "N"
一发枚举
from enum import Enum
CIRCLE = 360
class Directions(Enum):
N = 0
NE = 45
E = 90
SE = 135
S = 180
SW = 225
W = 270
NW = 315
def solution(direction: str, degree: int):
final = (Directions[direction].value + degree) % CIRCLE
return Directions(final).name
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class Compass {
static String compass(String direction, int degrees) {
// 创建 compass list 存储 罗盘方法
List<String> compass = new ArrayList<>(Arrays.asList("N", "NE", "E", "E", "SE", "S", "SW", "W", "NW"));
// 罗盘走的向量
int vector = degrees / 45;
// 获得罗盘最后一圈下的走向
int lastTowards = (compass.indexOf(direction) + vector) % compass.size();
// 利用 ArrayList.get() 方法,获取索引对应的值,因索引不能为负数,所以需要进行处理
String target = "";
if (lastTowards < 0) {
target = compass.get(compass.size() + lastTowards - 1);
} else {
target = compass.get(lastTowards);
}
return target;
}
@Test
void testCompass() {
assertAll(
() -> assertEquals(compass("S", 180), "N"),
() -> assertEquals(compass("SE", -45), "E"),
() -> assertEquals(compass("W", 495), "NE")
);
}
}
joyoko
(徐子宇)
2022 年4 月 26 日 05:51
12
def solution(direction: str, degree: int)-> str:
# your code here
compass_list = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
return compass_list[(compass_list.index(direction) + degree // 45) % 8]
1 个赞
def solution(direction: str, degree: int) -> str:
d = {'N': 0, 'NE': 45, 'E': 90, 'SE': 135, 'S': 180, 'SW': 225, 'W': 270, 'NW': 215}
degree = degree % 360
r = (d.get(direction) + degree) % 360
for k, v in d.items():
if v == r:
return k
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int)-> str:
directions=['N','NE','E','SE','S','SW','W','NW']
curr_index = directions.index(direction)
steps = degree//45
return directions[(steps+ curr_index)%8]
def solution(direction: str, degree: int)-> str:
# your code here
l = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']
# if degree > 360 or degree < -360:
# degree = degree % 360
# index = l.index(direction) + degree // 45
# if index > len(l) - 1:
# return l[index - len(l)]
# elif index <= len(l) - 1:
# return l[index]
# else:
# index = l.index(direction) + degree // 45
# if index > len(l) - 1:
# return l[index - len(l)]
# elif index <= len(l) - 1:
# return l[index]
# degree = degree % 360 if degree > 360 or degree < -360 else degree
# index = l.index(direction) + degree // 45
# return l[index] if index <= len(l) - 1 else l[index - len(l)]
index = degree//45
return l[(l.index(direction) + index) % len(l)]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
assert solution("W", 45) == "NW"```
只要取余数index就肯定是正的,所以不用考虑degree是正还是负
HLK
2022 年5 月 22 日 13:06
17
def solution(direction: str, degree: int)-> str:
list = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
index = list.index(direction)
degree_step = degree//45
if sum((index,degree_step)) < len(list):
return list[sum((index,degree_step))]
else:
step = degree_step % len(list)
return list[abs(len(list)-index-step)]
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"
def solution(direction: str, degree: int)-> str:
location = ['S','SW','W','NW','N','NE','E','SE']
after = int((degree/45 + location.index(direction)) % 8)
return location[after]
def solution(direction: str, degree: int)-> str:
location =['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW','WE']
if degree % 45 == 0:
degree = degree % 360 if degree > 360 or degree < -360 else degree
#location.index(direction)根据值判断索引
new_degree = int(location.index(direction))*45 + degree
new_diret_index = new_degree//45
new_diret = location[ int(new_diret_index % 8) if new_diret_index >= 8 else new_diret_index ]
print(new_diret)
else:
print("给定的转动角度不是45°的倍数")
solution('S',-30)
solution("SE", -45)
solution("W", 495)
def solution(direction: str, degree: int)-> str:
list=["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
a = 0
d = degree / 45
if direction in list:
a = int(list.index(direction) + d) % len(list)
return list[a]
def test_solution():
assert solution("S", 180) == "N"
assert solution("SE", -45) == "E"
assert solution("W", 495) == "NE"