đ Back to problem set index.
Problem One: Much Ado About Nothing
It can take a bit of practice to get used to the empty set. This problem will ask you to think about a few different sets related to $\emptyset$.
Answer each part of this question by editing the file MuchAdoAboutNothing.sets
in the res/
directory of the starter files. Thereâs information in the top of each of the files about how to represent sets; most importantly, note that to write out the empty set, you should write {}
rather than using the empty-set symbol. For example, the set $\Set{\emptyset}$ would be written as { { } }
.
Give a set equal to $\emptyset \cup \Set{\emptyset}$.
Give a set equal to $\emptyset \cap \Set{\emptyset}$.
Give a set equal to $\Set{\emptyset} \cup \Set{\Set{\emptyset}}$.
Give a set equal to $\Set{\emptyset} \cap \Set{\Set{\emptyset}}$.
Give a set equal to $\wp(\wp(\emptyset))$.
Give a set equal to $\wp(\wp(\wp(\emptyset)))$.
The starter code contains a driver program you can use to see the contents of your files and run tests to check your answers. Submit your answers through GradeScope under âCoding Problems for Problem Set Oneâ by uploading your edited files. Youâre welcome to submit as many times as youâd like; weâll count as your grade the last version of the uploaded files.
Problem Two: Set Theory in C++
The C++ standard library contains a type called std::set
that represents a set of elements, all of which must be of the same type. For example, the type std::set<int>
represents a set of integers, the type std::set<std::string>
represents a set of strings, and std::set<std::set<int>>
is a type representing a set of sets of integers.
There are all sorts of operations you can perform on std::sets
. For example, hereâs how you iterate over all the elements of a set:
std::set<T> mySet;
for (T elem: mySet) {
/* ⊠do something with the current element elem ⊠*/
}
(Don't worry if you see a warning message when using this style of loop; it's perfectly fine for our purposes.)
Hereâs how you check whether a particular value is an element of a set:
if (mySet.count(value)) {
/* ⊠value â mySet ⊠*/
} else {
/* ⊠value â mySet ⊠*/
}
And, finally, hereâs how you can get the cardinality of a set:
size_t size = mySet.size();
Here, the size_t
type is a type representing a natural number, since sets canât have negative size. (The folks who designed the C++ standard libraries had a strong discrete math background.)
One of the major differences between the sets weâve talked about in CS103 and the std::set
type is that in discrete mathematics, sets can contain anything â numbers, philosophies, recipes, other sets, etc. â but in C++ all objects in a set must have the same type. For the purposes of this problem, weâve created a custom C++ type called Object
. Variables of type Object
can represent just about anything, so a std::set<Object>
represents something pretty similar to the sets weâve been studying so far.
Some Object
s are actually just std::sets in disguise. If you have an Object, you can test whether itâs actually a set by using this provided helper function:
bool isSet(Object o);
This takes in an Object
, then returns true
if that Object
is actually a set and false
otherwise. If you have an Object
that really is a set, you can convert it to a set by using this helper function:
std::set<Object> asSet(Object o);
This function takes in an Object
that you know happens to be a set, then returns the std::set<Object>
that it actually is.
For example, suppose you have an Object
that you know is really the set $\Set{1, 2, 3, 4}$. You could iterate over it using this code:
Object reallyASet = /* ⊠*/;
for (Object x: asSet(reallyASet)) {
/* ⊠do something with x ⊠*/
}
In this problem, weâd like you to demonstrate your understanding of sets and set theory by coding up a number of functions in C++ that operate on sets. In doing so, we hope that youâll solidify your grasp of the distinctions between related concepts in set theory, such as the the $\in$ and $\subseteq$ relations and power sets.
Open the file SetTheory.cpp
from the starter files. There, youâll find a bunch of stubs of functions that youâll need to implement. The provided starter code contains a test harness you can use to try out your functions. You wonât need to modify any of the other C++ files bundled with the starter code.
As with Problem One, youâll submit the code that you write through GradeScope separately from the rest of the problems on this problem set. The GradeScope autograder will get back to you with feedback about how youâre doing on this problem, and youâre welcome to submit as many times as youâd like.
-
Implement a function
bool isElementOf(Object S, Object T);
that takes as input two
Object
s $S$ and $T$, then returns whether $S \in T$.$S$ and $T$ might not be sets; youâll need to use the
isSet
andasSet
functions appropriately.
-
Implement a function
bool isSubsetOf(Object S, Object T);
that takes as input an object $S$ and an object $T$, then returns whether $S \subseteq T$.
$S$ and $T$ might not be sets; use the
isSet
predicate to check whether the appropriate arguments are sets andasSet
to get a view of them as sets.
-
Implement a function
bool areDisjointSets(Object S, Object T);
that takes as input two objects $S$ and $T$, then returns whether $S$ and $T$ are sets where $S \cap T = \emptyset$. (Two sets with this property are called disjoint.) The input parameters $S$ and $T$ may or may not be sets, and if they arenât, your function should return
false
.
-
Implement a function
bool isSingletonOf(Object S, Object T);
that takes as input two objects $S$ and $T$, then returns whether $S = \Set{T}$. Again, $S$ and $T$ may or may not be sets.
-
Implement a function
bool isElementOfPowerSet(Object S, Object T);
that takes as input two objects $S$ and $T$, then returns whether $S$ and $T$ are sets and $S \in \wp(T)$. Again, $S$ and $T$ may or may not be sets.
As a hint, you shouldnât need to write code that computes $\wp(T)$ explicitly. See if you can find a different way to do this.
-
Implement a function
bool isSubsetOfPowerSet(Object S, Object T);
that takes as input two objects $S$ and $T$, then returns whether $S$ and $T$ are sets and $S \subseteq \wp(T)$. Again, $S$ and $T$ may or may not be sets.
-
Implement a function
bool isSubsetOfDoublePowerSet(Object S, Object T);
that takes as input two objects $S$ and $T$, then returns whether $S$ and $T$ are sets and $S \subseteq \wp(\wp(T))$. Again, $S$ and $T$ may or may not be sets.
To submit your work, upload your edited SetTheory.cpp
file to GradeScope. Youâll get immediate feedback on your score from our autograder, and youâre allowed to resubmit as many times as youâd like.
As a note, you shouldnât edit SetTheory.h
; our autograder will always use the default version of that file when running tests.
Problem Three: Describing the World in Set Theory
The notation of set theory (e.g. $\cup, \cap, \wp, \subseteq, \in, \emptyset, =,$ etc.) is a great tool for describing the real world. Answer each of the following questions by writing an expression using set theory notation, but without using plain English, without using set-builder notation, without introducing any new variables, and without using propositional or first-order logic (which weâll cover next week). No justification is required.
-
Let $C$ be the set of all cats and $D$ be the set of all dogs. Let $A$ be the set of all adorable things and $B$ be the set of all bouncy things. Write an expression that says "all cats and all dogs are both adorable and bouncy."
Once youâve written up your answer to this problem, take a minute to type-check it. As an example, suppose that you have the following answer:
\[(A \in B) \cap (A \in C).\]This expression canât be correct, and hereâs one way to see this. The expression $A \in B$ is of type Boolean â either $A \in B$ is true or it isnât â and the same is true of $A \in C$. However, the intersection operator $\cap$ can only be applied to sets. The expression therefore contains a type error: it tries to apply an operator that only works on sets to Boolean values.
You can type-check this answer in a different way. For example, suppose you came up with this expression:
\[A \cup D.\]Here, $A$ and $D$ are sets, so itâs perfectly fine to write $A \cup D$, which evaluates to an object of type set. But notice that the statement here asks you to write an expression that says âall cats and all dogs are both adorable and bouncy,â and that statement is of type Boolean (cats and dogs either have these properties or they donât). Therefore, $A \cup D$ canât possibly be an expression with the right meaning, since the type of the expression (set) doesnât match the type of the statement (Boolean).
If youâre having trouble with this problem, consider working backwards. You know that you need an expression that evaluates to type Boolean. What operations on sets do you have that produce Booleans? Or perhaps make the problem simpler. How would you say "all cats are adorable?" How about "all cats are both adorable and bouncy?"
-
Suppose youâre on an exciting first date. Let $Y$ represent the set of all your hobbies and $D$ represent the set of all your dateâs hobbies. Write an expression that says that you have a hobby that your date doesnât have.
As before, type-check your answer! Should your answer represent a set? A Boolean? Something else?
-
Letâs say that a committee is a group of people, which we can think of as being represented by the set of people that comprise it. Letâs have $S$ represent the set of all students at Stanford and let $F$ represent the set of all faculty at Stanford. Write an expression representing the set of all committees you can make from Stanford students and faculty that contain at least one student and at least one faculty member. You can assume no one is both a student and a faculty member.
Something to think about: how would you say âall committees made purely of students?â Something else to think about: what is the type of the statement to translate? Is it a Boolean? A set? Neither?