Set and List in Data Structures
A while ago I did a technical assessment/coding challenge and thought this would be pretty easy except I didn’t have a clear understanding of what ‘set’ and ‘list’ meant. Is ‘set’ just the concept of turning it into 1 singular object by putting everything in curly braces? If everything is already present and ‘listed’ inside the ‘set’ what actually changes by converting a ‘set’ into a list? If we wanted the final outcome to be a ‘list’, why put them into a ‘set’ in the first place?
Fortunately, I understand ‘sort’ and since we want the numbers to be in ascending order, we can eliminate the 2nd option where the numbers are in descending order.
Set
Upon initial research (aka Google), all the results to ‘insert elements into a set’ are in the C++ and Python realm. So, we’re delving into some high-level stuff in languages I have not yet pursued. But, I just want to get an idea of what a ‘set’ is and how to ‘insert elements into a set’.
From Geeks for Geeks:
The set::insert is a built-in function in C++ STL which insert elements in the set container or inserts the elements from a position to another position in the set to a different set.
Well, this is a confusing explanation. Is the ‘set container’ made before the ‘set insert’ function or does it create it in order to insert elements into it?
And, I absolutely cannot follow what this means. To “inserts the elements from a position to another position in the set to a different set’.
What is a ‘set’?
According to Computer Science Wiki, it is “an abstract data type that can store certain values, without any particular order, and not repeated values.”
A clue! No repeated values. We can now eliminate the 3rd option.
To recap, we’re beginning with elements 1, 2, 9, 1, 2, 3, 1, 4, 1, 5, 7 and after putting it into a ‘set’ with no particular order and no repeated values we’ll end up with something like 1, 2, 9, 3, 4, 5, 7.
The C++ explanation from Geeks for Geeks still doesn’t make sense to me. And, I’m still not sure where the curly braces come in. But, let’s move on. We’ve already eliminated half the answers.
List
What is a ‘list’?
Computer Science Circles, defined a list as “a sequence of several variables, grouped together under a single name”
One way to create a list is by enclosing several values, separated with commas, between square brackets.
Simple, I’ve seen these before and I call them arrays!
exampleSet = [1, 2, 3, 4, 5]
Note, Computer Science Circles did allude to lists and arrays in the title. So a ‘list’ is a grander idea and arrays are an example of lists.
They also mention Python. A list in Python is called an array in most other programming languages.
To recap, all arrays are lists. In most languages, they’ll just be called an array. Except in Python, they’ll call it in its “higher” and more general/basic form, a list.
Aside from arrays, what other types of lists are there?
Digging around for answers just brings up more Python and more arrays (values in square brackets).
But, 3 of the 4 of the options to the problem I’m trying to solve were presented in curly braces. Of course, the final option would be “None of the above”, aka a “Trick Question”.
Programmer Sought, explains the difference between parentheses ( ), brackets [ ] and curly braces { } in Python 3. The brackets represent a list data type just like I mentioned earlier.
Curly braces { }, on the other hand represent a dictionary data type. And this is the type of object I’m more familiar with curly braces encapsulating. Things don’t need to be in a particular order like an array because everything is a key and value pair. So, like a dictionary, values can be retrieved if you know the key.
exampleDictionary = { ‘E’: 5, ‘A’: 1, ‘D’: 4, ‘C’:3, ‘B’: 2 }
I’m not finding any other version of a list that’s not in square brackets[ ]. In other words, I’m not finding a correlation between curly braces { } as a list type, since it is a dictionary type and functions differently.
Though, I did find a hint in this Stack Overflow stating that, “Curly braces create dictionaries or sets. Square brackets create lists.”
Then it gives a couple good examples between the 3 ‘literals’.
The set literal is pretty close to the 3 options (though 2 are already eliminated) to the problem I’m trying to solve.
Big Brain Time
If it’s asking me to ‘convert the set into a list’ yet only gives me ‘set literals’ as options… then it’s probably ‘None of the above.’
Challenges
I did not know any Python or C++. If I did, then maybe I would’ve been more familiar with the idea of a ‘set’ versus a ‘list’. My reluctance to believe the answer would be ‘None of the above.’
Lastly, since the technical assessment is done and over with, I won’t have confirmation on whether I truly figured it out or not.