Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 9th, 2005, 12:03 PM   #1
allenm
Newbie
 
Join Date: Nov 2005
Posts: 3
Rep Power: 0 allenm is on a distinguished road
Exclamation Algorithm Question - Urgent Help Needed

Hello, I am presented with the following problem and have tried desperately (over 12 hours) to solve it:

I have a string say "011010101" and I want to parse this string based on a table of values:

i = 0
m = 011
k = 10
e = 101

I want to construct an algorithm that will go through the string above and parse it assigning each value to their corresponding table value. Looking at it, I know that the string would become "011" , "0" , "10" , "101" which for this example would be "mike" in that order. Since "i" is the first thing I check, my code tries to form "0", "11010101", and nothing else will get extracted. There can be more than one correct parsing and also the table of values includes all the letters (uppercase and lowercase) as well as ' ', '\n', and '\t'. Any help would be greatly appreciated.
allenm is offline   Reply With Quote
Old Nov 9th, 2005, 12:57 PM   #2
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
Quote:
Originally Posted by allenm
There can be more than one correct parsing and also the table of values includes all the letters (uppercase and lowercase) as well as ' ', '\n', and '\t'. Any help would be greatly appreciated.
Posting what you've tried so far would also be appreciated. That way we can help and not give
MBirchmeier is offline   Reply With Quote
Old Nov 9th, 2005, 1:05 PM   #3
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Google for bitshifting, you'll probably find some tuts.
Polyphemus_ is offline   Reply With Quote
Old Nov 9th, 2005, 1:15 PM   #4
allenm
Newbie
 
Join Date: Nov 2005
Posts: 3
Rep Power: 0 allenm is on a distinguished road
Exclamation

string ConvertCodes( string binary )
{
 // what to return
 string retval = "";
 
 // what to restore the passed in value to just incase
 string tempCopy = binary;
 
 foreach( PairItem i in CodeList )
 {
    // if the CODE (i.e. "011") is a prefix of the passed in string
    if( binary.StartsWith( i.CODE ) )
    {
      // add the symbol (i.e. "i") to the return value string
      retval+=i.Symbol.ToString();
     
      // update the passed in string for further checking
      binary = binary.SubString( i.CODE.Length );
      
      // check the update string, if it equals "", there were no further matches
      if( ConvertCodes(binary) == "" )
      {
        // reset the return value
        retval = "";

        // restore the passed in string
        binary = tempCopy;
      }
    }
 }

 return retval
}

I ran through this code in debug mode for a simple string, and it found the correct return value, but it still returned "".
allenm is offline   Reply With Quote
Old Nov 9th, 2005, 1:27 PM   #5
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
Quote:
Originally Posted by allenm
string ConvertCodes( string binary )
{
 // what to return
 string retval = "";
 
 // what to restore the passed in value to just incase
 string tempCopy = binary;
 
 foreach( PairItem i in CodeList )
 {
    // if the CODE (i.e. "011") is a prefix of the passed in string
    if( binary.StartsWith( i.CODE ) )
    {
      // add the symbol (i.e. "i") to the return value string
      retval+=i.Symbol.ToString();
     
      // update the passed in string for further checking
      binary = binary.SubString( i.CODE.Length );
      
      // check the update string, if it equals "", there were no further matches
      if( ConvertCodes(binary) == "" )
      {
        // reset the return value
        retval = "";

        // restore the passed in string
        binary = tempCopy;
      }
    }
 }

 return retval
}

I ran through this code in debug mode for a simple string, and it found the correct return value, but it still returned "".
Well the good news is you're close...

Look closer at your termination case, what's returned by :
ConvertCodes("") ; // (should be "")

now what's returned by
ConvertCodes("011");

hint:

      if( ConvertCodes(binary) == "" )
      {
        // reset the return value
        retval = "";

        // restore the passed in string
        binary = tempCopy;
      }

That should help out with one character 'strings'.

Once you get that, you also have no ability to create multi character strings. You're never getting/saving / returning information from characters after the first.

-MBirchmeier
(feel free to ask questions)
MBirchmeier is offline   Reply With Quote
Old Nov 9th, 2005, 1:44 PM   #6
allenm
Newbie
 
Join Date: Nov 2005
Posts: 3
Rep Power: 0 allenm is on a distinguished road
Exclamation

I ran my code with those two values and I got the same return value. I know this is because when I get done with the algorithm, no matter what, at some point ConverCodes(binary) will equal "", so I return "" no matter what, even if I pass in one character string. How would I go about solving this?
allenm is offline   Reply With Quote
Old Nov 9th, 2005, 1:58 PM   #7
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
Quote:
Originally Posted by allenm
How would I go about solving this?
Well after you find your starting character there are three valid scenarios:

1.) You've reached the end of the string
2.) The rest of the string is a valid series of characters
3.) The rest of the string is not a valid series of characters

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC