PE2 Review
Important Functions
Array
Define a Fixed length 2-D array
long dir[8][2] = {{1, -1}, {1, 1}, {-1, 1}, {-1, -1}, {0, -1}, {0, 1}, {1, 0}, {-1, 0}};Numbers
is_prime()
Return whether a number is a prime or not. Time complexity is .
bool is_prime(long num)
{
for (long i = 2; i * i <= num; i += 1)
{
if (num % i == 0)
{
return false;
}
}
return true;
}count_factors()
Return the number of prime factors of a num. Time complexity is
Strings
is_match()
char *word is your source string, size_t i is the position in word you want to start searching for. char *search_for is the string that you want to search for in word.
substitute()
An intermediate function that replace the target string with the replace string plus white spaces.
compact()
An intermediate function that takes in the source string word and reform the string by ignoring the white space in it.
replace()
A complete function that uses is_match(), substitute() and compact() above to achieve the purpose that after the execution of the function, all the serach_for substrings that have appeared in the source string will be replaced with the replace_with string.
My Solution for 5. Substring*
Binary Search
The classic one:
Binary Search for the position
Instead of returning -1 if the query q is not found, modify the binary search algorithm such that it returns either:
a position
k, such thatlist[k] <= q <= list[k+1].-1ifq < list[0]n - 1ifq > list[n-1]
Binary Search in a Rotated Sorted List
Appeared in 3. Rotate*
Where, within_range() is implemented as follows;
Binary Search to get the length of "#"
Recursive Method
Iterative Method
Counting Sort
This is the only sorting algorithm that has the chance to take to sort a list.
Selection Sort
Time complexity is
The soul of this algorithm is to 1) partition the list into two parts, an unsorted part followed by a sorted part, 2) repeatedly selects the maximum element from the unsorted part and move it to the front of the sorted part. (For increasing sort).
Otherwise, for decreasing sort, the idea will be to find the smallest unsorted element and add it to the end of the sorted list.
Insertion Sort
Similar to Selection Sort*, now we partition our list into 1) a sorted one, 2) followed by an unsorted one. Then we repeatedly take the first element from the unsorted partition, find its rightful place in the sorted partition, and insert it into place. We start with a sorted partition of one element, and we end if the sorted partition contains all the elements.
Improve Insertion Sort using Binary Search
Take the first element X from the unsorted partition
use binary search to find the correct position to insert X
insert X into the right place
Advanced Recursion
Permutation
NQueens - Find all solutions
NQueens - Find one solution quicker
Standard C Libaray
ctype.h
isalpha(char c)
check whether c is alphabetic or not
isdigit(char c)
check whether c is a decimal digit or not
islower(char c)
check whether c is a lowercase letter
ispunct(char c)
check whether c is a punctuation or not
isupper(char c)
check whether c is an uppercase letter
tolower(char c)
convert uppercase letters to lowercase
toupper(char c)
convert lowercase letters to uppercase
Punctuation letters: This is a set of ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
Tips
When doing string traversal, it is strongly recommended to use
strlen()to get the length of the string, and then use a for loop to traverse through thes string, with the loop variableiindicating the current position of the character we have traversed!Refer back to Lab 09 - Backtracking when doing the backtracking problems! Usually, an easier way to help you understand is to draw a "tree".
The 3. Group* provides an awesome idea to use array to store the previous states when doing backtracking!
When writing recursion in 2-D array, always think about how would you not enter an "infinite loop". This is usually down in your condition to enter the recursion. Some common methods are:
mark the attendance of the "cell" you have updated. (a.k.a build an
unvisited_array)there is some initial "constant" that you can compare to
When doing Backtracking/Recursion problems, always think about the three statements, they will help you form the solution: a)Current Stage, b)Terminating Condition, c)State Transitions. Anyway, the whole idea is to think about how to do the backtracking! (State Transitions). This is usually done by:
either redo the step you have done
write a new value to the step you have done
Somtimes changing the loop condition will change the obvious time complexity, pay attention to the "hidden" time complexity and sometimes this kind of change can be utilised to make your code more effificient. See 2. Sun
When you want to improve the complexity of searching/sorting problems,
try thinking about how to narrow your "searching area". This will be very useful!
think about find the pattern, this will affect your algorithm greatly. See Exercise 6 - Searching and Sorting
The
strlen()from C standard lib<string.h>will return the number of characters in a string (excluding the terminating'\0').Whenever you see the Time complexity is , try to think of binary search.
When implementing the search algorithm, always keep in mind:
How to define our search range (The use of index is inclusive or not)
When to stop our searching (How to decide the search is successful or not)
In the classic Binary Search, our search range is all the number bewteen
list[i]andlist[j](both inclusive). And there are two cases we should stop our searching: 1) when there is no element in our search range, 2) whenlist[mid]is what we want to find.
Valuable Problems from Past Year
Below are the valueable problems I think are important or classic
Last updated