Python高级模块化算术算法

我有这个程序:


#!/usr/bin/python3

def bounce(modulo: int, here: int, add: int) -> int:

    # additive version

    # modulo = 2n > 0, 0 <= here < n, add is any integer

    # cycle {

    #   {0, 1, 2, 3, ..., n - 1, n - 2, n - 3, ...,

    #    0, 1, 2, 3, ..., n - 1, n - 2, n - 3, ...},

    #   {0, 1, 2, 3, ..., n - 1, n - 2, n - 3, ...,

    #    0, 1, 2, 3, ..., n - 1, n - 2, n - 3, ...},

    #   ...

    # }

    tmp = abs(here + add) % (2 * modulo)

    if tmp <= modulo:

        tmp *= -1

        tmp -= 1

    tmp %= modulo

    return tmp



m = abs(int(input('Modulus: '))) + 1

i = abs(int(input('Iterate: '))) + 1


h: int = 0

m2: int = 3 * m


for x in range(1, 1 + i):

    print(h, end = ('\n' if x % m2 == 0 else ', '))

    h = bounce(m, h, 1)


input('Done.')

由于某种原因,该bounce()函数或其测试代码都没有工作。我不知道为什么。例如,如果我输入6模数和4迭代变量i,程序应该打印 5 行0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0. 相反,我会看到 2 行6, 0, 6, 0, .... 答案可能很简单。我已经对这个bounce()功能猛烈抨击了很多次,以至于我看不到它。add参数的标牌让我绊倒了。


跃然一笑
浏览 192回答 1
1回答

一只萌萌小番薯

您可能不需要生成整个序列,以下是封闭形式的实现,该形式计算并返回给定大小的序列中的索引,您希望以反复循环的方式迭代:您可以从任何时间点开始,包括过去(负时间),并获得您在序列(索引)中的位置,而无需迭代或构建整个序列。def bounce_back_and_forth(size: int, start_t: int=0)->int:&nbsp; &nbsp; """returns the index position in a sequence at time t=t, when the index starts&nbsp; &nbsp; at position zero at time t=0, and walks the list from end to end, and and bounces&nbsp; &nbsp; back and forth.&nbsp; &nbsp; returns the index for all t in Z&nbsp; &nbsp; @param size: int, the size of the sequence to iterate over&nbsp; &nbsp; @param start_t: int, the starting indice (usually time), zero by default&nbsp; &nbsp; :return: the index in the sequence corresponding to the start_t provided&nbsp; &nbsp; closed form, no iteration necessary --> O(1) time & space complexity&nbsp; &nbsp; &nbsp; &nbsp;size=5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size=5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size=5&nbsp; &nbsp; &nbsp; &nbsp;start at t=0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;start at t=6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start at t=-1 and decreases&nbsp; &nbsp; &nbsp;0 [0, _, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;6 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp; &nbsp; -1 [_, _, _, _, 4]&nbsp; &nbsp; &nbsp;1 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;7 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; -2 [_, _, _, 3, _]&nbsp; &nbsp; &nbsp;2 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8 [0, _, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; -3 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp;3 [_, _, _, 3, _]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;9 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; -4 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp;4 [_, _, _, _, 4]&nbsp; &nbsp; &nbsp; &nbsp; 10 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp; &nbsp; -5 [0, _, _, _, _]&nbsp; &nbsp; &nbsp;5 [_, _, _, 3, _]&nbsp; &nbsp; &nbsp; &nbsp; 11 [_, _, _, 3, _]&nbsp; &nbsp; &nbsp; &nbsp; -6 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp;6 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp; &nbsp; 12 [_, _, _, _, 4]&nbsp; &nbsp; &nbsp; &nbsp; -7 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp;7 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; 13 [_, _, _, 3, _]&nbsp; &nbsp; &nbsp; &nbsp; -8 [_, _, _, 3, _]&nbsp; &nbsp; &nbsp;8 [0, _, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; 14 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp; &nbsp; -9 [_, _, _, _, 4]&nbsp; &nbsp; &nbsp;9 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp; 15 [_, 1, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp;-10 [_, _, _, 3, _]&nbsp; &nbsp; 10 [_, _, 2, _, _]&nbsp; &nbsp; &nbsp; &nbsp; 16 [0, _, _, _, _]&nbsp; &nbsp; &nbsp; &nbsp;-11 [_, _, 2, _, _]&nbsp; &nbsp; """&nbsp; &nbsp; go_and_back = size * 2 - 2&nbsp; &nbsp; if start_t < 0:&nbsp; &nbsp; &nbsp; &nbsp; start_t = size - abs(start_t) % go_and_back&nbsp; &nbsp; tdx_at_start_t = start_t % go_and_back&nbsp; &nbsp; if tdx_at_start_t >= size:&nbsp; &nbsp; &nbsp; &nbsp; tdx_at_start_t = go_and_back - tdx_at_start_t&nbsp; &nbsp; return tdx_at_start_tif __name__ == '__main__':&nbsp; &nbsp; # tests&nbsp; &nbsp; res = [0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1]&nbsp; &nbsp; for idx in range(18):&nbsp; &nbsp; &nbsp; &nbsp; actual, expected = bounce_back_and_forth(5, start_t=idx), res[idx]&nbsp; &nbsp; &nbsp; &nbsp; assert actual == expected&nbsp; &nbsp; stride = 0&nbsp; &nbsp; for _ in range(5):&nbsp; &nbsp; &nbsp; &nbsp; mod = 8&nbsp; # the size of the sequence to iterate over&nbsp; &nbsp; &nbsp; &nbsp; start = 0&nbsp; &nbsp; &nbsp; &nbsp; stride += 1&nbsp; &nbsp; &nbsp; &nbsp; print(f'size: {mod}, stride: {stride} -> ', end='')&nbsp; &nbsp; &nbsp; &nbsp; for tdx in range(start, 20, stride):&nbsp; # <-- get indices for 20 time steps ahead&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(bounce_back_and_forth(mod, tdx), end=' ')&nbsp; &nbsp; &nbsp; &nbsp; print()输出:size: 8, step: 1 -> 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 1 2 3 4 5&nbsp;size: 8, step: 2 -> 0 2 4 6 6 4 2 0 2 4&nbsp;size: 8, step: 3 -> 0 3 6 5 2 1 4&nbsp;size: 8, step: 4 -> 0 4 6 2 2&nbsp;size: 8, step: 5 -> 0 5 4 1&nbsp;测试:class TestBounceBackAndForth(unittest.TestCase):&nbsp; &nbsp; def test_size5_t0(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=0), 0)&nbsp; &nbsp; def test_size5_t3(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=3), 3)&nbsp; &nbsp; def test_size5_t4(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=4), 4)&nbsp; &nbsp; def test_size5_t5(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=5), 3)&nbsp; &nbsp; def test_size5_t6(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=6), 2)&nbsp; &nbsp; def test_size5_t7(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=7), 1)&nbsp; &nbsp; def test_size5_t8(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=8), 0)&nbsp; &nbsp; def test_size5_t9(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=9), 1)&nbsp; &nbsp; def test_size5_t10(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=10), 2)&nbsp; &nbsp; def test_size5_t11(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=11), 3)&nbsp; &nbsp; def test_size2_t0(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=0), 0)&nbsp; &nbsp; def test_size2_t1(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=1), 1)&nbsp; &nbsp; def test_size2_t2(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=2), 0)&nbsp; &nbsp; def test_size2_t3(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=3), 1)&nbsp; &nbsp; def test_size2_t4(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=4), 0)&nbsp; &nbsp; def test_size15_t7(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=15, start_t=7), 7)&nbsp; &nbsp; def test_size15_t8(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=15, start_t=97), 13)&nbsp; &nbsp; # --- Negative Indices ------------&nbsp; &nbsp; def test_size5_t_m1(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-1), 4)&nbsp; &nbsp; def test_size5_t_m2(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-2), 3)&nbsp; &nbsp; def test_size5_t_m3(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-3), 2)&nbsp; &nbsp; def test_size5_t_m4(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-4), 1)&nbsp; &nbsp; def test_size5_t_m5(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-5), 0)&nbsp; &nbsp; def test_size5_t_m6(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-6), 1)&nbsp; &nbsp; def test_size5_t_m7(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-7), 2)&nbsp; &nbsp; def test_size5_t_m8(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-8), 3)&nbsp; &nbsp; def test_size5_t_m9(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-9), 4)&nbsp; &nbsp; def test_size5_t_m10(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-10), 3)&nbsp; &nbsp; def test_size5_t_m11(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=5, start_t=-11), 2)&nbsp; &nbsp; def test_size2_t_m1(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=-1), 1)&nbsp; &nbsp; def test_size2_t_m2(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=-2), 0)&nbsp; &nbsp; def test_size2_t_m3(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=-3), 1)&nbsp; &nbsp; def test_size2_t_m4(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=2, start_t=-4), 0)&nbsp; &nbsp; def test_size15_t_m7(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=15, start_t=-7), 8)&nbsp; &nbsp; def test_size15_t_m8(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=15, start_t=-8), 7)&nbsp; &nbsp; def test_size15_t_m97(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(bounce_back_and_forth(size=15, start_t=-97), 2)if __name__ == '__main__':&nbsp; &nbsp; unittest.main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python