from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def sum_list_iter(num_ls):
    """Sum number list by for loop."""
    _sum = 0
    for num in num_ls:
        _sum += num
    return _sum


def sum_list_recur(num_ls):
    """Sum number list by recursion."""
    if len(num_ls) == 1:
        return num_ls[0]
    else:
        return num_ls[0] + sum_list_recur(num_ls[1:])


def main():
    import time

    num_ls = range(100)
    
    start_time = time.time()
    print('By iteration: {}'.format(sum_list_iter(num_ls)))
    print('Time: {}'.format(time.time() - start_time))

    start_time = time.time()
    print('By recursion: {}'.format(sum_list_recur(num_ls)))
    print('Time: {}'.format(time.time() - start_time))


if __name__ == '__main__':
    main()

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def sum_list_recur(a_list):
    """Sum list by recursion.

    Time complexity: O(n), where n is the list length.
    Space complexity: O(n).
    """
    if len(a_list) == 1:
        return a_list[0]
    else:
        return a_list[0] + sum_list_recur(a_list[1:])


def sum_list_dp(a_list):
    """Sum list by bottom-up dynamic programming.

    Time complexity: O(n).
    Space complexity: O(1).
    """
    s = 0
    for x in a_list:
        s += x
    return s


def main():
    import time
    import random

    a_list = [random.randint(0, 1000) for _ in range(100)]

    start_time = time.time()
    print('By recursion: {}'.format(sum_list_recur(a_list)))
    print('Time: {}'.format(time.time() - start_time))

    
    start_time = time.time()
    print('By DP: {}'.format(sum_list_dp(a_list)))
    print('Time: {}'.format(time.time() - start_time))


if __name__ == '__main__':
    main()

Add time/space complexity; revise var's
Add time/space complexity; revise var's

  from __future__ import absolute_import
  from __future__ import print_function
  from __future__ import division
  
  
- def sum_list_iter(num_ls):
+ def sum_list_recur(a_list):
-     """Sum number list by for loop."""
-     _sum = 0
-     for num in num_ls:
-         _sum += num
-     return _sum
+     """Sum list by recursion.
+ 
+     Time complexity: O(n), where n is the list length.
+     Space complexity: O(n).
+     """
+     if len(a_list) == 1:
+         return a_list[0]
+     else:
+         return a_list[0] + sum_list_recur(a_list[1:])
  
  
- def sum_list_recur(num_ls):
-     """Sum number list by recursion."""
-     if len(num_ls) == 1:
-         return num_ls[0]
-     else:
-         return num_ls[0] + sum_list_recur(num_ls[1:])
+ def sum_list_dp(a_list):
+     """Sum list by bottom-up dynamic programming.
+ 
+     Time complexity: O(n).
+     Space complexity: O(1).
+     """
+     s = 0
+     for x in a_list:
+         s += x
+     return s
  
  
  def main():
      import time
+     import random
  
-     num_ls = range(100)
+     a_list = [random.randint(0, 1000) for _ in range(100)]
+ 
+     start_time = time.time()
+     print('By recursion: {}'.format(sum_list_recur(a_list)))
+     print('Time: {}'.format(time.time() - start_time))
+ 
      
      start_time = time.time()
-     print('By iteration: {}'.format(sum_list_iter(num_ls)))
+     print('By DP: {}'.format(sum_list_dp(a_list)))
-     print('Time: {}'.format(time.time() - start_time))
- 
-     start_time = time.time()
-     print('By recursion: {}'.format(sum_list_recur(num_ls)))
      print('Time: {}'.format(time.time() - start_time))
  
  
  if __name__ == '__main__':
      main()
  
Add time/space complexity; revise var's
## Code Before:
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def sum_list_iter(num_ls):
    """Sum number list by for loop."""
    _sum = 0
    for num in num_ls:
        _sum += num
    return _sum


def sum_list_recur(num_ls):
    """Sum number list by recursion."""
    if len(num_ls) == 1:
        return num_ls[0]
    else:
        return num_ls[0] + sum_list_recur(num_ls[1:])


def main():
    import time

    num_ls = range(100)
    
    start_time = time.time()
    print('By iteration: {}'.format(sum_list_iter(num_ls)))
    print('Time: {}'.format(time.time() - start_time))

    start_time = time.time()
    print('By recursion: {}'.format(sum_list_recur(num_ls)))
    print('Time: {}'.format(time.time() - start_time))


if __name__ == '__main__':
    main()

## Instruction:
Add time/space complexity; revise var's
## Code After:
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division


def sum_list_recur(a_list):
    """Sum list by recursion.

    Time complexity: O(n), where n is the list length.
    Space complexity: O(n).
    """
    if len(a_list) == 1:
        return a_list[0]
    else:
        return a_list[0] + sum_list_recur(a_list[1:])


def sum_list_dp(a_list):
    """Sum list by bottom-up dynamic programming.

    Time complexity: O(n).
    Space complexity: O(1).
    """
    s = 0
    for x in a_list:
        s += x
    return s


def main():
    import time
    import random

    a_list = [random.randint(0, 1000) for _ in range(100)]

    start_time = time.time()
    print('By recursion: {}'.format(sum_list_recur(a_list)))
    print('Time: {}'.format(time.time() - start_time))

    
    start_time = time.time()
    print('By DP: {}'.format(sum_list_dp(a_list)))
    print('Time: {}'.format(time.time() - start_time))


if __name__ == '__main__':
    main()
