Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree3Likes

Thread: how can i do this so better?!

  1. #1
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default how can i do this so better?!

    this is question :
    Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".

    starOut("ab*cd") → "ad"
    starOut("ab**cd") → "ad"
    starOut("sm*eilly") → "silly"
    ===========
    and this is my answer

    Java Code:
    public String starOut(String str) {
    		StringBuilder string = new StringBuilder(str);
    		String res = "";
    		for(int i=0;i<string.length();i++){
    			if(string.length()==1 && string.charAt(0)=='*')
    	    		return "";
    			if(i==0 && string.charAt(i)=='*'){
    				string.deleteCharAt(i);
    				string.deleteCharAt(i);
    				i = 0;
    			}else if(i==string.length()-1 && string.charAt(i)=='*'){
    				string.deleteCharAt(i-1);
    				string.deleteCharAt(i-1);
    			}else if(i<=string.length() && string.charAt(i)=='*' && string.charAt(i+1)=='*' && string.charAt(i+2)=='*' ){
    						string.deleteCharAt(i);
    						string.deleteCharAt(i);
    						string.deleteCharAt(i);
    						string.deleteCharAt(i);
    						string.deleteCharAt(i-1);
    						i = i-2;
    					    	}else if(i<=string.length() && string.charAt(i)=='*' && string.charAt(i+1)=='*' ){
    								string.deleteCharAt(i);
    								string.deleteCharAt(i);
    								string.deleteCharAt(i);
    	
    								string.deleteCharAt(i-1);
    								i = i-2;
    							    	}else if(i<=string.length() && string.charAt(i)=='*'){
    										string.deleteCharAt(i);
    										string.deleteCharAt(i);
    										string.deleteCharAt(i-1);
    										i = i-2;
    									    	}
    			
    		    }
    		res += string;
    		return res;
    		}
    (it's correct but it's very long and i think very idiot)!
    how can i do this better?
    Last edited by HearT.Hunt3r; 08-24-2011 at 02:09 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    ACK!

    Never mind I cannot read.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    The simplest way to achieve this would be to use indexOf and substring.

  4. #4
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    And This is all exam:
    Expected This Run
    starOut("ab*cd") → "ad" "ad" OK
    starOut("ab**cd") → "ad" "ad" OK
    starOut("sm*eilly") → "silly" "silly" OK
    starOut("sm*eil*ly") → "siy" "siy" OK
    starOut("sm**eil*ly") → "siy" "siy" OK
    starOut("sm***eil*ly") → "siy" "siy" OK
    starOut("stringy*") → "string" "string" OK
    starOut("*stringy") → "tringy" "tringy" OK
    starOut("*str*in*gy") → "ty" "ty" OK
    starOut("abc") → "abc" "abc" OK
    starOut("a*bc") → "c" "c" OK
    starOut("ab") → "ab" "ab" OK
    starOut("a*b") → "" "" OK
    starOut("a") → "a" "a" OK
    starOut("a*") → "" "" OK
    starOut("*a") → "" "" OK
    starOut("*") → "" "" OK
    starOut("") → "" "" OK
    other test OK

  5. #5
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    but how can i find all of the world by indexOf ?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What? Huh? Please explain.

  7. #7
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    we have like this for ex : "vahidxyvahidxyvahidxy"
    how can find all xy from this by indexOf?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    How would you do it?

    I mean with a pen and paper not using code.

  9. #9
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    i donnu what did u said...anyway thx.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    :sigh:

    How would you do something more than once?

  11. #11
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    A couple of ideas:
    1. Nested loops: When you find a '*', do another loop to find the end of the '*'s. Then delete characters.
    2. Keep track of state: Store the index where you found the fist '*', and then continue going around the main loop until you find the last '*'. Then delete. But then you'll have to change the loop index, 'i', for it to come out right.
    3. Copy characters from a String to a StringBuilder: Copy non-'*' characters as you find them (except in some cases). Probably have to keep track of the "previous character" found/processed. (If previousChar != '*' and thisChar == '*' then delete the last char from the output.)

  12. #12
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by HearT.Hunt3r View Post
    And This is all exam:
    Expected This Run
    starOut("ab*cd") → "ad" "ad" OK
    ...
    starOut("") → "" "" OK
    other test OK
    I like the automated tests. I like them a lot.

    I also suggest adding this one:

    starOut("ax*y*zd") → "ad" // "y" is deleted by both '*'s. (Be sure that the 2nd '*' does not delete the 'a'.)
    HearT.Hunt3r likes this.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    A single while loop is all that is needed. Inside the loop you need an if statement to handle the four different situations: * is first char, * is last char, * is followed by another *, default of single * in the middle. Each condition will need to do 1 or 2 substring calls.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by JeffGrigg View Post
    starOut("ax*y*zd") → "ad"
    Grrrr!

    My solution fails this. Time to rethink.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    OK simpler solution is to only add a char to a StringBuilder if it is not followed or preceded by a *.
    sunde887 and JeffGrigg like this.

  16. #16
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    OK simpler solution is to only add a char to a StringBuilder if it is not followed or preceded by a *.
    Good Idea!

  17. #17
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    OK simpler solution is to only add a char to a StringBuilder if it is not followed or preceded by a *.
    Hmmm...
    Well, I tried it this way, and I tried it with my suggestion #3 above. And I refactored heavily, relying on automated unit tests. Much as I like my heavily computer science automata theory based state machine approach, I think I like the code that I got from Junky's suggestion better: It's more understandable, and hence more maintainable.

  18. #18
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    i got that how i can find all word in a string by indexOf ..
    int v=0;
    for(int i=0;i<str.length();i++){
    v = str.indexOf(word, i);
    i += v;
    }

  19. #19
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    The 'String.indexOf(char)' method can be used to find '*' character(s) in a string. So it could be used in this assignment.

    However, the 'for' loop and 'charAt(i)' in your original posting will also work just as well. So I don't see a good reason for you to chase the 'indexOf' idea, unless you really want to.

  20. #20
    PavelChovanec is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Re: how can i do this so better?!

    The code below uses charAt() within a for-loop. It handled the 20 test strings I threw at it.

    Java Code:
    public String starOut(String str) {
         String result = "";
         boolean delete = false;
    		  
         for (int i = 0; i < str.length(); i++) {
              if (str.charAt(i) == '*')
                  delete = true;
              else if (delete)
                  delete = false;
              else if (i == str.length() - 1 || str.charAt(i + 1) != '*')
                  result = result + str.charAt(i);
         }
         return result;
    }
    Last edited by PavelChovanec; 09-18-2012 at 09:19 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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