Unit tests are some pieces of code to exercise the input, the output and the behaviour of any code.
Recursion :
What is Recursion? : The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
What is base condition in recursion? : In the recursive program, the solution to the base case is provided and the solution of the bigger problem is expressed in terms of smaller problems.
What are the disadvantages of recursive programming over iterative programming? : both recursive and iterative programs have the same problem-solving powers, i.e., every recursive program can be written iteratively and vice versa is also true. The recursive program has greater space requirements than iterative program as all functions will remain in the stack until the base case is reached. It also has greater time requirements because of function calls and returns overhead.
Python Lists
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets
List Methods
list.append(elem) –> adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
list.insert(index, elem) –>inserts the element at the given index, shifting elements to the right.
list.extend(list2) –>adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
list.index(elem) –> searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use “in” to check without a ValueError).
list.remove(elem) –> searches for the first instance of the given element and removes it (throws ValueError if not present)
list.sort() – >sorts the list in place (does not return it). (The sorted() function shown later is preferred.)
list.reverse() –> reverses the list in place (does not return it)
list.pop(index) – >removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).
Python Strings
Python has a built-in string class named “str” with many handy features (there is an older module named “string” which you should not use). String literals can be enclosed by either double or single quotes, although single quotes are more commonly used.
String Methods
s.lower(), s.upper() – returns the lowercase or uppercase version of the string
s.strip() – returns a string with whitespace removed from the start and end
s.isalpha()/s.isdigit()/s.isspace()… – tests if all the string chars are in the various character classes
s.startswith(‘other’), s.endswith(‘other’) – tests if the string starts or ends with the given other string
s.find(‘other’) – searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not found
s.replace(‘old’, ‘new’) – returns a string where all occurrences of ‘old’ have been replaced by ‘new’
s.split(‘delim’) – returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it’s just text. ‘aaa,bbb,ccc’.split(‘,’) -> [‘aaa’, ‘bbb’, ‘ccc’]. As a convenient special case s.split() (with no arguments) splits on all whitespace chars.
s.join(list) – opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. ‘—‘.join([‘aaa’, ‘bbb’, ‘ccc’]) -> aaa—bbb—ccc
pytest: helps you write better programs
The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries.