Method that returns true if two strings are scrambled versions of eachother 
Author Message
 Method that returns true if two strings are scrambled versions of eachother

Can someone help me make one?  I've tried and mine returns true no matter
what :(.


Sat, 01 May 2004 06:22:29 GMT  
 Method that returns true if two strings are scrambled versions of eachother

Quote:

> Can someone help me make one?

Here's a dumb way that's very easy to program.

Start with an array of integers of size 255 (because there are 255
possible ascii characters). Initially the values will all be zero (ain't
RB grand?). Run through the first string one character at a time; get the
ascii value of each character, use it as an index, and add 1 to the value
at that point in the array. In other words, you're counting how many times
every possible ascii character occurs in the first string and recording
this information in the array.

Now do it again with the second string, using a second array.

Now run through both arrays looking to see if all 255 values are
identical. If they are, the two strings are anagrams of one another.

m.

--

  *** REALbasic: The Definitive Guide! The Second Edition! ***
http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt



Sat, 01 May 2004 08:17:01 GMT  
 Method that returns true if two strings are scrambled versions of eachother


Quote:


>> Can someone help me make one?

> Here's a dumb way that's very easy to program.

> Start with an array of integers of size 255 (because there are 255
> possible ascii characters). Initially the values will all be zero (ain't
> RB grand?). Run through the first string one character at a time; get the
> ascii value of each character, use it as an index, and add 1 to the value
> at that point in the array. In other words, you're counting how many times
> every possible ascii character occurs in the first string and recording
> this information in the array.

> Now do it again with the second string, using a second array.

> Now run through both arrays looking to see if all 255 values are
> identical. If they are, the two strings are anagrams of one another.

> m.

That's a smart idea, but what if it can't be case-sensitive?


Sat, 01 May 2004 08:23:45 GMT  
 Method that returns true if two strings are scrambled versions of eachother


Quote:




>>> Can someone help me make one?

>> Here's a dumb way that's very easy to program.

>> Start with an array of integers of size 255 (because there are 255
>> possible ascii characters). Initially the values will all be zero (ain't
>> RB grand?). Run through the first string one character at a time; get the
>> ascii value of each character, use it as an index, and add 1 to the value
>> at that point in the array. In other words, you're counting how many times
>> every possible ascii character occurs in the first string and recording
>> this information in the array.

>> Now do it again with the second string, using a second array.

>> Now run through both arrays looking to see if all 255 values are
>> identical. If they are, the two strings are anagrams of one another.

>> m.
> That's a smart idea, but what if it can't be case-sensitive?

No wait, I could just make both stings lowercase firstt.  Never mind.


Sat, 01 May 2004 08:28:13 GMT  
 Method that returns true if two strings are scrambled versions of eachother


Quote:


>> Can someone help me make one?

> Here's a dumb way that's very easy to program.

> Start with an array of integers of size 255 (because there are 255
> possible ascii characters). Initially the values will all be zero (ain't
> RB grand?). Run through the first string one character at a time; get the
> ascii value of each character, use it as an index, and add 1 to the value
> at that point in the array. In other words, you're counting how many times
> every possible ascii character occurs in the first string and recording
> this information in the array.

> Now do it again with the second string, using a second array.

> Now run through both arrays looking to see if all 255 values are
> identical. If they are, the two strings are anagrams of one another.

It still doesn't work :(.  Can you give me the code to do this? Maybe I'm
just writing it wrong...


Sat, 01 May 2004 09:45:26 GMT  
 Method that returns true if two strings are scrambled versions of eachother

Quote:



> > Start with an array of integers of size 255 (because there are 255
> > possible ascii characters). Initially the values will all be zero (ain't
> > RB grand?). Run through the first string one character at a time; get the
> > ascii value of each character, use it as an index, and add 1 to the value
> > at that point in the array. In other words, you're counting how many times
> > every possible ascii character occurs in the first string and recording
> > this information in the array.

> > Now do it again with the second string, using a second array.

Or use the same array and subtract 1 instead of adding.

Quote:

> > Now run through both arrays looking to see if all 255 values are
> > identical.

In my version, you only need to check that all the values are 0.

Quote:
> > If they are, the two strings are anagrams of one another.
> It still doesn't work :(.  Can you give me the code to do this? Maybe I'm
> just writing it wrong...

Maybe you could give us your code. Then we can spot the error, if there
is one.

--
Groeten,
Roel Griffioen



Sun, 02 May 2004 07:13:02 GMT  
 Method that returns true if two strings are scrambled versions of eachother


Quote:
> Or use the same array and subtract 1 instead of adding.

God, I hate it when people are that clever! How come I never think up
anything that clever?

Chip



Sun, 02 May 2004 07:29:22 GMT  
 Method that returns true if two strings are scrambled versions of eachother

Quote:



> > Or use the same array and subtract 1 instead of adding.

> God, I hate it when people are that clever! How come I never think up
> anything that clever?

Why, what does it really save? You still have to run through both strings
once and then run through the whole array at the end to see what you got,
so I doubt that the time is reduced; and the space savings is irrelevant.
Save your admiration and your attempts at ingenuity, Chip, for when it
matters. It can be a mistake to spend time contemplating refinements when
you could be forging ahead.

In any case the algorithm that I suggested is not necessarily the
algorithm I would actually use. I was just trying to provide something
very clear that would be easy for a learner to grasp, to program and to
debug. Sometimes the clear dumb way is the appropriate way. m.

--

  *** REALbasic: The Definitive Guide! The Second Edition! ***
http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt



Sun, 02 May 2004 11:04:43 GMT  
 Method that returns true if two strings are scrambled versions of eachother
In article


Quote:




> > > Or use the same array and subtract 1 instead of adding.

> > God, I hate it when people are that clever! How come I never think up
> > anything that clever?

> Why, what does it really save? You still have to run through both strings
> once and then run through the whole array at the end to see what you got,
> so I doubt that the time is reduced; and the space savings is irrelevant.
> Save your admiration and your attempts at ingenuity, Chip, for when it
> matters. It can be a mistake to spend time contemplating refinements when
> you could be forging ahead.

> In any case the algorithm that I suggested is not necessarily the
> algorithm I would actually use. I was just trying to provide something
> very clear that would be easy for a learner to grasp, to program and to
> debug. Sometimes the clear dumb way is the appropriate way. m.

I know all of what you said, Matt, but it was just so darned clever!

Chip



Sun, 02 May 2004 12:13:56 GMT  
 Method that returns true if two strings are scrambled versions of eachother


Quote:



>>> Start with an array of integers of size 255 (because there are 255
>>> possible ascii characters). Initially the values will all be zero (ain't
>>> RB grand?). Run through the first string one character at a time; get the
>>> ascii value of each character, use it as an index, and add 1 to the value
>>> at that point in the array. In other words, you're counting how many times
>>> every possible ascii character occurs in the first string and recording
>>> this information in the array.

>>> Now do it again with the second string, using a second array.

> Or use the same array and subtract 1 instead of adding.

>>> Now run through both arrays looking to see if all 255 values are
>>> identical.

> In my version, you only need to check that all the values are 0.

>>> If they are, the two strings are anagrams of one another.
>> It still doesn't work :(.  Can you give me the code to do this? Maybe I'm
>> just writing it wrong...

> Maybe you could give us your code. Then we can spot the error, if there
> is one.

The method has two parameters (scrambled and trying) and returns boolean.
  dim scrusage (126) As integer
  dim tryusage (126) As integer
  dim scrchars (-1) As string
  dim trychars (-1) As string
  dim i,j,l As integer
  dim b As boolean
  dim lowerscrambled As string
  dim lowertrying As string
  lowertrying=lowercase(trying)
  lowerscrambled=lowercase(scrambled)
  rem this if-then-else loop will only do more time-consuming tests if the
two strings are the same length
  if len(scrambled)=len(trying) then
    b=true
    for i = 0 to len(scrambled)
      scrchars.append mid(lowerscrambled, i, 1)
    next
    i=0
    for i = 0 to len(trying)
      trychars.append mid(lowertrying, i, 1)
    next
    for j=0 to ubound(trychars)
      tryusage(asc(trychars(j)))=tryusage(asc(trychars(j)))+1
    next
    j=0
    for j=0 to ubound(scrchars)
      tryusage(asc(scrchars(j)))=scrusage(asc(scrchars(j)))+1
    next
    for l=0 to ubound(tryusage)
      if tryusage(l)=scrusage(l) then
        b=true
      else
        b=false
      end
    next
    return b
  else
    return false
  end


Mon, 03 May 2004 00:35:07 GMT  
 Method that returns true if two strings are scrambled versions of eachother

Quote:

>     for j=0 to ubound(scrchars)
>       tryusage(asc(scrchars(j)))=scrusage(asc(scrchars(j)))+1

guess that one has to be

        scrusage(asc(scrchars(j)))=scrusage(asc(scrchars(j)))+1

Quote:
>     next

[snip]

Quote:
>      if tryusage(l)=scrusage(l) then
>        b=true
>      else
>        b=false
>      end

Well, you don't want to ruin your check by setting b to true if
tryusage(126)=scrusage(126), since mostly they will both be zero. So
just write:

      if tryusage(l)<>scrusage(l) then
        b=false
      end

--
Groeten,
Roel Griffioen



Tue, 04 May 2004 06:14:54 GMT  
 Method that returns true if two strings are scrambled versions of eachother


Quote:
> if tryusage(l)<>scrusage(l) then
> b=false
> end

I don't understand, it still doesn't work!  Can you just give me all of the
code or soomething?  It is driving me nuts!


Sun, 09 May 2004 05:53:45 GMT  
 Method that returns true if two strings are scrambled versions of eachother

Quote:



> > if tryusage(l)<>scrusage(l) then
> > b=false
> > end
> I don't understand, it still doesn't work!  Can you just give me all of the
> code or soomething?  It is driving me nuts!

this code does it:

  dim scrusage (126) As integer
  dim tryusage (126) As integer
  dim scrchars (-1) As string
  dim trychars (-1) As string
  dim i,j,l As integer
  dim b As boolean
  dim lowerscrambled As string
  dim lowertrying As string
  lowertrying=lowercase(trying)
  lowerscrambled=lowercase(scrambled)
  rem this if-then-else loop will only do more time-consuming tests if
the two strings are the same length
  if len(scrambled)=len(trying) then
    b=true
    for i = 0 to len(scrambled)
      scrchars.append mid(lowerscrambled, i, 1)
    next
    i=0
    for i = 0 to len(trying)
      trychars.append mid(lowertrying, i, 1)
    next
    for j=0 to ubound(trychars)
      tryusage(asc(trychars(j)))=tryusage(asc(trychars(j)))+1
    next
    j=0
    for j=0 to ubound(scrchars)
      scrusage(asc(scrchars(j)))=scrusage(asc(scrchars(j)))+1
    next
    for l=0 to ubound(tryusage)
      if tryusage(l)<>scrusage(l) then
        b=false
      end
    next
    return b
  else
    return false
  end

--
Groeten,
Roel Griffioen



Sun, 09 May 2004 06:38:05 GMT  
 Method that returns true if two strings are scrambled versions of eachother


Quote:
> dim scrusage (126) As integer
> dim tryusage (126) As integer
> dim scrchars (-1) As string
> dim trychars (-1) As string
> dim i,j,l As integer
> dim b As boolean
> dim lowerscrambled As string
> dim lowertrying As string
> lowertrying=lowercase(trying)
> lowerscrambled=lowercase(scrambled)
> rem this if-then-else loop will only do more time-consuming tests if
> the two strings are the same length
> if len(scrambled)=len(trying) then
> b=true
> for i = 0 to len(scrambled)
> scrchars.append mid(lowerscrambled, i, 1)
> next
> i=0
> for i = 0 to len(trying)
> trychars.append mid(lowertrying, i, 1)
> next
> for j=0 to ubound(trychars)
> tryusage(asc(trychars(j)))=tryusage(asc(trychars(j)))+1
> next
> j=0
> for j=0 to ubound(scrchars)
> scrusage(asc(scrchars(j)))=scrusage(asc(scrchars(j)))+1
> next
> for l=0 to ubound(tryusage)
> if tryusage(l)<>scrusage(l) then
> b=false
> end
> next
> return b
> else
> return false
> end

Thank you soooooooooooo much!  It works perfectly!  Thanks!


Sun, 09 May 2004 07:48:10 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. compare two numbers with one string (true/false) output

2. returning text between two strings

3. String Collection returned from OCX method

4. Two strings in two columns in Exel?

5. Howto CancelClose return true ?

6. Return True to a Mouse Down Event in Canvas

7. If Observable#notify_observers returned true/false then false could imply veto of change

8. Stylistic question: returning strings vs. pointers to strings

9. eof on channel, not eof on transform: [eof] returns true

10. recursive function that returns true/false (newbie stuff)

11. Tkinter wart: returned texts are sometimes strings, sometime Unicode strings

12. Why do [string is true {}] and [string is false {}] BOTH return 1??

 

 
Powered by phpBB® Forum Software