Set Exercises 3

Harold Nelson

3/2/2020

Primary Possibilities

These exercises are based on the material beginning on page 46 of the finite mathematics text.

The first task is to generate the data in the table. Each row of the table is to be represented as a tuple with 5 items (a 5-tuple).

The first item in the tuple is a sequential number beginning with 1 and ending with 36. The remaining items in the tuple are one-character strings giving the winners of the primaries in the four states. You should use the same pattern of nested for loops inherent in the table as it is presented in the text.

The entire table, for our purposes is the set of all of these tuples.

Generate the table as a list and print it. Compare your results with the table in the text by spot checking. Since the list is in order, this is not difficult.

Answer

possibilities = []
i = 0
for NH in "AC":
    for MN in "ABC":
        for WI in "AB":
            for CA in "ABC":
                i += 1
                outcome = (i,NH,MN,WI,CA)
                possibilities.append(outcome)
for i in possibilities:
    print(i)
## (1, 'A', 'A', 'A', 'A')
## (2, 'A', 'A', 'A', 'B')
## (3, 'A', 'A', 'A', 'C')
## (4, 'A', 'A', 'B', 'A')
## (5, 'A', 'A', 'B', 'B')
## (6, 'A', 'A', 'B', 'C')
## (7, 'A', 'B', 'A', 'A')
## (8, 'A', 'B', 'A', 'B')
## (9, 'A', 'B', 'A', 'C')
## (10, 'A', 'B', 'B', 'A')
## (11, 'A', 'B', 'B', 'B')
## (12, 'A', 'B', 'B', 'C')
## (13, 'A', 'C', 'A', 'A')
## (14, 'A', 'C', 'A', 'B')
## (15, 'A', 'C', 'A', 'C')
## (16, 'A', 'C', 'B', 'A')
## (17, 'A', 'C', 'B', 'B')
## (18, 'A', 'C', 'B', 'C')
## (19, 'C', 'A', 'A', 'A')
## (20, 'C', 'A', 'A', 'B')
## (21, 'C', 'A', 'A', 'C')
## (22, 'C', 'A', 'B', 'A')
## (23, 'C', 'A', 'B', 'B')
## (24, 'C', 'A', 'B', 'C')
## (25, 'C', 'B', 'A', 'A')
## (26, 'C', 'B', 'A', 'B')
## (27, 'C', 'B', 'A', 'C')
## (28, 'C', 'B', 'B', 'A')
## (29, 'C', 'B', 'B', 'B')
## (30, 'C', 'B', 'B', 'C')
## (31, 'C', 'C', 'A', 'A')
## (32, 'C', 'C', 'A', 'B')
## (33, 'C', 'C', 'A', 'C')
## (34, 'C', 'C', 'B', 'A')
## (35, 'C', 'C', 'B', 'B')
## (36, 'C', 'C', 'B', 'C')

Exercise

Convert the list from the previous exercise to a set called Universe. Print the Universe.

Answer

Universe = set(possibilities)
for i in Universe:
    print(i)
## (18, 'A', 'C', 'B', 'C')
## (34, 'C', 'C', 'B', 'A')
## (1, 'A', 'A', 'A', 'A')
## (35, 'C', 'C', 'B', 'B')
## (22, 'C', 'A', 'B', 'A')
## (23, 'C', 'A', 'B', 'B')
## (33, 'C', 'C', 'A', 'C')
## (7, 'A', 'B', 'A', 'A')
## (13, 'A', 'C', 'A', 'A')
## (19, 'C', 'A', 'A', 'A')
## (26, 'C', 'B', 'A', 'B')
## (12, 'A', 'B', 'B', 'C')
## (11, 'A', 'B', 'B', 'B')
## (3, 'A', 'A', 'A', 'C')
## (2, 'A', 'A', 'A', 'B')
## (5, 'A', 'A', 'B', 'B')
## (32, 'C', 'C', 'A', 'B')
## (4, 'A', 'A', 'B', 'A')
## (24, 'C', 'A', 'B', 'C')
## (27, 'C', 'B', 'A', 'C')
## (28, 'C', 'B', 'B', 'A')
## (9, 'A', 'B', 'A', 'C')
## (16, 'A', 'C', 'B', 'A')
## (8, 'A', 'B', 'A', 'B')
## (31, 'C', 'C', 'A', 'A')
## (17, 'A', 'C', 'B', 'B')
## (30, 'C', 'B', 'B', 'C')
## (14, 'A', 'C', 'A', 'B')
## (36, 'C', 'C', 'B', 'C')
## (29, 'C', 'B', 'B', 'B')
## (20, 'C', 'A', 'A', 'B')
## (15, 'A', 'C', 'A', 'C')
## (21, 'C', 'A', 'A', 'C')
## (25, 'C', 'B', 'A', 'A')
## (6, 'A', 'A', 'B', 'C')
## (10, 'A', 'B', 'B', 'A')

Exercise

Write a function counter(cand,outcome) which returns the count of a candidate’s win in a particular outcome. For example counter(“A”, (1,“A”,“A”,“A”,“A”)) should return 4.

Test your function with a few of the possibilities you see above.

Answer

def counter(cand, outcome):
    count = 0
    for winner in outcome:
        if winner == cand:
            count += 1
    return count
    
# Tests
print(counter("B", (14, 'A', 'C', 'A', 'B')))
## 1
print(counter("C", (3, 'A', 'A', 'A', 'C')))
## 1
print(counter("B", (3, 'A', 'A', 'A', 'C')))
## 0
print(counter("A", (3, 'A', 'A', 'A', 'C')))
## 3

Example 1

Produce the set described in Example 1. Display it in numerical order.

Answer


Example1 = set()

for OC in Universe:
    if (counter("B",OC) > counter("A",OC)) and \
       (counter("B",OC) > counter("C",OC)):
        Example1.add(OC)

for OC in sorted(Example1):
    print(OC)
                   
## (11, 'A', 'B', 'B', 'B')
## (12, 'A', 'B', 'B', 'C')
## (17, 'A', 'C', 'B', 'B')
## (23, 'C', 'A', 'B', 'B')
## (26, 'C', 'B', 'A', 'B')
## (28, 'C', 'B', 'B', 'A')
## (29, 'C', 'B', 'B', 'B')

Exercise 1a.

Produce the set described in Exercise 1a and print it in numerical order.

Answer

Ex1a = set()
for OC in Universe:
    if counter("A",OC) == counter("C",OC):
        Ex1a.add(OC)
  

for OC in sorted(Ex1a):
    print(OC)
## (12, 'A', 'B', 'B', 'C')
## (15, 'A', 'C', 'A', 'C')
## (17, 'A', 'C', 'B', 'B')
## (21, 'C', 'A', 'A', 'C')
## (23, 'C', 'A', 'B', 'B')
## (26, 'C', 'B', 'A', 'B')
## (28, 'C', 'B', 'B', 'A')
## (31, 'C', 'C', 'A', 'A')

Exercise 1b.

Produce the set described in Exercise 1b and print it in numerical order.

Answer

Ex1b = set()
for OC in Universe:
    if OC[1] != OC[2] and OC[1] != OC[3] and OC[1] != OC[4]:
        Ex1b.add(OC)
    
for OC in sorted(Ex1b):
    print(OC)
## (11, 'A', 'B', 'B', 'B')
## (12, 'A', 'B', 'B', 'C')
## (17, 'A', 'C', 'B', 'B')
## (18, 'A', 'C', 'B', 'C')
## (19, 'C', 'A', 'A', 'A')
## (20, 'C', 'A', 'A', 'B')
## (22, 'C', 'A', 'B', 'A')
## (23, 'C', 'A', 'B', 'B')
## (25, 'C', 'B', 'A', 'A')
## (26, 'C', 'B', 'A', 'B')
## (28, 'C', 'B', 'B', 'A')
## (29, 'C', 'B', 'B', 'B')

Exercise 1c.

Produce the set described in Exercise 1c and print it in numerical order.

Ex1c = set()
for OC in Universe:
    if counter("C",OC) == 0:
        Ex1c.add(OC)  

for OC in sorted(Ex1c):
    print(OC)
## (1, 'A', 'A', 'A', 'A')
## (2, 'A', 'A', 'A', 'B')
## (4, 'A', 'A', 'B', 'A')
## (5, 'A', 'A', 'B', 'B')
## (7, 'A', 'B', 'A', 'A')
## (8, 'A', 'B', 'A', 'B')
## (10, 'A', 'B', 'B', 'A')
## (11, 'A', 'B', 'B', 'B')

Intersections

Show the intersections of Ex1a, Ex1b and Ex1c. How many are there?

print("1a and 1b")
## 1a and 1b
print(Ex1a & Ex1b)
## {(23, 'C', 'A', 'B', 'B'), (17, 'A', 'C', 'B', 'B'), (26, 'C', 'B', 'A', 'B'), (12, 'A', 'B', 'B', 'C'), (28, 'C', 'B', 'B', 'A')}
print(" ")
print("1a and 1c")
## 1a and 1c
print(Ex1a & Ex1c)
## set()
print(" ")
print("1b and 1c")
## 1b and 1c
print(Ex1b & Ex1c)
## {(11, 'A', 'B', 'B', 'B')}