---
# System prepended metadata

title: Week 6 - Dictionaries

---

# Week 6 - Dictionaries

## Team

Team name:
Date:

Members

| Role                                                                                                                                                                          | Name |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|
| **Facilitator** keeps track of time, assigns tasks and makes sure all the group members are heard and that decisions are agreed upon.                                             |Mihail   |
| **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings.                                              |Mihail      |
| **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. |Mihail      |
| **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data.                                                   |Mihail      |

## Activities

### Activity 1: count letters in a string

```c=
unsigned long countLetters(unsigned long counts[static 26], const char* str){
    for (int i = 0; i<strlen(str); i++){
        if(isalpha(str[i])){
            int letter = tolower(str[i]) - 'a';
            counts[letter]++;
        }
    }
    return counts;
}
```

### Activity 2: recognizing languages

The implementation of makeSignature():
```c=
const char* makeSignature(unsigned long counts[static 26]){
    static char signature[7];
    int max_idx = 0;
    for(int i = 0; i<7; i++) {
        // write the implementation
        for (int j = 0; j < 26; j++) {
            if(counts[j]>counts[max_idx]){
                max_idx = j;
            }
        }
        signature[i] = max_idx + 'a';
        counts[max_idx] = 0;
    }

    signature[6] = '\0'; // null terminator
    return &signature[0];
}
```
Unfortuantely the results I got from the match language function were not correct. The result I got was correct for the first file but incorrect for the rest.

### Activity 3: Find out: dictionaries around us

Real life examples of key-value relationships can be:
* A website database that links users to their account
* A donation ledger linking a person's name with the amount they donated
* A price sheet linking a product with its price
* An ID verification system linking a person with their ID number
* A key that is inherantly linked with the door that it unlocks

### Activity 4: Find out: dictionaries in other languages

* Python has a data structure called exactly dictionary. It looks like this:
``` python=
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
```
Python allows the use of the method update() to add a key-value combination. It looks like this.
```python=
thisdict.update({"color": "White"})
```
It also allows the use of the method pop() to remove a key-value combination. An example is:
```python=
thisdict.pop("model")
```
The get() method allows you to retrive the value if the key specified exists:
```python=
x = thisdict.get("year")
```
* In C# the class Dictionary can be used to easily create a dictionary, like so:
```csharp=
//creating a dictionary of strings with string keys
Dictionary<string, string> openWith = new Dictionary<string, string>();
```
The add() function can be used to add elements to the dictionary. The function can be used like this: 
```csharp=
openWith.Add("dib", "paint.exe");
```
The remove() function can be used like this:
``` csharp=
Console.WriteLine("\nRemove(\"key\")");
openWith.Remove("key");

if (!openWith.ContainsKey("key"))
{
    Console.WriteLine("Key \"key\" is not found.");
}
```
To get the value of a key we can use the function TryGetValue(). An example of the function can be seen here:
``` csharp=
string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
```
* Dictionaries in Java are called Maps
* The same as C JavaScript also doesn't have a premade dictionary data structure.
* In Swift a dictionary is declared like this:
```swift=
@frozen struct Dictionary<Key, Value> where Key : Hashable
```
### Activity 5: ensureCapacity function

```c=
 _Bool _ensureCapacity(Counter counter[static 1], const int minimumCapacity){
    int new_capacity = counter->capacity;
    if (new_capacity == 0){
        new_capacity = DEFAULT_CAPACITY;
    }
    while (minimumCapacity > new_capacity){
        new_capacity *= GROWTH_FACTOR;
    }

    if (new_capacity > counter->capacity){
       // TODO: implement resizing
        counter->data = realloc(counter->data, sizeof(Pair *) * new_capacity);
        counter->capacity = new_capacity;
        return true;
    }
    return false;
}
```

### Activity 6: insertAt function

Record your answer here

### Activity 7: increment function

And that's how you insert code blocks:

```c=

double *number = malloc(sizeof(double));


```

### Activity 8: function strtok

### Activity 9: how many words

## Look back

### What we've learnt

Fill in...

### What were the surprises

Fill in...

### What problems we've encountered

Fill in...

### What was or still is unclear

Fill in...

### How did the group perform?

How was the collaboration? What were the reasons for hick-ups? What worked well? What can be improved next time?
