# Week 7 - Maps
## Team
Team name: Academy Award Winning, Choccy Milk Drinking, Cross Dressing SIMPS In Yugoslavia With Honda Accords
Date: 30/03/2022
yes(40%)
Members
Dawid: Dawid Swietlinski (518337)
Cristi: Cristian Cimpeanu (524091)
Mihnea: Mihnea Bastea (514541)
Vlad: Vladislav Serafimov(509761)
Steve: Stephen Cruz Wright(521476)
| Role | Name |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ |
| **Facilitator** keeps track of time, assigns tasks and makes sure all the group members are heard and that decisions are agreed upon. | Mihnea |
| **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings. | Vlad |
| **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. | Steve |
| **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data. | Dawid |
| **Additional** we had another person join our group this week so we have an additional space. We chose to make this member double check work done to assure quality in our work. | Cristi |
## 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
```c
int isalpha(int c);
//Checks if character is alphabetic
int isspace(int c);
//Checks if character is white-space
int ispunct(int c);
//Checks if character is punctuation
int islower(int c);
//Checks if character is lowercase
int isupper(int c);
//Checks if character is uppercase
int tolower(int c);
//converts uppercase letters to lowercase.
int toupper(int c);
//converts lowercase letters to uppercase.
```
### Activity 2: Count letters in a string
```c=
unsigned long countLetters(unsigned long counts[static 26], const char* str) {
unsigned long count = 0;
for (int i = 0; str[i] != '\0'; ++i) {
if (isalpha(str[i])){
//all this line does increment the letter
++counts[tolower(str[i]) - 'a'];
++count;
}
}
return count;
}
int main(void){
unsigned long counts[26] = {0};
const char* text = "The Quick Brown Fox Jumps Over The Lazy Dog.";
unsigned long total = countLetters(&counts[0], text);
assert(35 == total);
printCounts(&counts[0], false, true);
}
```
### Activity 3: Recognizing languages
`FIX MEEEEEEEEE`
```c=
bool is_in(int val, int *arr, int arr_size){
for (int i = 0; i < arr_size; ++i) {
if (arr[i] == val) return true;
}
return false;
}
const char* makeSignature(unsigned long counts[static 26]){
static char arr[7] = {0, 0, 0, 0, 0, 0, '\0'};
int sizes[7] = {-1, -1, -1, -1, -1, -1};
int index = 0;
//compare characters and find the current biggest
for (int i = 0; arr[5] == 0; ++i) {
for (int j = 0; j < 25; ++j) {
//if the number is the biggest yet and the letter has bot occurred yet
bool is_not_occurring = (arr[0] != j + 'a') ||
(arr[1] != j + 'a') ||
(arr[2] != j + 'a') ||
(arr[3] != j + 'a') ||
(arr[4] != j + 'a') ||
(arr[5] != j + 'a');
if ( is_not_occurring && !is_in(counts[j], sizes, 6) && counts[j] > counts[j-1]){
index = j;
sizes[i] = counts[j];
}
}
arr[i] = index + 'a';
printf("%c", arr[i]);
}
return &arr[0];
}
int main(void) {
unsigned long counts1[26] = {15,3,4,5,16,6,7,8,9,7,6,3,2,11,14,1,2,12,13};
unsigned long counts2[26] = {16,4,7,5,20,7,4,3,14,5,9,1,2,18,6,12,9,13,9,15};
assert(strcmp("eaosrn", makeSignature(counts1)) == 0);
assert(strcmp("enatir", makeSignature(counts2)) == 0);
}
```
can s1 figure out the second part
one of the functions they want us to call returns a struct and all the others use unsigned long arrays
| File | Signature | Language |
| ---------- | --------- | -------- |
| alice0.txt | | |
| alice1.txt | | |
| alice2.txt | | |
| alice3.txt | | |
### Activity 4: Find out: dictionaries around us
- Students/Student numbers
- Book content page
- Products/Product IDs
- Species/Their names
- Tigers/Their stripe patterns
### Activity 5: Find out: dictionaries in other languages
1. C#
- creating
```csharp
var d = new Dictionary<string, string>; //one method
```
- adding
```csharp
d.Add("a string", "another string")
```
- removing
```csharp
d.Remove("a key");
```
- retrieving
```csharp
string value = d["a key"];
```
2. Python
- creating
- adding
```python
d.get("a key")
```
- removing
```python
d.popitem() //no other way of removing things
```
3. Javascript XD
- creating
```javascript
var dict = new Object();
//or
var dict_alt = [];
```
- adding
```javascript
dict.push({
key: "keyName",
value: "the value"
});
```
- removing
```javascript
dict.push({
key: "a key",
value: "a value"
});
```
- retrieving
```javascript
var value = d["a key"];
```
### Activity 6: The ensureCapacity function
```c=
// Internal function, called to resize a counter_t if needed
bool _ensureCapacity(counter_t* counter, const size_t minimumCapacity){
size_t new_capacity = counter->capacity;
while (minimumCapacity > new_capacity){
new_capacity = new_capacity * GROWTH_FACTOR + 1;
}
if (new_capacity > counter->capacity){
if (realloc(counter->data, new_capacity) != NULL){
counter->capacity = new_capacity;
return true;
}
//in case of a bad realloc
free (counter->data);
}
return false;
}
```
### Activity 7: insertAt function
```c=
static bool _insertAt(counter_t* counter, const size_t index, const char* key, unsigned long value){
(void)index;// prevents unused-parameter error
(void)key;// prevents unused-parameter error
(void)value;// prevents unused-parameter error
pair_t new_pair;
new_pair.key = key;
new_pair.value = value;
if (_ensureCapacity(counter, counter->size + 1)) {
memmove(counter + index + 1, counter + index, counter->size - index);
counter->data[index] = new_pair;
++counter->size;
}
return false;
}
int main() {
counter_t counter;
initCounter(&counter, 0);
insert(&counter, "Bob", 3);
insert(&counter, "Dave", 4);
insert(&counter, "Alice", 5);
insert(&counter, "Eva", 3);
insert(&counter, "Charlie", 7);
assert(strcmp(counter.data[0].key, "Alice") == 0);
assert(counter.data[0].value == 5);
assert(strcmp(counter.data[1].key, "Bob") == 0);
assert(counter.data[1].value == 3);
assert(strcmp(counter.data[2].key, "Charlie") == 0);
assert(counter.data[2].value == 7);
assert(strcmp(counter.data[3].key, "Dave") == 0);
assert(counter.data[3].value == 4);
assert(strcmp(counter.data[4].key, "Eva") == 0);
assert(counter.data[4].value == 3);
assert(counter.size == 5);
assert(counter.capacity >= counter.size);
destroyCounter(&counter);
}
```
### Activity 8: Increment function
```c=
(void)counter;// prevents unused-parameter error
(void)key;// prevents unused-parameter error
unsigned long god_val;
if ((god_val = getOrDefault(counter, key, counter->size + 1)) == counter->size + 1){
if (insert(counter, key, 1)) return 1;
}
else{
return ++counter->data[god_val].value;
}
return 0;
int main(void) {
counter_t counter;
initCounter(&counter, 0);
increment(&counter, "Alice");
increment(&counter, "Alice");
increment(&counter, "Alice");
increment(&counter, "Bob");
increment(&counter, "Bob");
assert(counter.size == 2);
assert(counter.data[0].value == 3);
assert(counter.data[1].value == 2);
destroyCounter(&counter);
}
```
### Activity 9: Find out: function `strtok`
> `strtok(string, separators)` breaks down the string into substrings (tokens) based on certain separating characters. `string` is the string that needs to be broken down and `separators` is a string filled with the separating characters. The function returns a pointer to the first token
```c=
void processLine(counter_t* counter, char* line) {
//characters by which our string will be separated into tokens
const char* sep = "\t (),.;:?!\"\n'";
//get a pointer to the first token
char* token = strtok(line, sep);
//while there is something left in line
while (token) {
//if the string is not empty increment the token key in counter (or add it)
if (strlen(token)) increment(counter, token);
//get the next token
token = strtok(NULL, sep);
}
}
```
### Activity 10: How many words?
1. How many time are the following words occurring in the "alice0.txt" document: "Alice", "the", "rabbit"?
Alice: 404
the: 2526
rabbit: 6
2. How many unique words are there in the "alice0.txt" document?
26432
## Looking back
### What we've learnt
Language guessing.
Python dictionaries lowkey suck.
*highkey
### What were the surprises
Surprisingly little errors
### What problems we've encountered
All of them
### What was or still is unclear
`"%lu"` or `"%lul"` for unsigned longs?
### How did the group perform?
Yes