# Week 7 - Maps ## Team Team name: Error Date:5/4/2022 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. |Thanh 516467 | | **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings. |Minh 511907 | | **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. |Kaloyan 511216 | | **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data. |Hoa 487272 | ## 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 1. isalnum() This function checks whether character is alphanumeric or not 2. isalpha() This function checks whether character is alphabet or not 3. isdigit() This function checks whether character is digit or not 4. isspace() This function checks whether character is space or not 5. isupper() This function checks whether character is uppercase character or not 6. islower() This function checks whether character is lowercase character or not 7. ispunct() This function checks whether character is a punctuation character or not 8. isprint() This function checks whether character is printable or not 9. toupper() This function return character in upper case 10. tolower() this function return character in lower case ### Activity 2: Count letters in a string ```c unsigned long countLetters(unsigned long counts[static 26], const char* str){ // TODO: implement counting letters (Activity 2) (void)counts; (void)str; unsigned long count = 0; for(int i=0; i<26; i++){ counts[i]=0; } char *lower = malloc(strlen(str)+1); for (int i=0; i < (int)strlen(str); i++){ lower[i] = tolower(str[i]); } for (int j=0; j < (int)strlen(str); j++){ if(isalpha(lower[j])){ counts[lower[j] - '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 ```c const char* makeSignature(unsigned long counts[static 26]) { static char signature[7]; // TODO: implement making a language signature based on the letter counts (Activity 3) (void)counts; signature[6] = '\0'; unsigned long* sorted_counts = malloc(sizeof (unsigned long) * ALPHABET_SIZE); const size_t shift = sortedCounts(counts, sorted_counts); for (int i = 0; i < 7; ++i) { char letter = (char)(sorted_counts[i] >> shift); signature[i]=letter; //unsigned int count = (unsigned int)((sorted_counts[i] << 8) >> 8); } free(sorted_counts); return &signature[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); } ``` | File | Signature | Language | | ---------- | --------- | -------- | | alice0.txt | etaoinh |en | | alice1.txt |iantesl | fi | | alice2.txt |eaionrt |it | | alice3.txt |enisrat |de | ### Activity 4: Find out: dictionaries around us Some examples: 1. Dictionary: “key” in here is a word in one language and “value” is a word in another language. 2. Telephone directory: “key” in here is the numbers, “value” is person name 3. Student number: “key” in here is numbers, “value” is student name 4. Hash map: “key” is password, “value” is the hash. 5. Employee number: “key” is number, “value” is employee name ### Activity 5: Find out: dictionaries in other languages Dictionary type in other language 1. Java: Hashtable 2. C++: map 3. Ruby: hash 4. Python: dict Retrieve value: get() Add key-value pair: update() Delete key: pop() Check if the key exist: has_key() 5. C#: Dictionary Add the key-value pair: Add() Delete key: Remove() Retrieve value: [] Check if the key exist: ContainsKey() ### Activity 6: The ensureCapacity function ```c bool _ensureCapacity(counter_t* counter, size_t minimumCapacity) { pair_t *tmp = (realloc(counter->data, new_capacity * sizeof(pair_t))); if (tmp) { *counter = (counter_t) {.data=tmp, .capacity=new_capacity, .size=counter->size}; return true; } } else { return true; } ``` ### Activity 7: insertAt function ```c bool _insertAt(counter_t* counter, const size_t index, const char* key, unsigned long value) { memmove(counter->data + index + 1, counter->data + index, (counter->size - index) * sizeof(pair_t)); counter->data[index] = makePair(key, value); ++counter->size; return true; } 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 unsigned long increment(counter_t* counter, const char* key) { if (key && counter) { size_t ret = getOrDefault(counter, key, 0); insert(counter, key, ret + 1); return ret + 1; } } 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` The strtok() function divides the text into separate tokens with a given character to be the delimiters. Given the string “Help me! I will fail Alg and DS” and the delimiter is set to be “e” the string after the function call will be like follows: “H lp m ! I will fail Alg and DS”. It removes the character that is set to be the delimiter and leaves the rest without that character. Explanation of the code: ```c void processLine(counter_t *counter, char *line) { // separator characters const char *sep = "\t (),.;:?!\"\n'"; //initialize a char pointer to the return value of strtok() function char *token = strtok(line, sep); //while the token separates the line string while (token) { //if the length of token is not 0 if (strlen(token)) //inserts the separated word as a key in the dictionary increment(counter, token); //deletes the separator character token = strtok(NULL, sep); } ``` ### Activity 10: How many words? ```c counter_t count; count=ProcessFile("../alice0.txt"); unsigned long count1=increment(&count,"Alice"); unsigned long count2=increment(&count,"the"); unsigned long count3=increment(&count,"rabbit"); printf("%zu\n", count.size); printf("%lu\n", count1); printf("%lu\n", count2); printf("%lu\n", count3); destroyCounter(&count); ``` 4057 386 1685 3 ## Looking back ### What we've learnt The importance of key-value pairs in real life. How to implement our own dictionary or hash-map. How to use some built-in library in C++. ### What were the surprises Nothing ### What problems we've encountered There are some problems for us to understand the compare function in activities 2 and 3. We also have some problems with assert function ### What was or still is unclear Nothing ### How did the group perform? As good as usual > Written with [StackEdit](https://stackedit.io/).