Collections in IB book are explained a little bit messy. You can go from page 256 onwards.
Collections in PSEUDOCODE are an abstract of other "iterables" from other programming languages
Other iterables like lists, linkedlists, Calendars, Queues, Stacks, Binary trees.
For example in Java these are the UML diagram of collections. You can never have an instance of a collection but you can have an instance of an ArrayList that is an AbstractList, that is a List and an Abstract Collection and is a Collection and all of them are Iterables
The specifics of those collections are for HL but they share some stuff. In this case that we can iterate through them and that accessing to one specific element is not that fast.
In the case of the abstract (more high level) definition of the collection we're going to have some values and to access them we need to use a cursor.
You need to know and use these functions of a collection.
That cursor is going to move (we don't need to know how) through the collection every time that we call the function "getNext()" and "getNext()" function is going to return the value that it was stored.
Example:
Each time that we call getNext() we are going to have the next element. So if we want to do something with it we should keep it in a variable.
To know if we have finished with the collection we can access to a function that is going to return a boolean that will return false in the case that we have finished the collection and true otherwise. That function is called "hasNext()"
Example:
If we have a TABLE collection and we call hasNext() is going to return true because we have still 2 more elements until the end of the collection.
Then to reset the cursor to the "first" position we call the function resetNext() that is a function that is not going to return nothing but it's going to make sure that the cursor is in the "first" position.
If we have a collection of NIKESNEAKERS and we want to output just the first one this would be the code:
They don't usually ask this kind of specific things of "give me the first 3 or last 5 elements", they usually go with all the collection.
Finally to add a new item (a new value) we use "addItem()" sending the value that we want to add.
To write them we need to write the name of the collection (usually in Upper case), a dot and the name of the method. An example would be POTATOES.addItem(POTATO)
We're working on a sushi restaurant webpage and they have all the sushi dishes in a collection called DISHES.
We can start by outputting all of them. This is iterating through a collection. Pretty similar and standar if you know how to iterate through an array.
Let's do an iteration through a collection of STUDENTS. Let's output the name of the students that are inside the collection.
Why the resetNext()? It's not stricly necessary in terms of IB but is a good measure. Imagine that we want to iterate the Dishes twice. Once to output the values and the other that is going to call a function called translate to translate the dishes to the prefered language. If we don't use resetNext, when we finish the first loop the second one is not going to do nothing since hasNext is going to be false
To solve that we reset the cursor using DISHES.resetNext()
In actual programming you probably are not going to be sure to know if this method has been called already so is a good safety measure to use it just before any loop through the collection.
Let's see if we want to count how many dishes are nigiris. We know that if a dish is a nigiri it will contain the string "nigiri".
For example, "tuna nigiri", "salmon nigiri", "shrimp nigiri".
For checking it out we can use the function containsSubstring(string s) that is going to return true if the string s is cointained in the string that is calling it.
Example
It's common in the IB exam that they gave you the instructions of how a function works and they ask you to use it, like in this case containsSubstring().
To count how many nigiris we have we need to check if that word contains "nigiri". So let's do it. Remember that we want to output how many nigiris we have.
Now the tricky part. We want to count how many nigiris or makis we have in the menu (and output them).
We need to store the getNext value in a variable.
Rule of thumb: if you have an iteration and inside the loop you call getNext() twice or more times, you're probably doing it wrong (unless you're comparing pairs but it's unlikely). Use the temporary variable.
If you don't remember because you're stuck how to write the temporary variable you can do 2 loops.
I suggest to store the value from getNext even if it's going to be used once for safety. So the first counting (of only the nigiris) I'd write in an exam like this:
Let's say that we want to output how many students have their name that starts with J, L or M, using the method "startsWith()"
First attempt is wrong:
Second attempt is also wrong
Count how many people have a name that starts with L and ends with O
use startsWith and endsWith
We have a collection with the names of all of our employees called NAMES.
We want to know how many of them are called "Alexei" and how many of them are called "Alexander". We want to know which name is more common.
Posible
Two loops version
One loop version
Another one loop version courtesy of K.B
(last pages of the document I submitted in teams in January 2024, 27 to be precise)
Howgarts (not the magic school, the other magic school) offers diffent wizards clubs for their students. Each student must choose one of these clubs (it's mandatory)
These are the 5 collections where we're storing the wizards IDs. When a wizard student joins in, a troll adds it's ID to the collection.
PotionsAndExplotions
SetupWizards
Tennis
SpellingSpells
AncientForbiddenLanguagesAndLatin
For example this could be the content of the Collections at a certain given time
PotionsAndExplotions = {122504,5524,65521,58756,36652}
SetupWizards = {9996545,6654,2235,8887,555}
Tennis = {2555,366668}
SpellingSpells = { 1525456,22589,2114,6995,2255,22236,222578,1112}
AncientForbiddenLanguagesAndLatin = {659979,2164654,22247,23368,47798,5554}
The method isIn(X, COL) is available where
This method will return true if the ID number X is inside the collection COL and false otherwise.
For example
isIn(22589,SpellingSpells) returns true
isIn(6654, Tennis) returns false
Construct a method to implement isIn(X, COL)
Remember that we don't need to do for each specific club. Because we're going to have that specific club sent as a parameter.
Remember also that this is a collection. Not an array.
Other variations would be these
Remember that this part is about defining the function, not using it. That's why it starts with the name of the method and ends with the end method or end isIn. It's like when we're implemnting the 4 lines of void dot or dash
Next part of the exercise ask that it happens that some of the meetings of PotionsAndExplotions happens at the same time that SetupWizards and we have to construct a code that is going to tell how many of them are in both clubs so measures can be taken. (PotionsAndExplotions is expected to have preference but this is about internal school politics). Use the function isIn described before (in the exam they tell you that you need to use that function)
Solution:
The description of this algorithm is
We set up a counter to 0
Loop through the collection A, for each element in collection A check with isIn and collection B. If that returns true, then we upldate the counter adding 1. Else we keep looking through the collection A. When the loop is done we output the counter.
We can use the other collection also, here is another solution
Credit Y.C
Another variation is to count how many students are in Tennis, SpellingSpells and AncientForbiddenLanguagesAndLatin. In this case we need to use either a nested if solution or a composed statement because we want to know who are in the three of them at the same time.
Solution
Composite condition sollution
Nested if solution (the name of the long collection is wrong but you get it)
Credit Y.C
The academic director Magdalena MacGeneral wants to see if there is any student that has not chosen yet any of the clubs.
They store the names of the wizards students (that are 120) in an array called SNAMES and their ids in another parallel array called SIDS.
The exercise is to create an algorithm that will output the names of the students that has not been enrolled to any clubs and, if all the students are in at least one club, there should be an appropiate message. You may use the method isIn from before.
For doing this we're going to split this problem into several parts.
This would be the description.
If they ask for the code this would be the possible answer.
credit K.B.
credit V.G.
credit A.P.
There is another version creating an array of the collections.
Credit V.G.
You have to implement the exercise 23 from here:
https://hackmd.io/GOWuknHrTJSBs6NsxEeDOA#Flag-variables-while-looking-into-arrays
Suposing that the nationalDestinations are stored in a collection and not in an array.
You can also use temporal collections if you need (For counting destinations will be easier)
First exercise is using the array so it's the same.
Next exercise is finding if an specific flight is national or international. Here you have the solution
The next one about counting all the fights that are National.
credits to CXJ
Count how many diffent locations we have in the array.