You are on page 1of 2

Jordan Cook-Irwin

25132458

FIT2004 Prac 5

Question 1:
1. String1 and String2 are the two strings we are comparing to find the
longest common subsequence (LCS)
2. Create a list of length (String1), named List1
3. Create a list of length (String2) for each element of List1. (thus creating a
grid of len(String1) by len(String2))
4. Fill the first column and the first row with zeroes.
5. For each position (x, y) in the grid, put the value of (match(x, y) + z)
where match(x, y) returns 1 if String1[x] == String2[y] and 0 if String1[x] !
= String2[y], and z is equal to position (x-1,y-1).
6. The value in the bottom right position is the length of the LCS for String1
and String2
Getting LCS from grid created above:
1. Start at the bottom right cell. Create a list name AnswerList. While not at
the top left cell, repeat steps 2-5. Once at the top left cell, go to step 6.
2. If the cell above has the same value as the current cell, move up. Repeat.
3. If not, but the cell to the left has the same value, move left. Go back to 2.
4. If neither 2 nor 3 are satisfied, append the corresponding letter to
AnswerList. (For any cell(x, y) the corresponding letter will be String1(x) or
String2(y)). Continue to 5.
5. Move up and to the left. Go back to 2 unless.
6. Reverse AnswerList and you have the LCS.

Question 2:
1.
2.
3.
4.

AList is the rotated ordered list of unique integers.


L = 0, M = len(AList)//2. R = len(AList) 1
Until the largest integer is returned repeat the following:
If AList[L] is greater than AList[M], R = M and M = midway between L and
current M
5. Else if A[L] is less than AList[M], L = M and M = midway between current M
and R
6. Else, if AList[L] > AList[R]: return AList[R]
7. Else, return AList[L]
This will have a time complexity of O(logn) as it is an altered form of binary
search.

Question 3:
Psuedo code:
1. Create a list called stop_list
2. Create a variable called last_stop. Set it to zero.
3. For every possible stop, i
If (stop(i)-last_stop) > m (m being the number of miles able to be
travelled on a full tank of gas)
Add stop(i-1) to stop_list
Set last_stop to stop(i-1)
4. The list of optimal stops is now stored in stop_list
The time complexity of the algorithm is O(n) as it goes through each stop once.

Jordan Cook-Irwin

Question 4:
See NQueens.py

25132458

FIT2004 Prac 5

You might also like