# Week 7 - Maps ## Team Team name: Peach Technologies Date: 07/12/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. | Nafiz Redwan 515199 | | **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings. | Husam Kanoa 512881 | | **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. | Yazan Tayeh 473427 | | **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data. | Rimma Davletova 508350 | ## 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) ``` This function checks whether the passed character is alphabetic. ```c= int islower(int c) ``` This function checks whether the passed character is lowercase letter. ```c= int isupper(int c) ``` This function checks whether the passed character is an uppercase letter. ```c= int tolower(int c) ``` This function converts uppercase letters to lowercase. ```c= int toupper(int c) ``` This function converts lowercase letters to uppercase. ### 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; *(str + i)!= '\0'; i++){ if(isalpha(*(str + i))){ 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 ```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'; int index=0; for(int i =0; i<26; i++){ int max = counts[i]; int maxIndex = i; for(int j =0; j<26; j++){ if((int)counts[j]>max){ max = counts[j]; maxIndex = j; } } signature[index] = (char)tolower(maxIndex)+'a'; counts[maxIndex] =0; index++; if (index == 6){ break; } } 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 | yutxzw | pl | | alice1.txt | vwytux | nl | | alice2.txt | vtuxws | en | | alice3.txt | wxzyut | en | ### Activity 4: Find out: dictionaries around us Students stored by student ID in a school system Website users stored by Email Supermarket items stored by barcode number Electronics store items stored by serial number Swapfiets bikes stored by barcode number in the system ### Activity 5: Find out: dictionaries in other languages Python: ```python= Dic = dict() ``` C#: ```c= var myDict = new Dictionary<string, string> ``` Java: ```java= Map<String,ArrayList<String>> lst = new HashMap<String,ArrayList<String>>(); ``` Javascript ```javascript= var dict = new Object(); //insert Key-value pair obj.key3 = "value3"; //delete a key delete obj.key2; //check if key exists obj.hasOwnProperty("key"); //retrieve the value associated with a key obj.someKey; ``` PHP ```php= $foo = new StdClass(); //insert Key-value pair $foo->bar = '1234'; //delete a key unset($a->some_key); //check if key exists property_exists($object, "some_key") //retrieve the value associated with a key $obj->some_key ``` ### Activity 6: The ensureCapacity function ```c bool _ensureCapacity(counter_t* counter, 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){ // TODO: implement resizing (Activity 6) pair_t * new_data = realloc(counter->data, new_capacity); if(new_data){ counter->data = new_data; counter->capacity = new_capacity; return true; }else{ return false; } } 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 if (_ensureCapacity(counter, counter->size + 1)) { // TODO: implement insertion as described in activity 7 pair_t p = makePair(key,value); memmove(counter->data,counter->data,(counter->size-1) -index); counter->data[index] = p; counter->size++; return true; } 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); } ``` Record your answer here ### Activity 8: Increment function ```c unsigned long increment(counter_t* counter, const char* key) { // TODO: implement the increment function as described in Activity 8 // TIP: you can use the existing functions to do it // or implement an own version (that's potentially a bit faster) (void)counter;// prevents unused-parameter error (void)key;// prevents unused-parameter error long val = getOrDefault(counter,key,-1); if(val == -1){ insert(counter,key,1); return 1; }else{ counter->data[indexOf(counter,key).index].value++; return counter->data[indexOf(counter,key).index].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` function ```char *strtok(char *str, const char *delim)``` breaks string str into a series of tokens using the delimiter delim. ### Activity 10: How many words? Alice: 393 the: 1549 rabbit: 3 Unique words: 4385 ```c= int main() { // TODO: your code for activity 10 goes in here counter_t counter = processFile("alice0.txt"); if(contains(&counter,"Alice")){ long aliceCount0 = getOrDefault(&counter,"Alice",-1); printf("%d",(int) aliceCount0); } if(contains(&counter,"the")){ long aliceCount1 = getOrDefault(&counter,"the",-1); printf("%d", (int)aliceCount1); } if(contains(&counter,"rabbit")){ long aliceCount2 = getOrDefault(&counter,"rabbit",-1); printf("%d", (int)aliceCount2); } int wCount = 0; for(int i=0; i< (int)counter.size;i++){ if(counter.data[i].value==1){ wCount++; } } printf("%d", (int)wCount); return 0; } ``` ## Looking back ### What we've learnt Formulate at least one lesson learned. ### 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? > Written with [StackEdit](https://stackedit.io/).