---
# System prepended metadata

title: Week 6 - Dictionaries

---

# Week 6 - Dictionaries

## Team

Team name: **Group [497441]**
Date: 01/08/2021

Members: Max Thielen

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

## 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++){
        counts[tolower(str[i]) - 'a']++;
    }
    return *counts;
}
```

### Activity 2: Recognizing Languages
```c=
const char* makeSignature(unsigned long counts[static 26]){
    static char buffer[7];

    for(int i=0; i<6; i++){
        int biggest_index = 0;
        for(int j=1; j<ALPHABET_SIZE; j++){
            if(counts[biggest_index]<counts[j]){
                biggest_index=j;
            }
        }
        buffer[i] = biggest_index + 'a';
        counts[biggest_index]=0;
    }

    buffer[6] = '\0';
    return &buffer[0];
}
```

### Activity 3:  Dictionaries Around Us

* each phone number is mapped to person
* each adress is mapped to a landlord
* each city is mapped to a country
* each class is mapped to a teacher
* each book is to an author

### Activity 4:  Dictionaries in other Languages

* **Java's Hashtable:**
  * Hashtable<type, type> dict = new Hashtable<type, type>(); 
  * dict.put(key,val); 
  * dict.remove(key); 
  * dict.get(key); 
* **Python's Dictionary:**
  * dict = {'jack': 4098, 'sape': 4139}
  * dict['guido'] = 4127
  * del dict['sape']
  * dict['jack']
* **C#'s Dictionary/Hashtable**
* **Rust's HashMap**
* **R's RDict**
### 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){
       counter = (Counter*) realloc(counter, sizeof(Counter*) * new_capacity);
        counter->capacity = new_capacity;
       if(counter!=NULL){
           return true;
       }
       else return false;
    }
    return true;
}
```

### Activity 6: insertAt Function

```c=
static _Bool _insertAt(Counter counter[static 1], const int index,  const char* key, const unsigned long value){
    if (_ensureCapacity(counter, counter->size + 1)) {
        Pair new_entry = makePair(key, value);
        if(counter->size>0) memmove(&counter->data[index + 1], &counter->data[index], sizeof(Pair[counter->size - index]));
        counter[index].data = &new_entry;
        counter->size++;

        return true;
    }
    return false;
}
```

### Activity 7: increment Function

```c=
unsigned long increment(Counter counter[static 1], const char* key){
    KeyIndex index = indexOf(counter, key);
    if (index.found) {
        return ++counter[index.index].data->value;
    }
    else{
        _insertAt(counter, index.index, key, 1);
        return 1;
    }
    return 0;
}
```

### Activity 8: strtok Function
The **strok** seperates the char array pointer 'line' given as an argument based on the established 'sep' chars in the first line. The token is an individual word from the line and as long as the line has words it calls the increment function with the token then sets the token to NULL.


### Activity 9: How many Words

I was not able to properly debug my program and ended with 'SIGSEGV (Segmentation fault)' once adding E-Book to the counter. Frustrated and tired i surcame to failure :( 


## Look back

### What we've learnt

This week I learned about alot about parsing through a word document and couting the individual letters and words. The letters can be stored in an array however the words need a dictionary since we dont know the amount of unique words within the document. A dictionary and link a key(the word) to the number of occurences within a data strucutre.

### What were the surprises

The fact that the language a text is writen in can be determined by the five most common letters used in the text. Thats pretty cool!

### What problems we've encountered

KeyIndex structure was pretty confusing and hard to debug...

### What was or still is unclear

Please tell me I made a stupid mistake in one of the three functions I had to edit because i almost lost my mind trying to run debugger over and over again.

### How did the group perform?

Gud.