题:
有一个机器人从位置 (0, 0)(原点)开始,位于 2D 平面上。给定它的一系列动作,判断这个机器人在完成它的动作后是否在 (0, 0) 处结束。
移动序列用一个字符串表示,字符moves[i]代表它的第i个移动。有效的移动是 R(右)、L(左)、U(上)和 D(下)。如果机器人在完成所有移动后返回原点,则返回 true。否则,返回false。
注意:机器人“面对”的方式无关紧要。"R" 将始终使机器人向右移动一次,"L" 将始终使其向左移动,等等。此外,假设机器人每次移动的移动幅度都相同。
输入:移动 = "UD" 输出:真 说明:机器人向上移动一次,然后向下移动一次。 所有移动都具有相同的幅度,因此它最终在它开始的原点处结束。 因此,我们返回true。
我有以下解决方案,对于序列 = 似乎是错误的"UD",它应该返回 True。有人可以帮助我了解我在这里做错了什么以及如何解决吗?
class Solution:
class Mover:
def __init__(self, x, y):
self.x, self.y = x, y
def new_pos(self, x, y):
return x + self.x, y + self.y
WALKS = dict(U=Mover(0, -1), D=Mover(0, 1),
L=Mover(-1, 0), R=Mover(1, 0))
def judge_circle(self, moves):
x = y = 0
for id in moves:
x, y = self.WALKS[id].new_pos(x, y) …Run Code Online (Sandbox Code Playgroud) 我已经编写了代码来从 .csv 文件创建一个秘密圣诞老人列表,其中包含一组名称和相应的电子邮件。
import random
import pandas as pd
def compare(list_of_names, list_of_emails):
zipped_lists = list(zip(list_of_emails, list_of_names))
random.shuffle(zipped_lists) # shuffle list of emails and names
result = []
shuffled_emails = [i[0] for i in zipped_lists]
for i, _ in enumerate(shuffled_emails):
result.append(zipped_lists[i-1][1]) # shift email relatively one position to the right
return list(zip(result, shuffled_emails))
def main():
names = ["John", "Bob", "Alice"]
emails = ["John@gmail.com", "Bob@gmail.com", "Alice@outlook.com"]
print(compare(names,emails))
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我如何对这段代码进行单元测试,因为我使用的是随机洗牌操作?我不确定如何为相同的测试用例编写测试用例。