# Week 7 - Dictionaries
## Team
>Members:
>
>- Member one Dominik Huspeka
>- Member two Bianka Tomasčíková
>- Member three Alex Zab
>
> Date: *day* *month* 2023 | |
## Activities
Make sure to have the activities signed off regularly to ensure progress is tracked.
Download the provided project and open it in CLion.
### Activity 1: The ctype header file
int isalpha(int c) - check if its alphabetic
int islower(int c) - check if lowercase
int isupper(int c) - check if uppercase
int tolower() - checks and converts to lower
int toupper() - checks and converts to upper
### Activity 2: Count letters in a string
```c
unsigned long count_letters(unsigned long counts[static 26], const char* str) {
if (counts == NULL || str == NULL)
return 0;
unsigned long total = 0;
for (; *str != '\0'; ++str) {
if (isalpha(*str)) {
unsigned int index = tolower(*str) - 'a';
counts[index]++;
total++;
}
}
return total;
}
```
### Activity 3: Recognizing languages
```c
const char* make_signature(const unsigned long counts[static 26]){
unsigned long index[6] = {0};
unsigned long temp_counts[26];
unsigned long i, j, temp, temp_index;
for (i=0; i < 26; i++){
temp_counts[i] = counts[i];
}
temp = 0;
for (i=0; i < 6; i++){
for(j = 0; j < 26; j++){
if (temp < temp_counts[j]){
temp = temp_counts[j];
temp_index = j;
}
}
index[i] = temp_index;
temp = 0;
temp_counts[temp_index]=0;
}
static char buffer[7];
for (i = 0; i < 6; i++) {
buffer[i] = 'a' + index[i];
}
buffer[6] = '\0';
return &buffer[0];
}
```
| File | Signature | Language |
| ---------- | --------- | -------- |
| alice0.txt | etaoin | en |
| alice1.txt | iantes | fi |
| alice2.txt | eaionr | it |
| alice3.txt | enisra | de |
### Activity 4: Find out: dictionaries in other languages
-C++: std::map
• insert a key-value pair: insert() or emplace()
• delete a key: erase()
• check if a key exists: find()
• retrieve the value associated with a key: operator[reference link]
-C#: dictionary
• insert a key-value pair: Add(key, value)
• delete a key: dictionary.Remove(key)
• check if a key exists: dictionary.ContainsKey(key)
• retrieve the value associated with a key: dictionary[key]
-Python: dict
-Java: HashMap
-JavaScript: Map
### Activity 5: Generic sorting in C
```c
int compare_strings(const void* a, const void* b) {
const char** str_a = (const char**)a;
const char** str_b = (const char**)b;
return strcmp(*str_a, *str_b);
}
int main(void) {
const char* strings[] = {"Spam", "Cheese", "Knights", "Holy Grail", "Lumberjack", "Ministry", "Swallow",
"Silly", "Black Knight", "Camelot", "Coconut", "Parrot", "Shrubbery", "Taunt", "Argument"};
size_t num_strings = sizeof(strings) / sizeof(const char*);
printf("Before sorting:\n");
print_string_array(strings, num_strings);
qsort(strings, num_strings, sizeof(const char*), compare_strings);
printf("\nAfter sorting:\n");
print_string_array(strings, num_strings);
return 0;
}
```
**The output is:**
[Argument, Black Knight, Camelot, Cheese, Coconut, Holy Grail, Knights, Lumberjack, Ministry, Parrot, Shrubbery, Silly, Spam, Swallow, Taunt]
### Activity 6: Generic searching in C
```c
print_string_array(strings, sizeof(strings) / sizeof(const char*));
const char * strings_search[] = {"Holy Grail", "Parrot", "Rabbit"};
for (int i = 0; i < 3; i++){
const char* str_search = strings_search[i];
bsearch_result_t result = binary_search(&str_search,strings, sizeof(strings) / sizeof(const char*), sizeof(const char*), compare_strings);
if (result.found){
printf("%s is at index %zu \n", str_search, result.index);
}
else{
printf("%s not found. \n", str_search);
}
}
```
**The output is:**
Holy Grail is at index 5
Parrot is at index 9
Rabbit not found.
### Activity 7: Counter - function implementations
```c
size_t counter_get_count(const counter_t *counter, const char *string) {
const pair_t *found_pair = (const pair_t *)counter_get_pair_at(counter, 0);
for (size_t i = 0; i < counter->count; i++) {
if (strcmp(found_pair[i].key, string) == 0) {
return found_pair[i].value;
}
}
return 0;
}
void counter_increment(counter_t *counter, const char *string) {
for (size_t i = 0; i < counter->count; ++i) {
pair_t *found_pair = (pair_t *) counter_get_pair_at(counter, i);
if (counter->cmp_fun(found_pair[i].key, string) == 0) {
found_pair[i].value++;
return;
}
}
pair_t new_pair = make_pair(string);
set_add(counter, &new_pair);
}
const pair_t * counter_get_pair_at(const counter_t *counter, size_t index) {
return (const pair_t *)counter->data + index;
}
counter_t *counter_init(counter_t *counter, size_t capacity) {
return set_init(counter, capacity, sizeof(pair_t), (compare_fun_t)strcmp);
}
void counter_destroy(counter_t *counter) {
set_destroy(counter);
}
```
### Activity 8: Find out: function `strtok`
It takes input, breaks it down, and replaces it by a null pointer.
```c
void process_line(counter_t* counter, char* line) { // creates function with 2 parameters
const char* sep = "\t (),.;:?!\"\r\n'_-*"; // we define a char named sep
char* token = strtok(line, sep);
while (token) { // while loop when token is active
if (strlen(token) > 0) process_word(counter, token); // if statement, which says that strlen(token) must be higher than 0 to call function with pointer counter
token = strtok(NULL, sep); //calls strtok function with null pointer which is now equal to token
}
}
```
### Activity 9: How many words?
1. How many times do the following words occur in the "alice0.txt" document: "Alice", "the",
"rabbit"?
Alice: 401
the: 1691
rabbit: 5 (but Rabbit: 47)
2. How many unique words are there in the "alice0.txt" document?
3713
### Activity 10: Most frequent words
```c
counter_t new_counter;
memcpy(&new_counter, &counter, sizeof(counter_t));
qsort(new_counter.data, new_counter.count, new_counter.item_size, compare_pairs);
pair_t* pairs = (pair_t*)new_counter.data;
for (size_t i = 0; i < 10; i++) {
printf("%zu: %s (%lu)\n", i + 1, pairs[i].key, pairs[i].value);
}
```
The output of the program is:
1: the (1691)
2: and (817)
3: to (797)
4: a (672)
5: of (610)
6: it (523)
7: she (508)
8: said (458)
9: in (418)
10: you (402)
## Looking back
### What we've learnt
### What were the surprises
### What problems we've encountered
### What was or still is unclear
### How did the group perform?