shithub: util

Download patch

ref: a6ed04f41557953cd022f331ff5dfdb28e05ad2e
parent: 8e93f40014ffa6fed9eb77f7dc975f9178486f1e
author: eli <eli@singularity>
date: Thu May 9 23:05:57 EDT 2024

anagrams update

--- a/anagrams.c
+++ b/anagrams.c
@@ -9,6 +9,13 @@
 	int nchildren;
 } item;
 
+typedef struct _dlist {
+	char *word;
+	struct _dlist *next;
+} dlist;
+
+dlist *dict = nil;
+
 void
 removechars(char *input, char *word)
 {
@@ -49,22 +56,13 @@
 }
 
 void
-findanagrams(char *words, item *items, int minlen)
+findanagrams(item *items, int minlen)
 {
-	FILE *wordsfile = fopen(words, "r");
-	char word[8192];
 	item *child;
+	dlist *cur = dict;
 
-	if (wordsfile == nil)
-		exits("fopen");
-
-	while(fgets(word, 8192, wordsfile) != nil){
-		word[strcspn(word, "\n")] = '\0';
-
-		if (strlen(word) < minlen)
-			continue;
-
-		if(contains(items->input, word)){
+	while(cur->word != nil){
+		if(contains(items->input, cur->word)){
 			child = calloc(1, sizeof(item));
 			child->input = strdup(items->input);
 			child->anagram = strdup(items->anagram);
@@ -74,18 +72,18 @@
 			items->children[items->nchildren] = child;
 			items->nchildren++;
 
-			removechars(child->input, word);
-			child->anagram = realloc(child->anagram, strlen(child->anagram) + strlen(word) + 2);
-			sprintf(&child->anagram[strlen(child->anagram)], "%s ", word);
+			removechars(child->input, cur->word);
+			child->anagram = realloc(child->anagram, strlen(child->anagram) + strlen(cur->word) + 2);
+			sprintf(&child->anagram[strlen(child->anagram)], "%s ", cur->word);
 
 			if (strlen(child->input) == 0)
 				printf("%s\n", child->anagram);
 
-			findanagrams(words, child, minlen);
+			findanagrams(child, minlen);
 		}
-	}
 
-	fclose(wordsfile);
+		cur = cur->next;
+	}
 }
 
 void
@@ -94,7 +92,11 @@
 	char input[8192];
 	item *items;
 	int minlen = 0;
-	char *wordsfile;
+	char *wordsfilename;
+	FILE *wordsfile;
+	char word[8192];
+	dlist *cur;
+	item *child;
 
 	if (argc < 2) {
 USAGE:
@@ -102,7 +104,7 @@
 		exits("usage");
 	}
 
-	wordsfile = argv[1];
+	wordsfilename = argv[1];
 
 	if (strcmp(argv[1], "-l") == 0) {
 		if (argc < 4)
@@ -109,18 +111,58 @@
 			goto USAGE;
 
 		minlen = atoi(argv[2]);
-		wordsfile = argv[3];
+		wordsfilename = argv[3];
 	}
 
+	wordsfile = fopen(wordsfilename, "r");
+	if (wordsfile == nil)
+		exits("fopen");
+
 	read(0, input, sizeof(input));
 	input[strcspn(input, "\n")] = '\0';
 	while(contains(input, " "))
 		removechars(input, " ");
 
+	dict = calloc(1, sizeof(dlist));
+	cur = dict;
+	while(fgets(word, 8192, wordsfile) != nil){
+		word[strcspn(word, "\n")] = '\0';
+
+		if (strlen(word) < minlen)
+			continue;
+
+		if(contains(input, word)){
+			cur->word = strdup(word);
+			cur->next = calloc(1, sizeof(dlist));
+			cur = cur->next;
+		}
+	}
+	fclose(wordsfile);
+
 	items = calloc(1, sizeof(item));
 	items->input = strdup(input);
 	items->anagram = strdup("");
 	items->children = calloc(1, sizeof(item*));
 
-	findanagrams(wordsfile, items, minlen);
+	while(dict->word != nil){
+			child = calloc(1, sizeof(item));
+			child->input = strdup(items->input);
+			child->anagram = strdup(items->anagram);
+			child->children = calloc(1, sizeof(item*));
+
+			items->children = realloc(items->children, (items->nchildren+1) * sizeof(item*));
+			items->children[items->nchildren] = child;
+			items->nchildren++;
+
+			removechars(child->input, dict->word);
+			child->anagram = realloc(child->anagram, strlen(child->anagram) + strlen(dict->word) + 2);
+			sprintf(&child->anagram[strlen(child->anagram)], "%s ", dict->word);
+
+			if (strlen(child->input) == 0)
+				printf("%s\n", child->anagram);
+			else
+				findanagrams(child, minlen);
+
+			dict = dict->next;
+	}
 }