Skip to document

Midtermcheatsheet - Mid term cheat sheet

Mid term cheat sheet
Course

Programming Methodology (CS1010S)

309 Documents
Students shared 309 documents in this course
Academic year: 2021/2022
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
National University of Singapore

Comments

Please sign in or register to post comments.

Preview text

Useful Exam Phrases for Complexity Questions

Iterative <the loop will iterate some <n> number of times, and each iteration it will evaluate <function> which takes <n> time= Recursive <there are <n> recursive calls, and each recursive call will evaluate <function> which takes <n> time=

Normal Loops <& where n is length of input string/<input>/&= Iterative Time: O(n), the loop will iterate n times Space: O(1), no extra memory is needed because the variables are overwritten with the new values. Recursive Time: O(n), there is a total of n recursive calls Space: O(n), there is a total of n recursive calls, each call will take up space on the stack

Looping through String/Tuple + Slicing/Concatenating Iterative Time: O ( n 2 ) where n is the length on the input string. Loop iterates n times, and in every loop iteration, string of up to length n is concatenated. Space: O ( n ), because even though there are no delayed operations or new objects being created every iteration, string of up to length n is created. Recursive Time: O ( n 2 ) where n is the length on the input string. String slicing at every of the n recursions. Space: O ( n

2

) where n is the length on the input string. String slicing at every of the n recursions takes up space in the stack.

Log(n) Iterative Time: O(n), no. of iterations is number of recursive calls is the number of times n divides by <5> until 0. Space: O(1), no extra memory is needed because the variables are overwritten with the new values. Recursive Time: O(log n), number of recursive calls is the number of times n divides by <5> until 0. Space:O(log n), as there are logn recursive calls, and each call creates a new frame on the stack. Recursive Branching # recursive example def f(n): if n == 0: # has some base case else: return f(n-1) + 2*f(n-2) + 3 * f(n-3)

space: O(n) because height of tree is ntime: O(3n) because there are three leaves

branching out from each <node=, leading to 3n leaves at the bottom of the tree

Accumulate and Fold accumulate(op, initial, sequence) combines the first element towards the right of the sequence. Returns 𝗅(𝗆𝗅𝗅[ 0 ])· (𝗅(𝗆𝗅𝗅[ 1 ]· & · 𝗅𝗅𝗅𝗆𝗅𝗄𝗅) fold is similar, except the inputs are integers 𝗅(𝗅)· 𐀀𝗅(𝗅 2 1)· &𝗅( 1 )· 𝗅( 0 )

String Comparisons Small letters are greater than capitals while letters occurring later are bigger. 8a9 > 8A9 and 8b9 > 8a

String/Tuple Indexing s[start(inclusive):stop(non-inclusive):step] s = <= s[0] -> IndexError s[0:] -> <= s[12345:] -> <=

Obscuring / Hiding If there is a need to obscure the data type9s content, wrap the variable you want to hide in a lambda function to obscure it

def f(secret): return lambda: secret

Not possible to resolve without breaking data abstraction as <item> is hidden behind a function, no way to obtain <item> using the provided functions

Recursively calling helper function

Was this document helpful?

Midtermcheatsheet - Mid term cheat sheet

Course: Programming Methodology (CS1010S)

309 Documents
Students shared 309 documents in this course
Was this document helpful?