7 Replies - 2789 Views - Last Post: 19 December 2009 - 02:49 PM Rate Topic: -----

#1 Cobok  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 18-March 07

Suggestions upon my anagram finder

Posted 18 December 2009 - 04:05 PM

Option Strict On
Option Explicit On
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim countl, countd, z As Integer
		Dim Check As Boolean
		Dim st, leksh As String
		Dim line As String
		Check = False
		st = ""
		z = 0
		For i = 1 To 10
			leksh = ""
			Do While TextBox1.Text(z) <> " "
				leksh = leksh & TextBox1.Text(z)
				z = z + 1
			Loop
		 Dim fileReader As System.IO.StreamReader
			fileReader = _
			My.Computer.FileSystem.OpenTextFileReader("C:\wordlist.txt")
			Do While (fileReader.Peek() >= 0)
				line = fileReader.ReadLine()
				Check = False
				If Len(leksh) = Len(line) Then
					Check = True
					countd = 0
					countl = 0
					For j = 32 To 126
						For k = 0 To Len(leksh) - 1
							If Chr(j) = leksh(k) Then
								countl = countl + 1
							End If
							If Chr(j) = line(k) Then
								countd = countd + 1
							End If
						Next
						If countd <> countl Then
							Check = False
						End If
					Next
				End If
				If Check = True Then
					st = st & line & ","
								 End If
			Loop
			z = z + 1
		Next
		TextBox1.Text = st
	End Sub
End Class



Edit: Somehow I messed it up and deleted the original post... Anyways the general dea can be found in the title, I want to hear ideas and I'm open to any suggestion... :)

This post has been edited by Cobok: 19 December 2009 - 03:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Suggestions upon my anagram finder

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Suggestions upon my anagram finder

Posted 18 December 2009 - 06:07 PM

It is my understanding that each variable must have datatype declared, even if all on one line, else will default to Variant. Example: Dim x As String, y As String, z As String. Variant datatype reserves more memory space.
Was This Post Helpful? 0
  • +
  • -

#3 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Suggestions upon my anagram finder

Posted 18 December 2009 - 07:14 PM

Note - the only printable chr values are between 32 and 126 inclusive so you don't need to go 0..255.

You could do it in "reverse" to your method. Compare the lengths, but then create an array of bytes and increment the entries for each character in the input string and then decrement the elements for each character in the scrambled string. The result is that all array entries should still be zero. Your current algorithm does 256xlengthx2 comparisons while this one does 256 initializations steps, 2xlength increments/decrements and 256 compares to zero. You could even "avoid " the initialization as the array defaults to zero on creation and can be zeroed after the check stage to maintain the zero values.

An alternate method would be to sort the characters in each string then a string comparison will work and you can use many of the optimized string routines like SubString, etc.. You would need to keep 2 strings for every array element, the sorted and the original strings.

You should also read the words from the file into an array of string so you don't keep hitting the file system - much faster that way. If you use the sorted method, then you would sort each user input string and then do the comparison. This method can scale to very large string databases as you can start using bianry sorts instead of much slower linear searches.

There are many ways to do this.
Was This Post Helpful? 0
  • +
  • -

#4 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Suggestions upon my anagram finder

Posted 19 December 2009 - 02:02 AM

Stick two lines at the top:

Option Strict On
Option Explicit On


And fix up all the errors.

Refactor the code to rename the variable stringReader. The problem with the name as it stands is that it's not a string reader - it's the line that you read, and also there is a StringReader type in the framework, so it is confusing.

You can do this by finding the line where it is declared, renaming it to something else (I suggest line as a better name) and pressing ctrl-. and then enter (do this before you click or spacebar away - a little red line needs to be showing under the word that you have typed in). This will quickly do a "find and replace" for all occurrences of the name. It's a very handy keyboard short-cut to know.

Use & to concatenate strings, not + (see: http://msdn.microsof...8VS.80%29.aspx).

This post has been edited by crepitus: 19 December 2009 - 02:06 AM

Was This Post Helpful? 0
  • +
  • -

#5 Cobok  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 18-March 07

Re: Suggestions upon my anagram finder

Posted 19 December 2009 - 02:43 AM

First I ought to say something... it feels so good programming in a 24-inch widescreen at 1920*1080 , I believe all of you should try it at some time... :) It cant compare with the square 18-inch i used to have .... at all!

View Postmark.bottomley, on 18 Dec, 2009 - 06:14 PM, said:

An alternate method would be to sort the characters in each string then a string comparison will work and you can use many of the optimized string routines like SubString, etc.. You would need to keep 2 strings for every array element, the sorted and the original strings.

You should also read the words from the file into an array of string so you don't keep hitting the file system - much faster that way. If you use the sorted method, then you would sort each user input string and then do the comparison. This method can scale to very large string databases as you can start using bianry sorts instead of much slower linear searches.


Thanks for your suggestions... :)
June7 I realised that mistake and corrected it...

mark.bottomley about the above idea , can you explain a bit more the string arrays and the sorted method you are referring to?

Edit: Crepitus :P lol you are right... Anyway I think I will use these options more in the future they seem interesting... I wonder why they are off by default

This post has been edited by Cobok: 19 December 2009 - 03:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Suggestions upon my anagram finder

Posted 19 December 2009 - 03:21 AM

View PostCobok, on 19 Dec, 2009 - 08:43 AM, said:

crepitus , the options you suggested didnt change anything at all , no errors appeared...


Yup, you've done what June7 said, so that fixes the errors that would have shown with the original code.
Was This Post Helpful? 0
  • +
  • -

#7 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Suggestions upon my anagram finder

Posted 19 December 2009 - 12:30 PM

you are currently trying to see if a word has the same number of the same letters as the words read from the file.
Dim wordList() As String = IO.File.ReadAllLines("C:\wordlist.txt")


will put all the words into an array assuming each word in the file is on it's own line. You only need to do this once at the beginning of the program - maybe in form load.
Now sort each of the input word letters so e.g. "ample" becomes "aelmp"
Dim i As Integer
Dim letterSortedWordList() As String
ReDim letterSortedWordList(wordList.Count)
For i = 0 to wordList.Count - 1
  letterSortedWordList(i) = sortChars(wordList(i))
Next


I leave the sortChars implementation to you.
Now you have your input (I would only do one word per click instead of 10) - apply the sortChars to the input string and then search the list.
Dim scrambled As String = tbScrambled.Text
Dim sorted As String = sortChars(scrambled)
Dim match As Integer = -1
For i = 0 to wordList.Count - 1
  If letterSortedWordList(i) = sorted Then
	match = i
	Exit For
  End If
Next
If match <> -1 Then
  'output wordList(match)
Else
  ' output "not Found"
End If



The sort code will likely include things like:
Dim c() as Char = s.ToCharArray()
Array.Sort(c)
Return New String(c)


Was This Post Helpful? 0
  • +
  • -

#8 Cobok  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 18-March 07

Re: Suggestions upon my anagram finder

Posted 19 December 2009 - 02:49 PM

Yeah , I see , great idea... I understood your concept but I didn't really know how to implement it in VB , which I do now ... :)

I intentionally made the part about reading ten pieces because I used this to solve a programming challenge :) ,
and I wish I used your idea earlier cause it wouldve saved a lot of time....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1