c# - Searching if an array of strings contain an array if char's -
currently fiddling little project i'm working on, it's count downwards type game (the tv show). currently, programme allows user pick vowel or consonant limit of 9 letters , asks them input longest word can think of using these 9 letters.
i have big text file acting dictionary search through using user inputted string seek match result check if word entered valid word. problem, want search dictionary longest word made of 9 letters, cant seem find way implement it.
so far i've tried putting every word array , searching through each element check if contains letters wont cover me if longest word can made out of 9 letters 8 letter word. idea's? have (this under submit button on form, sorry not providing code or mentioning it's windows form application):
streamreader textfile = new streamreader("c:/eclipse/personal projects/local_projects/projects/countdown/windowsformsapplication1/wordlist.txt"); int counter1 = 0; string letterlist = (txtletter1.text + txtletter2.text + txtletter3.text + txtletter4.text + txtletter5.text + txtletter6.text + txtletter7.text + txtletter8.text + txtletter9.text); // stores letters string char[] letters = letterlist.tochararray(); // reads letters char array string[] line = file.readalllines("c:/eclipse/personal projects/local_projects/projects/countdown/windowsformsapplication1/wordlist.txt"); // reads every line in word file string array (there new word on everyline, , theres 144k words, assume big performance nail i've never done before im not sure ?) line.any(x => line.contains(x)); // playing linq, i've no thought im doing though i've never used before (int = 0; < line.length; i++)// loop loops every word in string array // if (line.contains(letters)) //checks if word contains letters in char array(this gets hazy if went way, i'd planned on using words witha letter length > 4, adding words found text file , either finding longest word in text file or keeping running longest word i.e. while looping find word 7 letters, longest word, go next word , has 8 of our letters, set longest word this) counter1++; if (counter1 > 4) txtlongest.text += line + environment.newline;
mike's code:
using system; using system.collections.generic; using system.linq;
class program
static void main(string[] args) { var letters = args[0]; var wordlist = new list<string> { "abcbca", "bca", "def" }; // dictionary var results = string word in wordlist // makes every word in dictionary seperate string isvalidanswer(word, letters) // calls isvalid method orderby word.length descending // sorts word letters top select word; // selects word foreach (var result in results) { console.writeline(result); // outputs word } } private static bool isvalidanswer(string word, string letters) { foreach (var letter in word) { if (letters.indexof(letter) == -1) { // checks if theres letters in word homecoming false; } letters = letters.remove(letters.indexof(letter), 1); } homecoming true; } }
here's reply knocked in couple of minutes should want. others have said, problem complex , algorithm going slow. linq query evaluates each string in dictionary, checking whether supplied letters can used produce said word.
using system; using system.collections.generic; using system.linq; class programme { static void main(string[] args) { var letters = args[0]; var wordlist = new list<string> { "abcbca", "bca", "def" }; var results = string word in wordlist isvalidanswer(word, letters) orderby word.length descending select word; foreach (var result in results) { console.writeline(result); } } private static bool isvalidanswer(string word, string letters) { foreach (var letter in word) { if (letters.indexof(letter) == -1) { homecoming false; } letters = letters.remove(letters.indexof(letter), 1); } homecoming true; } } c# string algorithm full-text-search text-files
No comments:
Post a Comment