Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1020 Data Structures and Algorithms I Lecture Note #14

Similar presentations


Presentation on theme: "CS1020 Data Structures and Algorithms I Lecture Note #14"— Presentation transcript:

1 CS1020 Data Structures and Algorithms I Lecture Note #14
Wrapping Up

2 [CS1020 Lecture 14: Wrapping Up]
Outline Past Years’ Exam Questions Exam Matters Announcements [CS1020 Lecture 14: Wrapping Up]

3 1 Past Years’ Exam Questions

4 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 We will discuss questions 1 to 8 here For the rest, please discuss on the IVLE forum Note that exam answers are not released Q1 ArrayList<Integer> arr = new ArrayList<Integer>(); for (int i=0; i<10; i++) arr.add(i); for (int i=0; i<arr.size()/2; i++) arr.add(i, arr.remove(arr.size()-1-i)); System.out.println(arr); (A) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (B) [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (C) [9, 7, 5, 3, 1, 0, 2, 4, 6, 8] (D) [9, 7, 5, 3, 1, 8, 6, 4, 2, 0] (E) [9, 0, 8, 1, 7, 2, 6, 3, 5, 4] [CS1020 Lecture 14: Wrapping Up]

5 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 Q2. Calling recurse(12); public static void recurse(int n) { System.out.println("The number is " + n); if (n-- > 0) { recurse(n--); } (A) 5 (B) 6 (C) 7 (D) 12 (E) None of the above. [CS1020 Lecture 14: Wrapping Up]

6 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 Q3. How many integers in s can also be found in q? int[] array = new int[] {1,2,3,4,5}; Stack<Integer> s = new Stack<Integer>(); for (int i: array) { s.push(i); if (i%2 == 0) s.pop(); } Queue<Integer> q = new LinkedList<Integer>(); q.offer(i); if (i%2 == 0) q.poll(); (A) 0 (B) 1 (C) 2 (D) 3 (E) 4 [CS1020 Lecture 14: Wrapping Up]

7 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 Q4. Result of first partitioning: 2, 5, 3, 7, 9, 15, 20, 18, 25, 30, 10 How many possible integers among the elements could be the pivot? (A) 3 (B) 4 (C) 5 (D) 6 (E) None of the above. [CS1020 Lecture 14: Wrapping Up]

8 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 Q5. Shift elements in an n×n matrix by a shift amount. Shift 3 12 11 10 9 8 7 6 5 4 3 2 1 arr newArr int[][] newArr = new int[arr.length][arr[0].length]; int index, numRows = arr.length, numCols = arr[0].length; for (int r = 0; r < numRows; r++) { for (int c = 0; c < numCols; c++) { index = newArr[index/numCols][index%numCols] = arr[r][c]; } (A) (r*numCols + c + shiftAmt); (B) (r*numCols + c + shiftAmt) % numRows; (C) (r*numCols + c + shiftAmt) % numCols; (D) (r*numCols + c + shiftAmt) % (numRows*numCols); (E) None of the above. [CS1020 Lecture 14: Wrapping Up]

9 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 Q6. Which of the following statements is true? (A) Quadratic probing may avoid the clustering problem completely. (B) A perfect hash function must distribute the keys evenly in the hash table. (C) It is effective (i.e. there are fewer collisions) to use direct addressing table when the keys are not dense. The probe sequence of modified linear probing covers all positions in a hash table. (E) None of the above. [CS1020 Lecture 14: Wrapping Up]

10 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 Q7. searchAndSwitch(E item) If item found not in the last node, return true and switch node containing item with its next node; If item not found or item found is last node, return false. Example: [Abe, Gigi, Flora, Andy, Jack, Zoe] After calling searchAndSwich(“Andy”): [Abe, Gigi, Flora, Jack, Andy, Zoe] General case: curr prev next Special case: node found is first node of list curr next head [CS1020 Lecture 14: Wrapping Up]

11 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 General case: curr prev next public boolean searchAndSwitch(E item) { ListNode <E> curr = head; ListNode <E> prev = null; while (curr != null && !curr.getElement().equals(item)) { prev = curr; curr = curr.getNext(); } return true; Special case: node found is first node of list curr next head [CS1020 Lecture 14: Wrapping Up]

12 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 Q8. triangulate() Triangulation: to split a convex polygon into a number of triangles Triangulate a MyPolygon object into an ArrayList of triangles v0 v1 v2 v3 v4 v0 v1 v2 v3 v4 v5 v6 v7 Let’s label the vertices of the given polygon v0 to vn-1, where n is the number of vertices, as shown in the diagrams above. [CS1020 Lecture 14: Wrapping Up]

13 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 import java.awt.Point; import java.util.ArrayList; class MyPolygon { private int size; // number of vertices private ArrayList<Point> vertices; public MyPolygon(int num, ArrayList<Point> pts) { size = num; vertices = new ArrayList<Point>(pts); } // Other methods not shown as they are not required // for this question [CS1020 Lecture 14: Wrapping Up]

14 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 v0 v1 v2 v3 v4 v0 v1 v2 v3 v4 v5 v6 v7 public ArrayList<MyPolygon> Triangulate() { ArrayList<MyPolygon> triangles; return triangles; } [CS1020 Lecture 14: Wrapping Up]

15 [CS1020 Lecture 14: Wrapping Up]
1 AY2013/4 Semester 2 v0 v1 v2 v3 v4 v0 v1 v2 v3 v4 v5 v6 v7 public ArrayList<MyPolygon> Triangulate() { ArrayList<MyPolygon> triangles; return triangles; } Alternative solution [CS1020 Lecture 14: Wrapping Up]

16 2 Exam Matters

17 [CS1020 Lecture 14: Wrapping Up]
2 CS1020 Objectives (1/2) Give an introduction to OO Programming (OOP) Model using Java programming language, linear data structures, and algorithms for constructing efficient programs. Emphasize on data abstraction issues (through ADTs) in the code development. Emphasize on efficient implementations of chosen data structures and algorithms. [CS1020 Lecture 14: Wrapping Up]

18 [CS1020 Lecture 14: Wrapping Up]
2 CS1020 Objectives (2/2) Linear data structures include arrays, lists, stacks, queues, and hash tables; together with their algorithms (insert, delete, find, and updates). Simple algorithmic paradigms, such as sorting and search algorithms, and divide-and-conquer algorithms were introduced. Recursion and elementary analysis of algorithms were taught. [CS1020 Lecture 14: Wrapping Up]

19 [CS1020 Lecture 14: Wrapping Up]
2 Final Exam (1/2) Date: 4 May 2015, Monday Time: 5 – 7pm Venue: To be announced by Registrar’s Office Weightage: 40% (see for details) Scope: Everything covered in lectures, tutorials and labs  30%: Materials up to and including Exceptions (tested in mid-term)  70%: Materials after Exceptions [CS1020 Lecture 14: Wrapping Up]

20 [CS1020 Lecture 14: Wrapping Up]
2 Final Exam (2/2) Format: Section A (18 marks): 6 MCQs Section B (62 marks): 7 questions Closed book. You are allowed to bring in one A4 handwritten reference sheet. Calculators NOT allowed. You may use pencil to write your answers. Read through all questions first before answering them. Refer to CS1020 website, “Exams” page: [CS1020 Lecture 14: Wrapping Up]

21 3 Announcements

22 [CS1020 Lecture 14: Wrapping Up]
3 Announcements Our consultations hours are given on the IVLE announcement Please check out the IVLE as we will still be making announcements We will post your CA marks for your checking Please check your CA marks and report any discrepancy before the stated deadline [CS1020 Lecture 14: Wrapping Up]

23 [CS1020 Lecture 14: Wrapping Up]

24 End of file


Download ppt "CS1020 Data Structures and Algorithms I Lecture Note #14"

Similar presentations


Ads by Google