Results 1 to 7 of 7

Thread: Another javabat problem.

  1. Registered TeamPlayer i8pptuakamonstercam's Avatar
    Join Date
    11-26-06
    Location
    Anywhere you want to be.
    Posts
    3,946
    Post Thanks / Like
    Blog Entries
    1
    Stat Links

    Another javabat problem.
    #1

    Another javabat problem.

    Given a string, compute recursively (no loops) a new string where all the lowercase 'x' chars have been changed to 'y' chars.

    changeXY("codex") ? "codey"
    changeXY("xxhixx") ? "yyhiyy"
    changeXY("xhixhix") ? "yhiyhiy"

    *code I have so far*
    Code:
    public String changeXY(String str) {
     if(str.length() == 0)
      return "";
      
     if(str.charAt(str.length() - 1) == 'x')
     {
      str.charAt(str.length() - 1) = 'y';
     }
      
     return str + changeXY(str.substring(0, str.length() - 1));
    }
    *error that I am getting*
    Error: str.charAt(str.length() - 1) = 'y';
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The left-hand side of an assignment must be a variable
    The only problem is that I can not think of a way to put this in "variable" form, but I can think how to do this in a loop which does not help me at all in this case.

  2. Registered TeamPlayer draco7891's Avatar
    Join Date
    02-11-08
    Posts
    3,700
    Post Thanks / Like
    #2

    Re: Another javabat problem.

    Store the location to a variable in the function and pass it when you call the function (making the initial call (str.length() - 1) and all recursive calls (<current location> - 1, checking in the first part of the function if you've passed the first index (ie, location as called is less than 0)).

    ie, your function becomes:

    public String changeXY (String str, int location)
    {

    //check for zero-length, return "" and terminate if true

    //check for location < 0; if true, THEN return str, otherwise continue

    //check char at location, change if necessary

    //location is not < 0, so call changeXY(str, (location - 1))

    }

    And the initial call when starting the recursion is: changeXY(str, (str.length() - 1))

    SUPER EDIT:

    Ah, I see the problem; you're trying to perform operations on a string directly, and you can't do that (they're nasty little things that don't behave like normal variables).

    Use concat and substring with a current working location to pull the string apart and substitute the correct character as needed, although strings in java are stupid. Use a StringBuffer instead?

    Draco

  3. Registered TeamPlayer i8pptuakamonstercam's Avatar
    Join Date
    11-26-06
    Location
    Anywhere you want to be.
    Posts
    3,946
    Post Thanks / Like
    Blog Entries
    1
    Stat Links

    Another javabat problem.
    #3

    Re: Another javabat problem.

    OK im a little confused, but the idk if implemented it correctly but javabat does not like it when you add in new parameters in their methods.

    I also looked up string buffer seems interesting but Im not sure how to use it correctly. I saw how I could append things but that didn't really help.

    *Something I didn't quite get*
    //check for location < 0; if true, THEN return str, otherwise continue
    why would I need to check for location < 0...if I already checked the str.length() to see if it was == 0. Why would the location be less than 0 any way?


  4. Registered TeamPlayer draco7891's Avatar
    Join Date
    02-11-08
    Posts
    3,700
    Post Thanks / Like
    #4

    Re: Another javabat problem.

    Quote Originally Posted by i8pptuakamonstercam
    *Something I didn't quite get*
    //check for location < 0; if true, THEN return str, otherwise continue
    why would I need to check for location < 0...if I already checked the str.length() to see if it was == 0. Why would the location be less than 0 any way?
    What the code is doing is stepping through the string backwards, from the end to the first character. Every time that the function recurses, it calls a location one less in the string (ie, the character that would be "in front" of the one in the prior recursion).

    Ex.: string given of "heavy"

    Strings are null terminated, so the whole string in memory looks like: "heavy\0"

    str.length() - 1 is the location of the last non-escape character in the string, which is 'y'. The initial call to the function is to work on this location.

    The function does what it should as appropriate (change 'y' to 'x').

    The function then calls itself (recurses), calling the next forward location (in this case, 'v').

    And so on, until it reaches the first character in the string, which is always at location == 0.

    _________________________________________________

    SO, the reason you check for location < 0 is because the last recursion works on location == 0, then calls the next recursion to the next lower location (-1). At this point, you have worked on the entire string and location is now invalid, which means the function should return what it's done and exit.

    It's sort of like a token-controlled loop, in reverse, using recursive functions. Each recursion should check to see first if it's past the beginning of the string (beyond location == 0), which means everything's already been done.

    Draco

  5. Registered TeamPlayer i8pptuakamonstercam's Avatar
    Join Date
    11-26-06
    Location
    Anywhere you want to be.
    Posts
    3,946
    Post Thanks / Like
    Blog Entries
    1
    Stat Links

    Another javabat problem.
    #5

    Re: Another javabat problem.

    OOo I think I understand now. now lets hope I can just complete it.. stay tuned lol.
    *Update*
    Although I understand(partially) what you are telling me I have worked over an hour on this fucking problem(aka way too long). I tried StringBuffer it doesn't allow it, I look at different methods in the string class they don't work.

    I will now kill myself.

  6. Registered TeamPlayer draco7891's Avatar
    Join Date
    02-11-08
    Posts
    3,700
    Post Thanks / Like
    #6

    Re: Another javabat problem.

    With StringBuffer, you can use the functions:

    deleteCharAt(int index)

    and:

    insert(int offset, char c)

    to accomplish the task. Recurse through the indexes of the given string, checking the value at the current index using a comparison to charAt(int index); if true, then delete the character at the current index, and then use insert to push the new character back into the same offset/index.

    The other way using strings alone is to make a new string, and use substring to take the part of the given string before the character to be changed, then the new character, then all after the character to be changed.

    Ex:

    Code:
    string changeXY (string strGiven, int index)
    {
    
     if (index < 0)
     {
    
      return strGiven;
    
     }
     else
     {
    
      if (strGiven.charAt(index) == 'x')
      {
    
       string strResult = strGiven.substring(0, (index - 1)) + 'y' + strGiven.substring((index + 1), (strGiven.length() - 1));
    
      }
      else
      {
    
       string strResult = strGiven;
    
      }
    
      changeXY(strResult, (index - 1));
    
     }
    
    };
    Initial call to set off the recursion is:

    changeXY(str, str.length() - 1);

    Where str is however you're getting your string into your code to begin with (iostreams? fstreams? I dunno ).

    _________________________________________________

    The code checks the value of the character at the current index; if it needs to be changed, the result string is built from a substring from the beginning of the given string to just before the current index, then the value we want to substitute, then another substring from just after the current index to the end of the string. The string is built and defined right there to avoid building an extraneous string variable at the last recursion, and to avoid any issues with trying to manipulate the string's data after creation.

    The very last recursion (where index < 0) will simply return the string given to it without any further manipulation, unwinding the call stack.

    Draco

    PS: String also has a function called replace(char m, char n) which does exactly what the problem wants you to do without resorting to recursion (ie, replaces all instances of char m in the string with char n). Just remember in the throes of your madness that what you're doing is rebuilding fundamental functions by working through their component steps, which is a good way to appreciate the functional complexity of the language.

  7. Registered TeamPlayer i8pptuakamonstercam's Avatar
    Join Date
    11-26-06
    Location
    Anywhere you want to be.
    Posts
    3,946
    Post Thanks / Like
    Blog Entries
    1
    Stat Links

    Another javabat problem.
    #7

    Re: Another javabat problem.

    In a burst of excitement I am happy to say that I finally got it ahahahaha*runs off in the forest of the crazy people*

    If anyone wants help with this problem ask me muhuhahahahahha. I did it so happy*cries *

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Title