4 ArrayList
4 ArrayList
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.
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
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
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
19 20 21
7
6 33
34
16 17 18
5 31
4
32
14 15
2 3 30
29
12 13
28
1
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
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
19
Here are several solutions: (1st complexity is
time, 2nd is amount of additional space
beyond A)
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