|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - datatypes/collections |
| 4 | +--- |
| 5 | + |
| 6 | +# Dictionary (Solutions) |
| 7 | + |
| 8 | +## Multiple Choices |
| 9 | + |
| 10 | +#. Put a checkmark in the box corresponding to true statements. |
| 11 | + |
| 12 | + - [x] Any datatype can be used for keys, but they tend to be "simpler" than the datatype used for values. |
| 13 | + - [ ] Two pairs (or `Cell`s) with the same key can be stored in a dictionary, provided they have different values. |
| 14 | + - [x] A collision occurs when two keys (or, in general, two pieces of data) have the same hash. |
| 15 | + - [x] Open addressing and (separate) chaining are two methods to resolve collisions. |
| 16 | + - [ ] Clustering is what makes dictionaries process data faster. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + <details> |
| 21 | + <summary>Comments on the solution</summary> |
| 22 | + |
| 23 | + - Note that a collision actually occurs when the keys have the same hash *modulo the array size* in our case. |
| 24 | + - Clustering is actually negative: it means that data is often stored in the same place in the dictionary, making it more computationally costly to find the data. |
| 25 | + </details> |
| 26 | + |
| 27 | +## Problem |
| 28 | + |
| 29 | +SDictionary_solution |
| 30 | + |
| 31 | +#. Consider the implementation of "simple" dictionary `SDictionary` below: |
| 32 | + |
| 33 | + ```{download="./code/projects/SDictionary.zip"} |
| 34 | + !include code/projects/SDictionary/SDictionary/SDictionary.cs |
| 35 | + ``` |
| 36 | + |
| 37 | + Remember that, for example, `"Bob"[0]` is `'B'` and use the correspondence below between characters and their integer representation to help you (i.e., `(int)'B'` is 66): |
| 38 | + |
| 39 | + | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | |
| 40 | + |----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----| |
| 41 | + | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | |
| 42 | + |
| 43 | + #. Fill the `table` array below after the following have been performed: |
| 44 | + |
| 45 | + ``` |
| 46 | + SDictionary friends = new SDictionary(11); |
| 47 | + friends.Add("Bob", null); |
| 48 | + friends.Add("Pete", null); |
| 49 | + friends.Add("Mary", null); |
| 50 | + friends.Add("Lora", null); |
| 51 | + ``` |
| 52 | + |
| 53 | + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
| 54 | + | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | |
| 55 | + | | | | | | | | | | | | |
| 56 | + |
| 57 | + <details> |
| 58 | + <summary>Solution</summary> |
| 59 | + |
| 60 | + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
| 61 | + | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | |
| 62 | + | "Bob" | "Mary" | `null` | "Pete" | `null` | `null` | `null` | `null` | `null` | `null` | "Lora" | |
| 63 | + |
| 64 | + The important point is to realize that |
| 65 | + |
| 66 | + Expression | Value |
| 67 | + --- | --- |
| 68 | + `"Bob"[0]` | `'B'` |
| 69 | + `(int)'B'` | `66` |
| 70 | + `66 % 11` | `0` |
| 71 | + `"Mary"[0]` | `'M'` |
| 72 | + `(int)'M'` | `77` |
| 73 | + `77 % 11` | `0` |
| 74 | + |
| 75 | + So "Bob" and "Mary" are both stored at index 0. "Bob" is inserted first at index `0`, and since the `Add` method use linear open addressing, "Mary" is stored at the next available index, `1` in this case. |
| 76 | + </details> |
| 77 | + |
| 78 | + |
| 79 | + #. What would happen if `friends.Add("Lora", null);` was executed again? Is it what is expected from a dictionary? |
| 80 | + |
| 81 | + <details> |
| 82 | + <summary>Solution</summary> |
| 83 | + "Lora" would be inserted at index `2`: `10`, `0` and `1` being taken, `Add` goes to the next available index, `2`. This is not expected, since a dictionary should reject |
| 84 | + |
| 85 | + #. Write a `ToString` method for the `SDictionary` class, that returns a `string` containing all the keys and values stored in the dictionary. |
| 86 | + |
| 87 | + <details> |
| 88 | + <summary>Solution</summary> |
| 89 | + ```{download="./code/projects/SDictionary_solution.zip"} |
| 90 | + !include`snippetStart="// ToString solution:", snippetEnd="// End of ToString solution."` code/projects/SDictionary_solution/SDictionary/SDictionary.cs |
| 91 | + ``` |
| 92 | + </details> |
| 93 | + |
| 94 | + #. What would happen if we were to try to insert 12 elements in our `friends` object? |
| 95 | + |
| 96 | + <details> |
| 97 | + <summary>Solution</summary> |
| 98 | + When trying to insert the 12th element in the array of size 11, `Add` would loop forever, always circling through the array, looking for a cell containing `null`, while none are left. |
| 99 | + </details> |
| 100 | + |
| 101 | + #. Consider the following `Delete` method: |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + Complete the series of instructions below such that `demo.Delete(error)` would return `false` even though the string `error` *is* the key of a value present in the `demo` dictionary object. |
| 106 | + |
| 107 | + ``` |
| 108 | + class Program{ |
| 109 | + static void Main(){ |
| 110 | + SDictionary demo = new SDictionary( ); // Complete it. |
| 111 | + |
| 112 | + string error = // Fill me |
| 113 | + // To be completed. |
| 114 | + Console.WriteLine($"{error} was in demo: {demo.Delete(error)}."); |
| 115 | + } |
| 116 | + } |
| 117 | + ``` |
| 118 | + |
| 119 | + <details> |
| 120 | + <summary>Solution</summary> |
| 121 | + The solution is to be in a position where the `error` value is "hidden after" a `null` value: |
| 122 | + |
| 123 | + ```{download="./code/projects/SDictionary_solution.zip"} |
| 124 | + !include`snippetStart="// Exhibiting Delete incorrect behavior:", snippetEnd="// Done: the program will believe that "Alex" is not"` code/projects/SDictionary_solution/SDictionary/SDictionary.cs |
| 125 | + ``` |
| 126 | + </details> |
0 commit comments