0% found this document useful (0 votes)
37 views84 pages

INF1339ComputationalThinking Week6 Slides LEC

INF1339ComputationalThinking-Week6-Slides-LEC MI, University of Toronto

Uploaded by

蕭可榆
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views84 pages

INF1339ComputationalThinking Week6 Slides LEC

INF1339ComputationalThinking-Week6-Slides-LEC MI, University of Toronto

Uploaded by

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

INF1339 Introduction to

Computational Thinking
WEEK 6 OCTOBER 8 AND 9, 2024 – LECTURE

1
For thousands of years the University of Toronto has been the
traditional land of the Huron-Wendat, the Seneca, and the
Mississaugas of the Credit. Today, this meeting place is still the
home to many Indigenous people from across Turtle Island and we
are grateful to have the opportunity to work on this land.

For more information, see the Final Report of the Steering Committee for the University of Toronto
Response to the Truth and Reconciliation Commission of Canada and the Faculty of Information’s
Commitment to the Findings and Call for Action of the Truth and Reconciliation Commission.

2
Agenda
Objectives
Data structures
Books are Us data structures
Text processing and regular expressions
Break

3
Agenda
Objectives
Data structures
Books are Us data structures
Text processing and regular expressions
Break

4
Objectives (from Riley & Hunt Ch 7)
To understand the importance of properly naming items
To understand how a computing system organizes data in memory
To understand how lists, trees, and graphs can be stored in memory
To understand how lists, trees, and graphs are correlated to familiar
concepts such as family trees, road maps, and organizational charts
To understand how indexing is used to organize data in memory
To understand how linking is used to organize data in memory

5
Objectives (from Riley & Hunt Ch 8)
To understand the von Neumann principle
To understand the concept of two-dimensional data layout and
tabular information retrieval
To understand the relationship of discrete functions and tables
To understand how formulas can be used for textual processing
To understand how formula can be used to define and process
patterns

6
Agenda
Objectives
Data structures
Books are Us data structures
Text processing and regular expressions
Break

7
Data Structures: Importance of Naming
Guidelines help to ensure that two different people or
computing systems can identify, locate, and reason about
data without confusion:
◦ Names should be unique
◦ One item should not have more than one name
◦ A name should be descriptive
◦ The name should be related to the location (think www)

8
Week 6 Quiz
1. Naming files word files in the following way abcde.docx, xyzwv.docx, and
dfghi.docx violates which of the following guidelines for properly name items
in a computational setting:

a) Names should be unique


b) One item should not have more than one name
c) A name should be descriptive
d) The name of an item should be related to the location of the item
Week 6 Quiz
1. Naming files word files in the following way abcde.docx, xyzwv.docx, and
dfghi.docx violates which of the following guidelines for properly name items
in a computational setting:

a) Names should be unique


b) One item should not have more than one name
c) A name should be descriptive
d) The name of an item should be related to the location of the item
Data Structures
Lists
Arrays
Graphs
Stacks
Queues

11
Data Structures
Lists
Arrays
Graphs
Stacks
Queues

12
Linked Lists: Operations
Traversal
Searching
Length
Insertion:
◦ Insert at the beginning
◦ Insert at the end
◦ Insert at a specific position
Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-list-tutorial/
13
Linked Lists

https://www.geeksforgeeks.org/singly-linked-list-tutorial/

14
Linked Lists: Operations
Traversal
Initialize a pointer current to the head of the list.
Searching Use a while loop to iterate through the list until the
Length current pointer reaches NULL.
Insertion: Inside the loop, perform whatever operation is wanted on
◦ Insert at the beginning the current node and move the current pointer to the next
◦ Insert at the end node.
◦ Insert at a specific position
Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-list-tutorial/
15
Linked Lists: Operations
Traversal Searching
Searching 1. Traverse the Linked List starting from the head.
2. Check if the current node’s data matches the target
Length value.
Insertion: 3. If a match is found, return true.
◦ Insert at the beginning 4. Otherwise, move to the next node and repeat step 2.
◦ Insert at the end 5. If the end of the list is reached without finding a match,
◦ Insert at a specific position return false.

Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-list-tutorial/
16
Linked Lists: Operations
Traversal
Searching
How might you find length of a linked list?
Length
Insertion:
◦ Insert at the beginning
◦ Insert at the end
◦ Insert at a specific position
Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-list-tutorial/
17
Linked Lists: Operations
Traversal 1. Initialize a counter length to 0.
Searching 2. Start from the head of the list, assign it to current.
Length 3. Traverse the list:
Insertion: 1. Increment length for each node.
◦ Insert at the beginning 2. Move to the next node (current = current->next).
◦ Insert at the end
◦ Insert at a specific position
4. Return the final value of length.

Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-list-tutorial/
18
Linked Lists: Operations
Traversal 1. Create a new node with the given value.
Searching
2. Set the next pointer of the new node to
Length the current head.
Insertion:
◦ Insert at the beginning 3. Move the head to point to the new node.
◦ Insert at the end
◦ Insert at a specific position 4. Return the new head of the linked list.
Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-
list-tutorial/
19
Linked Lists: Operations
Traversal 1. Create a new node with the given value.
Searching 2. Check if the list is empty:
◦ If it is, make the new node the head and return.
Length
3. Traverse the list until the last node is reached.
Insertion:
◦ Insert at the beginning 4. Link the new node to the current last node by
◦ Insert at the end setting the last node’s next pointer to the new
◦ Insert at a specific position
node.

Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-
list-tutorial/
20
Linked Lists: Operations
Traversal 1. Traverse the list to the desired position
Searching
2. Link the new node to the next node
Length
Insertion:
3. Update the links accordingly
◦ Insert at the beginning
◦ Insert at the end
◦ Insert at a specific position
Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-
list-tutorial/
21
Linked Lists: Operations
Traversal
Searching
Length
Insertion:
◦ Insert at the beginning
◦ Insert at the end
◦ Insert at a specific position
Deletion:
◦ Delete from the beginning
◦ Delete from the end
◦ Delete a specific node

https://www.geeksforgeeks.org/singly-linked-
list-tutorial/
22
Data Structures
Lists
Arrays
Graphs
Stacks
Queues

23
Arrays

24
Arrays: Operations
Traversing
Accessing
Searching
Length
Insertion
Deletion

25
Arrays: Operations
Traversing
Accessing
Use a for loop
Searching For index  0 to array_length-1
Length Perform operation on array[index]
Insertion
Deletion

26
Arrays: Operations
Traversing
Accessing If we know the index we want, we can easily access it
Searching array[0] # first element
Length array[j] # jth element
array[n-1] # last element
Insertion
Deletion

27
Arrays: Operations
Traversing
If sorted, can use binary search:
Accessing Initialize i to middle index (n//2)
Searching Use a while loop checking
Length if array[i] == item then
done
Insertion
else if array[i] < item then
Deletion search the array from index 0 to index i-1
else
search the array from index i+1 to n-1

28
Arrays: Operations
Traversing
Accessing If NOT sorted, can use linear search:
Searching Use a for loop with i from 0 to n-1 checking
Length For i  0 to array_length-1
if array[i] == item then done
Insertion
Deletion

29
Arrays: Operations
Traversing
Accessing length == n
Searching indices go from 0 to n-1
Length
Insertion
Deletion

30
Arrays: Operations
Traversing Assuming there is space in the array or it can be extended:
Accessing
Searching
Length
Insertion
Deletion

https://www.geeksforgeeks.org/inserting-elements-in-an-array-array-operations/ 31
Arrays: Operations
Traversing Insert item X at position pos in array of length n that has
Accessing room for n+1 items:

Searching
for i  n – 1 down to pos
Length array[i + 1]  array[i]
Insertion
Deletion array[pos] = X;

https://www.geeksforgeeks.org/inserting-elements-in-an-array-array-operations/ 32
Arrays: Operations
Traversing
Accessing
Searching
Length
Insertion
Deletion

https://www.geeksforgeeks.org/search-insert-and-delete-in-an-unsorted-array/ 33
Arrays: Operations
Traversing Delete item at position pos in array of length n:
Accessing
for i  pos to n-1
Searching
array[i]  array[i+1]
Length
Insertion
Deletion

https://www.geeksforgeeks.org/inserting-elements-in-an-array-array-operations/ 34
Data Structures
Lists
Arrays
Graphs
Stacks
Queues

35
Graphs
Many real-world items can be represented as graphs and thus
stored, manipulated as graphs
Graph G has a set of vertices V and edges E: G=(V, E)
V={A, B, C, D, E}
E={(A,E), (A, B), (B, A), (B, D),
(C, E), (D, C), (E, B), (E, C),
(E, D)}

36
Week 6 Quiz
10. According to the textbook, which of the following real-world problems and
their solutions can be captured by graphs (select all that apply):
a) Computer networks such as the internet
b) A list of paintings
c) Games like checkers and chess
d) A university’s curriculum
Week 6 Quiz
10. According to the textbook, which of the following real-world problems and
their solutions can be captured by graphs (select all that apply):
a) Computer networks such as the internet
b) A list of paintings
c) Games like checkers and chess
d) A university’s curriculum
Graphs
Vertex A has an “out-degree” of 2 and an “in-degree” of 1
There is a “cycle” from B to D to C to E to B

39
Week 6 Quiz
6. Which of the following represents the directed graph G = (V, E), where V = {A,
B, C, D, E} and E = {(A, D), (B, E), (C, E), (D, A), (A, B), (E, A)}:

a) b) c) d)
Week 6 Quiz
6. Which of the following represents the directed graph G = (V, E), where V = {A,
B, C, D, E} and E = {(A, D), (B, E), (C, E), (D, A), (A, B), (E, A)}:

a) b) c) d)
Trees
A type of graph such that
◦ Exactly one vertex with in-degree zero (the “root” of the tree)
◦ Every vertex other than the root has an
in-degree of one
◦ There is a path from the root to every other
vertex
T, E, C, and A are leaf vertices
B is a parent of S and an ancestor of S and T
B, E, and N are children of R

42
Week 6 Quiz
3. Match the term with the picture that best illustrates it:

a) In-degree
b) Out-degree
c) Leaf
d) Cycle
Week 6 Quiz
3. Match the term with the picture that best illustrates it:

a) In-degree D
b) Out-degree C
c) Leaf A
d) Cycle B
Binary Trees
A special kind of tree where every vertex (except leaf vertices) have
at most 2 children (left child, right child)
What do you notice about each level of
the binary tree?

https://www.geeksforgeeks.org/binary-tree-data-structure/ 45
Binary Trees
What do you notice about each level of the binary tree?
◦ There is one vertex at the root (20)
◦ There are two at the next level (21)
◦ There are four at the next level (22)
◦ There could be eight at the next level (23)
Each level has 2x the number at
the previous level
A tree with N vertices has log2N levels

https://www.geeksforgeeks.org/binary-tree-data-structure/ 46
Binary Search Trees
How many steps will it take to search
for the number 14 in the tree?
How many steps will it take to search
for the number 16 in the tree?

47
Binary Search Trees
Used for organizing and storing data in a
sorted manner
Each vertex has at most two children,
a left child and a right child
The left child contains values less than
the parent vertex and the right child
containing values greater than the parent
vertex

48
Data Structures
Lists
Arrays
Graphs
Stacks
Queues

49
https://csanim.com/tutorials/queues-vs-stacks-brief-visual-explanation 50
Feature Stack Queue
A linear data structure that follows A linear data structure that follows
Definition the Last In First Out (LIFO) principle. the First In First Out (FIFO) principle.

Enqueue (add an item), Dequeue


Push (add an item), Pop (remove an
Primary Operations item), Peek (view the top item)
(remove an item), Front (view the first
item), Rear (view the last item)

Elements are added and removed from Elements are added at the rear and
Insertion/Removal the same end (the top). removed from the front.

Browser history (back button), Customer service lines, CPU task


Examples reversing a word. scheduling.

A stack of plates: you add and remove A queue at a ticket counter: the first
Real-World Analogy plates from the top. person in line is the first to be served.

https://www.geeksforgeeks.org/difference-between-stack-and-queue-data-structures/ 51
Week 6 Quiz
8. According to Anna Lytical, if you put your make up in a data structure in the
order you want to apply it and the first thing you put in the data structure is
the first think you apply (the first step you take), you are using the following
data structure:

a) An array
b) A graph
c) A queue
d) A stack
e) A tree
Week 6 Quiz
8. According to Anna Lytical, if you put your make up in a data structure in the
order you want to apply it and the first thing you put in the data structure is
the first think you apply (the first step you take), you are using the following
data structure:

a) An array
b) A graph
c) A queue
d) A stack
e) A tree
Summary
These data structures are treated differently in different
programming languages and there are others as well
Different programming languages have different ways of making data
structures like these available to programmers with different
characteristics, syntax, names, operations, etc.
It is useful in the computational thinking sense to understand these
data structures in the abstract sense!

54
Agenda
Objectives
Data structures
Books are Us data structures
Text processing and regular expressions
Break

55
Books-R-Us Data Structures

56
Example Data Items
Consider the “things” (data items) that we need to input, store,
manipulate, and/or output

57
Example Data Items
1. Catalog of books (bookID, title, author, price, and the number of
copies of the book that are available for purchase, pending
transactions on that book)
2. List of shoppers and their information (shopperID, name, address,
credit card, shopping cart info)
3. Shopping cart info (list of books: bookID, quantity, price)

58
Example Operations / Processes /
Procedures
Consider the operations / processes / procedures that we
need to support

59
Example Operations / Processes /
Procedures
1. Read in search info (title, author last name), search catalog, return book info or not found
2. Read in book catalog from csv file
3. Write out book catalog to csv file
4. Search for book by title
5. Search for book by author last name
6. Add book to shopping cart (one at a time)
7. Remove book from shopping cart (one at a time)
8. Shopper purchases items in their shopping cart
9. Shopper leaves the site without making a purchase

60
Consider Data Structures
What kind of data structures might we use to store the following:
1. Catalog of books (bookID, title, author, price, and the number of copies of the book that are
available for purchase, pending transactions on that book)
2. List of shoppers and their information (shopperID, name, address, credit card, shopping cart info)
3. Shopping cart info (list of books: bookID, quantity, price)
We learned about:
◦ Arrays
◦ Lists
◦ Stacks
◦ Queues
◦ Hierarchies
◦ Graphs
◦ Binary trees

61
Consider the Following Fiction Books
bookID Author Title Price Quantity Pending
1 Ashley Audrain The Whispers - Indigo Exclusive Edition: A Novel $17.46 1 0
2 R. F Kuang Yellowface: A Novel $17.49 2 0
3 Riley Sager The Only One Left $18.71 1 0
4 Ali Hazelwood Love Theoretically $18.40 4 0
5 Ruth Ware Zero Days $18.89 3 0
6 Stephen King Fairy Tale $27.00 1 0
7 Elin Hilderbrand The Five-Star Weekend $24.99 1 0
8 Holly Smale Cassandra in Reverse $24.99 4 0
9 Tessa Bailey Unfortunately Yours: A Novel $23.99 2 0
10 E L James The Missus $26.99 5 0

62
Arrays

0 1 2 3 4 5 6 7 8 9

bookID: 1 bookID: 5 bookID: 10


Author: Ashley Audrain Author: Ruth Ware Author: E L James
Title: The Whispers - Indigo Exclusive Edition: A Novel Title: Zero Days Title: The Missus
Price: $17.46 … Price: $18.89 … Price: $26.99
Quantity: 1 Quantity: 3 Quantity: 5
Pending: 0 Pending: 0 Pending: 0

63
Linked Lists
HEAD

bookID: 1
bookID: 5 NULL
Author: Ashley Audrain
Author: Ruth Ware
Title: The Whispers - Indigo Exclusive Edition: A Novel
Title: Zero Days
Price: $17.46
Price: $18.89
Quantity: 1
Quantity: 3
Pending: 0
Pending: 0

64
Binary Search Trees
The Only One Left

Love Theoretically Unfortunately Yours: A Novel

Fairy Tale The Missus The Whispers Zero Days

Cassandra in Reverse The Five-Star Weekend Yellowface: A Novel

65
Array, Linked List, or Binary Tree for the
Catalog?
Arrays:
◦ Any list can be stored in memory as an array
◦ Finding an element is easy if we know the index or position
◦ A new item can be inserted only if there is space
Linked Lists:
◦ Any list can be stored in memory as a list
◦ Finding the Nth element requires N-1 steps
◦ Inserting or deleting an element is easy
Binary Search Tree:
◦ Can represent the same that a list can but in a form that (in general) supports faster searching
◦ Can become “unbalanced” if updated a lot

66
Week 6 Quiz
2. Match each of the properties to the appropriate data structure:

a) Finding an element is easy if we know the index or position


b) Inserting or deleting an element is easy
c) A new item can be inserted only if there is space
d) Finding the Nth element requires N-1 steps

“appropriate data structures” to be matched to the properties: Array, Linked


list
Week 6 Quiz
2. Match each of the properties to the appropriate data structure:

a) Finding an element is easy if we know the index or position (Array)


b) Inserting or deleting an element is easy (Linked List)
c) A new item can be inserted only if there is space (Array)
d) Finding the Nth element requires N-1 steps (Linked List)

“appropriate data structures” to be matched to the properties: Array, Linked


list
Agenda
Objectives
Data structures
Books are Us data structures
Text processing and regular expressions
Break

69
Text Processing
“A string is simply a piece of text, or more formally, an ordered
sequence of individual characters.”
◦ Any sequence of characters that is enclosed by double-quotes
is known as a string literal (the quotes aren’t part of the
string)

if feedback == “+++” then


print (“Congratulations! You have guessed the
secret number in: ”, countOfGuesses, “ guesses.”)

70
Week 6 Quiz
4. Using the way the textbook demonstrates string indexing and concatenating,
which of the following produces the string “hire me” from the string B given
below:

B  “The Faculty of Information at the University of Toronto”

a) B[2]+B[42]+B[40]+B[33]+B[27]+B[21]+B[3]
b) B[1]+B[23]+B[39]+B[32]+B[26]+B[20]+B[2]
c) B[2]+B[24]+B[40]+B[33]+B[27]+B[21]+B[3]
d) B[1]+B[15]+B[39]+B[32]+B[26]+B[20]+B[2]
Indexing the String B
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

T h e F a c u l t y o f I n f o r m a t i o n a t

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

t h e U n i v e r s i t y o f T o r o n t o

a) B[2]+B[42]+B[40]+B[33]+B[27]+B[21]+B[3]
b) B[1]+B[23]+B[39]+B[32]+B[26]+B[20]+B[2]
c) B[2]+B[24]+B[40]+B[33]+B[27]+B[21]+B[3]
d) B[1]+B[39]+B[32]+B[26]+B[20]+B[2]
B[15]+

72
Week 6 Quiz
4. Using the way the textbook demonstrates string indexing and concatenating,
which of the following produces the string “hire me” from the string B given
below:

B  “The Faculty of Information at the University of Toronto”

a) B[2]+B[42]+B[40]+B[33]+B[27]+B[21]+B[3]
b) B[1]+B[23]+B[39]+B[32]+B[26]+B[20]+B[2]
c) B[2]+B[24]+B[40]+B[33]+B[27]+B[21]+B[3]
d) B[1]+B[15]+B[39]+B[32]+B[26]+B[20]+B[2]
Week 6 Quiz
7. Which of the following are strings of length 8 (select all that apply):

a) Kelly Lyons
b) 10/08/24
c) Bachelor
d) eight
e) 8888888
f) 01234567
g) http://a
Week 6 Quiz
7. Which of the following are strings of length 8 (select all that apply):

a) Kelly Lyons
b) 10/08/24
c) Bachelor
d) eight
e) 8888888
f) 01234567
g) http://a
Regular Expressions (Patterns in Text)

76
Week 6 Quiz
5. All graduate courses at the University of Toronto have a course code that consists of:
a prefix associated with the academic unit or program (three uppercase letters); a
four-digit course number; and, a suffix associated with the course weight (either Y –
full course/1.0 FCE or H – half course/0.5 FCE). Which regular expression is a correct
pattern for graduate course codes at University of Toronto (such as INF1339H,
INF2194Y, CSC2209H, etc.)

a) [A-Z]{3}[0-9]{4}[H|Y]
b) [A-Z]{3}[0-9]{4}[H]+[Y]+
c) [A-Z]{3}[0-9]{4}(H|Y)
d) (A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z){4}[0-
9]{4}(H|Y)
Week 6 Quiz
5. All graduate courses at the University of Toronto have a course code that consists of:
a prefix associated with the academic unit or program (three uppercase letters); a
four-digit course number; and, a suffix associated with the course weight (either Y –
full course/1.0 FCE or H – half course/0.5 FCE). Which regular expression is a correct
pattern for graduate course codes at University of Toronto (such as INF1339H,
INF2194Y, CSC2209H, etc.)

a) [A-Z]{3}[0-9]{4}[H|Y]
b) [A-Z]{3}[0-9]{4}[H]+[Y]+
c) [A-Z]{3}[0-9]{4}(H|Y)
d) (A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z){4}[0-
9]{4}(H|Y)
Week 6 Quiz
9. A character class is pattern that concisely defines a set of
characters. The class of word characters denotes any of the
following characters: a-z, A-Z, 0-9 (true or false)
Week 6 Quiz
9. A character class is pattern that concisely defines a set of
characters. The class of word characters denotes any of the
following characters: a-z, A-Z, 0-9 (true or false)
Regular Expressions
Regular expression “cheat sheet”:
https://cheatography.com/davechild/cheat-sheets/regular-
expressions/
Practice here: https://regex101.com/
Often used to clean data and get it ready for analysis

81
Agenda
Objectives
Data structures
Books are Us data structures
Regular expressions (patterns in text)
Break

82
Move to your Tutorial Room
Tuesday, Oct 8 Tut101: Room 5170: OISE Victoria

Tut102: Room 5230: OISE Kelly


Tut103: Room 5250: OISE Inessa
Wednesday, Oct 9 Tut201: Room 325: Claude T. Bissell Building Victoria
Tut202: Room 520: Claude T. Bissell Building Kelly
Tut203: Room 417: Claude T. Bissell Building Marcus

83
Break

84

You might also like