Collection Classes Quiz
1. What is the difference between a set and a bag?
Duplicate values are allowed in a bag, but
not in a set.
2. Sets are considered to be simpler than bags, and bags are simpler than
sequences. What is the simplest data type you could use to represent each of the
following?
Hint: only use a sequence when it is important to specify the order
of the values in the collection.
- All the prime numbers between 1 and 1000
- The marks obtains by a class of 30 students, anonymized (i.e. you wish to store
just the marks obtained, not the names of the students), where the marks are
integers
- The names of all the students in a class
- A shopping list (assume that each item in the list can be represented by
a value of type string)
- The same shopping list, but arranged in the order in which the items
appear on the shelves of the store
- set of int
- bag of int
- set of string if we assume that no two students have the same name (so if
we get two students called "John Smith" then we would have to call them
e.g. "John 1
Smith" and "John 2 Smith"). Otherwise, bag of string.
- set of string assuming that we never write the same item down two or more
times, otherwise bag of string.
- seq of string because the order is important.