###### tags: `Kiditech` # Task on big O notation: Chapter 1 ### 1.3 Task on big o What is the big O notation of the following function ? ``` 1. Indicate constant time complexity in terms of Big-O notation A. O(n) B. O(1) //answer C. O(logn) D. O(n^2) 2. Indicate exponential time complexity in terms of big-O notation? A. O(n) B. O(n^2) //answer C. O(2^n) D. O(logn) 3. What is the big o for the following function ? list = [2,6,3,8,4,13,4] size = 7 def print_all_combinations(list,size) for x in range(0,size): for i in range(0, size): print(x * i) A. O(n) B. O(n^2) //answer C. O(2^n) D. O(logn) 4. What is the big o for the following function ? def double(n) return (n*2) A. O(1) //answer B. O(n^2) C. O(2^n) D. O(logn) 5. What is the big o for the following al·go·rithm? for i in range( 1 , 20 ): print(i) A. O(n) //answer B. O(1) C. O(logn) D. O(n^2) ``` (task description): Here is a function call printLast() which take in a list as argument. This function will loop through the whole list and print out the last number from the list. The big o notation for this function right now is o(n) since the runtime is base on the size of the list. this is a bad designed, try to make it perform big o(1). ``` def printLast(list): last_index = len(list)-1 size = len(list) for i in range(0,size): if i == last_index: print(list[i]) list = [2,5,1,8,9] printLast(list) ``` (hint): the last_index represents the last index of the element , we can just retun it. (answer): ``` def printLast(list): last_index = len(list)-1 print( list[last_index] ) ```