0% found this document useful (0 votes)
9 views

4 ArrayList

Uploaded by

Arhaan Khaku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

4 ArrayList

Uploaded by

Arhaan Khaku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

COSC 222 Data Structures

Yves Lucet

CC BY-SA 3.0
https://en.wikipedia.org/wiki/Skip_list#/media/File:Skip_list_add_element-en.gif
1
"Hash table 3 1 1 0 1 0 0 SP" by Jorge Stolfi - Own work. Licensed under CC BY-SA 3.0 via Commons -
https://commons.wikimedia.org/wiki/File:Hash_table_3_1_1_0_1_0_0_SP.svg#/media/File:Hash_table_3_1_1_0_1_0_0_SP.svg
Menu Today
• Teams tuned:
– 2 emailed to log into dashboard Image courtesy of
Grant Cochrane/
FreeDigitalPhotos.net

– 1 duplicate in dashboard
• RAT postmortem
– All marks posted
– Contact me asap if you disagree with your
mark
• Exercises: tMAT practice

2
TA contact information
• Drop by any lab information
• Post in Ms Teams
• TAs are paid hourly and not expected to
have zoom meetings during
weekends/holidays

3
REMINDER: TESTING

4
Unit Testing
1. Select your method signature: int Fact(int n)
2. Design your tests: Fact(0)==1; Fact(1)==1; Fact(3)==6; Fact(-1) error;
Fact(10.5) error
3. Write your code for Fact(n)
4. Write your tests; run tests; fix till all tests pass

Depending on your task, you may or may not implement exceptions, e.g. you could
forget about Fact(-1) in a 1-week lab and just put a comment in the code that no
negative integers are expected.

Key principle: All the tests should


• run quickly (in 1-5s with 10s max) or you won’t run them each time you modify
the code
• be automated. No user input whatsoever!
• be repeatable. No random function unless you fix the seed

5
After every code change, run ALL the tests. This checks your change did not
Unit Tests are
 reproducible
 quick: running all tests should not take
more than 5 seconds. The longer it takes,
the less often you run them.
 fully automated: all tests should run with a
single click with no other user input
needed whatsoever.
 run every time the code is modified
6
% coverage
Coverage Testing
Coding time
• Unit testing assesses whether your code works
as expected
• Coverage testing assesses whether your code
has the bare minimum of tests
– Line/statement coverage: after running all your
unit tests, each line/statement should be executed
once. This is the weakest metric
– Other coverage metrics:
• Function coverage: call each function
• branch coverage: execute both the then and the else
clauses for if statements
• Condition coverage: check each Boolean evaluated to 7
Wk Class Date Activity Reading/Prep given Peer Lab
1 1 Sep 06 Syllabus, TBL, Java review NO LAB
2 Sep 08 Git, testing Java, Generics, Testing
2 3 Sep 13 RAT1: generics; unit testing Complexity 1 unit testing
4 Sep 15 Lect: Complexity, streams Lists
3 5 Sep 20 Build & Critic (training) iMAT1 2 coverage testing
6 Sep 22 tMAT1 Recursion
4 7 Sep 27 RAT2 Stack, Queue P eer 1 3 streams
8 Sep 29 Build & Critic Iterators
5 9 Oct 04 mini-lecture+exercises iMAT2 4 simulation
10 Oct 06 tMAT2 BST, PQ, heap Holiday: Mon Oct 2
6 11 Oct 11 RAT3 Hash, skip list P eer 2
12 Oct 13 Hash table, Skiplist, bottom-up
14.6heap
Shortest
construction
path Holiday: Mon Oct 9

7 13 Oct 18 Dijsktra+adaptable PQ Union-find 5 hashing


14 Oct 20 Union-find/Disjoint sets iMAT3
8 15 Oct 25 tMAT3 Search Trees AVL/RB 6 connected
16 Oct 27 Lecture BST, AVL, (2,4), RB B-Trees P eer 3
9 17 Nov 01 B-trees iMAT4
18 Nov 03 tMAT4
10 19 Nov 08 Midterm review NO LAB
20 Nov 10 Midterm
11 Nov 15 Reading week
Nov 17 Reading week Text processing
12 21 Nov 22 Pattern matching KMP, BM, Trie 7 matching
22 Nov 24 RAT4 Huffman coding
13 23 Nov 29 Huffman coding iMAT5 8 Trie vs. BST vs. Hash tabl
24 Dec 01 tMAT5 8
14 25 Dec 06 Review/Course Evaluation P eer 4 NO LAB
Module 1: Labs

1. Unit testing, comparing arrays, sorting


2. Coverage testing, counting sort
– [unit/coverage testing expected from then on]
3. Generics
4. Streams

9
Feedback
• Labs have imprecise formulation or are ambiguous
– By design! It makes you more independent and prepare
you to implement software instead of writing code. Yes it
is less comfortable but software implementation is all
about taking plenty of tiny decisions to deal with
ambiguity.
• You are no longer taught in class before a lab
– Focus is on learning not teaching. Professor role is no
longer to pre-digest material but to guide you through
various material sources
– Asking questions engages you so you relate the material
with your previous knowledge resulting in much more
efficient learning 10
– Learning how to find answers online is a critical skill
Readings for today
• See Readings page on Canvas
• Topic: Arrays, Lists, cloning
• Concepts: singly linked list, doubly linked
list, circular linked list, dynamic array,
deep/shallow/lazy copy
• Java:
– copy constructor, cloning, serialization, loop,
stream
– Arraycopy, addAll, copyOfRange, copyOf
11
tMAT

Build & Critic

12
Team build & critic
Do not forget to assign a TIMEKEEPER
1. [10 marks] Answer the questions on your
30’ poster
1. write clearly
2. Take picture and post it on Canvas as group
assignment tMAT0 - Answer

4’2. [4 marks] Rotate posters on each aisle (left, centre, right); do NOT cross
Per aisle. Critic 4 posters.
poster Make a list of any error, bug, omission; is complexity correct?
The full team has to agree on the justification
Upload your poster to tMAT0 - Critics

Be quick! We have to start even if you haven’t finished writing your


poster!

Include the list of team members present today


On your poster
13
25 26 27
10 11 37
38
22 23 24
8 9 35 36

19 20 21
7
6 33
34
16 17 18
5 31
4
32
14 15
2 3 30
29
12 13
28
1

Do NOT cross aisle


14
Log to Canvas or Ms Team to know your team
Pseudo code: Binary search
algorithm int bin(A,x)
Input: A: sorted array of n distinct numbers; x: number
to find in the array
Output: i if A[i]==x or -1 (not found)
Return bin1(A,1,n,x)

Function int bin1(A, i, j, x)


1. If (j-i)<2 then return (A[i]==x)?i:-1;
2. eIse if A[(i+j)/2] < x then return bin1(A, (i+j)/2,j,x)
else return bin1(A,i,(i+j)/2,x)

15
Complexity: runtime O(log n); space O(n)
tMAT

tMAT0 – Answer
tMAT0 - Critics

16
Let A be an array of size containing integers
from 1 to n-1 inclusive, one of which is
repeated, e.g. A=[4, 2, 1, 3, 2]
Describe an efficient algorithm for finding the
integer in A that is repeated

1. Write the pseudo-code explaining any data


structure you use
2. Write the worst-case time complexity
3. Write the worst-case space complexity of
any additional memory beyond the space for
array
17
Team build & critic
Do not forget to assign a TIMEKEEPER
1. [10 marks] Answer the questions on your
30’ poster
1. write clearly
2. Take picture and post it on Canvas as group
assignment tMAT0

3’2. [4 marks] Rotate posters on each aisle (left, centre, right); do NOT cross
Per aisle. Critic 4 posters.
poster Make a list of any error, bug, omission; is complexity correct?
The full team has to agree on the justification
Upload your poster to tMAT0

Be quick! We have to start even if you haven’t finished writing your


poster!

Include the list of team members present today


On your poster
18
Solution

19
Here are several solutions: (1st complexity is
time, 2nd is amount of additional space
beyond A)

1. Sort A; loop till you find the repeated


consecutive integer.
2. Sort A using counting sort; loop till you
find the repeated consecutive integer. ,
3. Sum all integers in A in variable s; return .
,

20
Detailed solution
• Find the Duplicate Number is fully explained at
https://leetcode.com/problems/find-the-duplicate
-number/
Sol # Time O(?) Space O(?) Description
1 n log n log n sort
2 n n set (counting sort)
3 n 1 negative marking
4 n n HashMap recursion
5 n 1 HashMap iterative
6 n log n 1 sum of set bits
7 n 1 Floyd’s Tortoise and Hare (cycle
detection)
21
Techniques
• Common:
– Sort, set (hashset/counting sort); binary
search
• Less common:
– Negative marking, HashMap
• Custom:
– Sum of set bits
– Floyd’s Tortoise and Hare

22
Next Time
• submit iMAT on Canvas
– DEADLINE: on Canvas
• be ready for tMAT
• Format: build & critic (same as today)

23

You might also like