diff --git a/Commissions/flis/IMT/Case3/.envrc b/Commissions/flis/IMT/Case3/.envrc new file mode 100644 index 0000000..17d6464 --- /dev/null +++ b/Commissions/flis/IMT/Case3/.envrc @@ -0,0 +1 @@ +use_nix \ No newline at end of file diff --git a/Commissions/flis/IMT/Case3/case3 b/Commissions/flis/IMT/Case3/case3 new file mode 100755 index 0000000..9d7d7a1 Binary files /dev/null and b/Commissions/flis/IMT/Case3/case3 differ diff --git a/Commissions/flis/IMT/Case3/case3.c b/Commissions/flis/IMT/Case3/case3.c new file mode 100644 index 0000000..bc5f736 --- /dev/null +++ b/Commissions/flis/IMT/Case3/case3.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include + + +struct Node { + struct Node* children[10]; + bool isEnd; +}; + +struct Node* newNode(void) { + struct Node *nNode = NULL; + nNode = (struct Node *) malloc(sizeof(struct Node)); + if (nNode) { + nNode->isEnd = false; + for (int i = 0; i < 10; i++) { + nNode->children[i] = NULL; + } + } + return nNode; +} + +int numbers[] = {113, 11354, 77777, 255044}; + +int main(void) { + struct Node* root = newNode(); + printf("%p\n", root); + + int n_numbers = sizeof(numbers)/sizeof(int); + + for (int i = 0; i < n_numbers; i++) { // Go through all the phone numbers in the global list + struct Node* pointer = root; // Create a pointer that points to whatever part of the trie we're working on + char number[9]; // A number has 8 + \0 chars + snprintf(number, 9, "%i", numbers[i]); // Convert int to string + int len_number = strlen(number); + for (int j = 0; j < len_number - 1; j++) { // loop over chars in the phone number + if (j == len_number - 1) { // if we are at the last iteration + if (pointer->isEnd == true) { // Check if we've seen the number before + printf("Duplicate Number found at element %i, number %s", i, number); + break; + } + else { // if we're at the last iteration, and isEnd isnt true, its either a new number OR we found a prefix + bool continues = false; + for (int k = 0; k < 10; k++) { + if( pointer->children[k] != NULL) { + continues = true; + }; + }; + if (continues == true) { + printf("FOUND PREFIX IN NUMBER\n"); + printf("%s and %s", number, number); + while (pointer->isEnd != true) { // follow the first paths you can find + for (int k = 0; k < 10; k++) { + if( pointer->children[k] != NULL) { + pointer = pointer->children[k]; + printf("%c", k + '0'); + break; + }; + }; + }; + printf("\n"); + return 0; + }; + }; + pointer->isEnd = true; + } + else if (pointer->isEnd == true) { // if we've already seen a shorter number with this prefix + printf("Found prefix!"); + for (int k = 0; k < j; k++) { + printf("%c", number[k]); + } + printf("\n"); + }; + int index = number[j+1] - '0'; + pointer->children[index] = newNode(); + pointer = pointer->children[index]; + }; + }; +} \ No newline at end of file diff --git a/Commissions/flis/IMT/Case3/shell.nix b/Commissions/flis/IMT/Case3/shell.nix new file mode 100644 index 0000000..d364c29 --- /dev/null +++ b/Commissions/flis/IMT/Case3/shell.nix @@ -0,0 +1,5 @@ +with import {}; +stdenv.mkDerivation { + name = "c-shell"; + buildInputs = [ gcc gnumake ]; +}