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

AoT 02 Structure Flowchart Assignment 2

The document discusses improving functions that perform set operations by adding logic to handle corner cases. It then provides examples of combining set aggregation functions and set operation functions into new functions, such as calculating the sum of the intersection or product of the difference of two sets. Finally, it discusses using nested for loops and the join function to compute factorials.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

AoT 02 Structure Flowchart Assignment 2

The document discusses improving functions that perform set operations by adding logic to handle corner cases. It then provides examples of combining set aggregation functions and set operation functions into new functions, such as calculating the sum of the intersection or product of the difference of two sets. Finally, it discusses using nested for loops and the join function to compute factorials.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Art of Thinking

Algorithmic Thinking | Flowcharts


Assignment 2

1. There are two important skills of a good software engineer (a) consider all corner cases and (b) try to
improve the speed of the code. In the following example for the function intersection(A, B), see how we
have improved the original code with the corner cases as well as improved the speed.

def intersection(A, B):


I = set() # null set
If (len(A) == 0): return I # If A is empty, then intersection is a null set
If (len(B) == 0): return I # If B is empty, then intersection is a null set
If (len(A) > len(B)): # make sure first set is smaller for faster execution
return intersection(B, A)
… # Rest of the intersection code will go here.

Now think about the following operations on sets we have already written in class. Please write the
additional code before the start of the iteration to make those functions “better”. Copy the remaining
code from class slides and improve the original code.

(a) def belongsTo(x, B)


• HINT: what if B is an empty set?

(b) def union(A, B)


• HINT: What if A is an empty set but B is not? What if B is an empty set but A is not?
• HINT: If BOTH are NOT empty but |A| is much smaller than |B| then how should we initialize U
• Using above hints complete the better version of the union code.

(c) def difference(A, B)


• HINT: What if A is empty?
• HINT: What if B is empty?

(c) def subset(A, B) [Return True is A is the subset of B]


• HINT: What if set A is empty? (Empty set is always a subset of the other set)
• HINT: What if set B is empty?
• HINT: What if size of set A is larger than size of set B

2. We know how to do AGGREGATIONS (sum, min, max, prod) on Sets and OPERATIONS (belongsTo,
intersection, union, difference, subset) on Sets. We will now combine the two into a single function. First
one is an example. Note how these can be done in two ways.

PREPARATION: First write the following functions from class notes (plus above improvements)
• sum_of_set(A), prod_of_set(A), min_of_set(A), max_of_set(A)
• intersection(A, B), union(A, B),
difference(A, B):

(a) def sum_of_intersection(A, B):

Composition Method: (using other


functions to write this function)

def sum_of_intersection(A, B):


I = intersection(A, B)
return sum_of_set(I)
First Principles Method: See the flowchart above of the same without using the other functions.

(b) def prod_of_difference(A, B):


• Composition Method: Write the python program in terms of above functions
• First Principles Method: Draw the flowchart as above & write the python program

(c) def min_of_difference(A, B):


• Composition Method: Write this using existing functions above.
• First Principles Method: Draw the flowchart as above & write the python program

3. Let’s say you are given two Sets: 𝐀 = {𝑎, 𝑏, 𝑐} and 𝐁 = {𝑝, 𝑞}. Let’s say we want to compute the following
function called the join(A, B) of the two sets as follows

𝑗𝑜𝑖𝑛(𝐀, 𝐁) = 𝑎𝑝 + 𝑎𝑞 + 𝑏𝑝 + 𝑏𝑞 + 𝑐𝑝 + 𝑐𝑞

i.e. we want to take EACH element in A and multiply with EACH element of B and then add all the products.

(a) Below is the code for join(A, B). Please fill in the blanks. This is called a NESTED for loop (one for loop
inside another).

def join(A, B):


sum = _____
for x in _____: # For each element in A
for y in _____: # For each element in B
sum = sum + _______ # Increment the sum appropriately
return _____
end

(b) Fill the blanks in the below flowchart with the correct statements. Fill the boxes with Yes or No.

(c) What is the value of: 𝑗𝑜𝑖𝑛(∅, {5})?

(d) What is the value of: 𝑗𝑜𝑖𝑛({3}, {3}). Is it true that 𝑗𝑜𝑖𝑛({𝑥}, {𝑥}) = 𝑥 !

(f) What is the value of: 𝑗𝑜𝑖𝑛({3}, {5}). What is 𝑗𝑜𝑖𝑛({𝑥}, {𝑦})?

(c) If we want to compute (𝑎 + 𝑏)! how can we use the join function?

(d) If we want to compute (𝑎 + 𝑏 + 𝑐)! how can we use the join function?

You might also like