diff --git a/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9.pdf b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9.pdf new file mode 100644 index 0000000..9056190 Binary files /dev/null and b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9.pdf differ diff --git a/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9.txt b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9.txt new file mode 100644 index 0000000..ef97f29 --- /dev/null +++ b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9.txt @@ -0,0 +1,52 @@ +AS +2H +2S +XS +6D +6C +JH +3D +XD +AD +JC +QC +9S +2C +7D +AH +4C +KH +2D +3C +7S +8S +JS +KC +8C +JD +5D +QD +9C +9D +5C +AC +4D +3H +KD +4S +6S +7H +6H +8H +XC +5H +3S +7C +XH +KS +QH +5S +9H +4H +8D +QS diff --git a/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9_Prof_Solution.pdf b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9_Prof_Solution.pdf new file mode 100644 index 0000000..851e7ee Binary files /dev/null and b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/Ex9_Prof_Solution.pdf differ diff --git a/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/ex9.c b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/ex9.c new file mode 100644 index 0000000..fdfb3ad --- /dev/null +++ b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/ex9.c @@ -0,0 +1,90 @@ +/* License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html + * + * Exercise 9 - Dynamic Programming: Crazy Eights (Week 10) + * Name: Manish + * Student Login: ***** + * + * Compile as: + * $ gcc -std=c11 -Wall -o ex9 ex9.c + */ + +#include +#include + +int max(int i, int j); + +int main(void) +{ + printf("Enter file name: "); + // Assuming filename/file path will not be longer than 256 characters + char filename[257]; + scanf("%s", filename); + + FILE* file = fopen(filename, "r"); + if (!file) + { + perror(filename); + exit(EXIT_FAILURE); + } + + // 0th index is dummy card + char cards[53][2]; + int size = 0; + int length[53] = {0}; // all get initialized to 0; + int parent[53] = {0}; // all get initialized to 0; + int max_leaf = 0; // index of last card of longest chain + + // Dummy card as first card + cards[size][0] = '8'; + cards[size++][1] = 'S'; + + // Read and add cards + while (fscanf(file, " %2c ", cards[size++]) != EOF) + { + } + + int current_length; + int current_parent; + for (int i=1; i < size; i++) + { + current_length = 1; + current_parent = 0; + for (int j = i-1; j >= 0; j--) + { + if (cards[i][0] == cards[j][0] + || cards[i][1] == cards[j][1] + || cards[i][0] == '8' + || cards[j][0] == '8' + ) + { + if (current_length < length[j]+1) + { + current_length = length[j]+1; + current_parent = j; + } + } + } + length[i] = current_length; + parent[i] = current_parent; + if (length[max_leaf] < current_length) + max_leaf = i; + } + + // find first card of longest chain + int first = max_leaf; + while (parent[first] != 0) + first = parent[first]; + + printf( + "Longest sequence length: %d\n" + "First card in longest sequence: %.2s\n" + "Last card in longest sequence: %.2s\n", + length[max_leaf], + cards[first], + cards[max_leaf] + ); + + fclose(file); + + return 0; +} diff --git a/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/makefile b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/makefile new file mode 100644 index 0000000..86e6889 --- /dev/null +++ b/Exercise 9 - Dynamic Programming: Crazy Eights (Week 10)/makefile @@ -0,0 +1,8 @@ +make: ex9.c + make ex9 && make ex9gnu11 + +ex9: ex9.c + gcc -Wall -std=c11 -o ex9 ex9.c + +ex9gnu11: ex9.c + gcc -Wall -std=gnu11 -o ex9gnu11 ex9.c && rm ex9gnu11