FIT1029: Tutorial 2 Solutions Semester 2, 2013: Task 1
FIT1029: Tutorial 2 Solutions Semester 2, 2013: Task 1
Semester 2, 2013
Task 1
Part 1
One possible solution:
0. 000
1. 100
2. 101
3. 111
4. 110
5. 010
6. 011
7. 001
The problem can be represented as a graph by assigning each bitstring to
a node, and adding edges between pairs of nodes that dier by one bit. Any
Hamiltonian path through the graph represents a solution to the problem.
(A Hamiltonian path in a graph G is a path that contains each vertex in G
exactly once.)
1
Part 2
Algorithm 1 Intersect(bitString1, bitString2)
1: Finds the bit string corresponding to the intersection of two subsets
2: input: bitString1 and bitString2, which are bitstrings representing sub-
sets of the same set
3: output: A bitstring, newBitString, representing the intersection of the
two subsets
4: newBitString [ ] // create a new empty list
5: i 0
6: while (i < length(bitString1)) do
7: if ((bitString1[i] == 1) AND (bitString2[i] == 1)) then
8: add(1, newBitString)
9: else
10: add(0, newBitString)
11: end if
12: i i + 1
13: end while
14: return newBitString
Note: You can also use bitString2 in the while loop condition since they
are of the same length as they are bit string representing subsets of the same
set.
Task 2
Selection Sort
The bold characters are the ones being swapped.
A,L,G,O,R,I,T,H,M (Start)
A,L,G,O,R,I,M,H,T
A,L,G,O,H,I,M,R,T
A,G,L,M,H,I,O,R,T
A,G,L,I,H,M,O,R,T
A,G,H,I,L,M,O,R,T (swap with itself)
A,G,H,I,L,M,O,R,T (swap with itself)
2
A,G,H,I,L,M,O,R,T (swap with itself)
A,G,H,I,L,M,O,R,T (swap with itself, End)
Algorithm 2 MaximumPosition(L[0..n-1])
1: Finds the index of the item with the maximum value
2: input: A list L[0..n-1] of real values
3: output: Index of the maximum item
4: index 0
5: j 0
6: while (j < n) do
7: if L[j] > L[index] then
8: index j
9: end if
10: j j + 1
11: end while
12: return index
Algorithm 3 SelectionSort(L[0..n-1])
1: Sorts a list into ascending order
2: input: A list L[0..n-1] of real values
3: output: A list sorted into ascending order
4: i n-1
5: while (i 0) do
6: index MaximumPosition(L[0..i])
7: tmp L[index]
8: L[index] L[i]
9: L[i] tmp
10: i i - 1
11: end while
Insertion Sort
The bold character is the one to put in the correct position
A,L,G,O,R,I,T,H,M (Start)
A,L,G,O,R,I,T,H,M
A,G,L,O,R,I,T,H,M
3
A,G,L,O,R,I,T,H,M
A,G,L,O,R,I,T,H,M
A,G,I,L,O,R,T,H,M
A,G,I,L,O,R,T,H,M
A,G,H,I,L,O,R,T,M
A,G,H,I,L,M,O,R,T (End)
Algorithm 4 InsertionSort(L[0..n-1])
1: Sorts a list into ascending order
2: input: A list L[0..n-1] of real values
3: output: A list sorted into ascending order
4: if (n == 0) or (n == 1) then
5: return L
6: end if
7: k 1
8: loop
9: Put L[k] in the correct position in L[0..k]
10: k k + 1
11: if (k == n) then
12: return L
13: end if
14: end loop
A more detailed version. . .
4
Algorithm 5 InsertionSort(L[0..n-1])
1: Sorts a list into ascending order
2: input: A list L[0..n-1] of real values
3: output: A list sorted into ascending order
4: i 1
5: while (i < n) do
6: j i
7: tmp L[j]
8: while tmp < L[j-1] AND j > 0 do
9: L[j] L[j-1]
10: j j-1
11: end while
12: L[j] tmp
13: i i + 1
14: end while
Task 3
Algorithm 6 Anagrams(W[0..n-1], theWord)
1: Find all the anagrams of a given word
2: input: A list W[0..n-1] of words and a given word, theWord
3: output: A list containing all the anagrams of theWord
4: sortedWord Sort(theWord) // sort the letters of theWord into alphabetical order
5: anagramList []
6: i 0
7: while (i < n) do
8: tmp Sort(W[i])
9: if (tmp == sortedWord) then
10: add(W[i], anagramList)
11: end if
12: i i + 1
13: end while
14: return anagramList
5
Task 4
Algorithm 7 Palindromes(L[0..n-1])
1: Determine whether or not a given word is a palindrome
2: input: A word represented by a list L[0..n-1] of letters
3: output: true or false
4: i 0
5: if (n == 0 OR n == 1) then
6: return true
7: end if
8: while (i < n/2) do
9: if (i = n-i-1) then
10: return false
11: end if
12: i i+1
13: end while
14: return true
An alternative approach would be to make a copy of the input word,
reverse the copy, then compare it to the original to see if it is identical.
Task 5
Algorithm 8 Mean(L[0..n-1])
1: Find the mean value of a list of numbers
2: input: A list L[0..n-1] of numbers
3: output: A single number representing the mean value of all numbers
in L
4: i 0
5: sum 0
6: while (i < n) do
7: sum sum + L[i]
8: i i + 1
9: end while
10: return sum/n
6
Algorithm 9 StandardDev(L[0..n-1])
1: Find the standard deviation of a list of numbers
2: input: A list L[0..n-1] of numbers
3: output: A single number representing the standard deviation of all
numbers in L
4: i 0
5: sumsq 0
6: while (i < n) do
7: sq (L[i] - Mean(L)) * (L[i] - Mean(L))
8: sumsq sumsq + sq
9: i i + 1
10: end while
11: stdev Sqrt(sumsq/(n-1))
12: return stdev
7