This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borrow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = accumulate(cur)
return list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borrow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = accumulate(cur)
return list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borrow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = accumulate(cur)
return list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borowborrow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = accumulate(cur)
return list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = accumulate(cur)
return list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borrow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = accumulate(cur)
return list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = list(accumulate(cur))
return cur[list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = list(accumulate(cur))
return cur[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)
This
for u in range(1, ups+1):
cur[u] = cur[u] + cur[u-1]
is just an accumulation loop. Python 3 has the itertools.accumulate
function to perform it efficiently, but you can borow the code from there if you want to stay with Python 2: it will name things and make the code more readable:
def accumulate(iterable):
"""Return running totals"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = total + element
yield total
def grid_move_v2(rights, ups):
cur = [1] * (ups + 1)
for _ in range(rights):
cur = accumulate(cur)
return list(cur)[-1]
if __name__ == "__main__":
print grid_move_v2(2,3)
print grid_move_v2(4,2)
(I also used the improvement proposed by @Graipher)