Exercise 1. Chapter 8.
Lab description:This program takes a string and returns it backwards.It was difficult to understand how to use this conditions for functions.
Code:
def letters_back(x): index=-1 length=(-1*len(x)) while index >= length: letter=x[index] print(letter) index = index-1 print(letters_back("fbs123456789"))
Screenshot:
Exercise 3. Chapter 10.
Lab description: The function cumulative _sum returns a new list that where the i-th element is the sum of first i+1 elements.It was not easy to figure out the return line.
Code:
def cumulative_sum(n): if len(n)<2: return n n[1]=n[0]+n[1] return [n[0]]+cumulative_sum(n[1:]) print (cumulative_sum([1,2,3,4,5,6,7,8,9]))
Screenshot: