Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29287 > unrolled thread

A question about a solution from codingbat.com

Started bycdalten@gmail.com
First post2014-07-24 17:26 -0700
Last post2014-07-27 23:02 +0000
Articles 9 — 7 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  A question about a solution from codingbat.com cdalten@gmail.com - 2014-07-24 17:26 -0700
    Re: A question about a solution from codingbat.com Arne Vajhøj <arne@vajhoej.dk> - 2014-07-24 20:47 -0400
      Re: A question about a solution from codingbat.com cdalten@gmail.com - 2014-07-24 18:39 -0700
        Re: A question about a solution from codingbat.com Arne Vajhøj <arne@vajhoej.dk> - 2014-07-24 21:43 -0400
        Re: A question about a solution from codingbat.com thinbluernime <thinbluernime3@gmail.com> - 2014-07-25 03:52 +0000
    Re: A question about a solution from codingbat.com Joerg Meier <joergmmeier@arcor.de> - 2014-07-25 08:05 +0200
      Re: A question about a solution from codingbat.com Leif Roar Moldskred <leifm@huldreskog.lan> - 2014-07-25 01:19 -0500
    Re: A question about a solution from codingbat.com rossano.basile@tiscali.it (Rossano Basile) - 2014-07-26 10:05 +0200
      Re: A question about a solution from codingbat.com Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-07-27 23:02 +0000

#29287 — A question about a solution from codingbat.com

Fromcdalten@gmail.com
Date2014-07-24 17:26 -0700
SubjectA question about a solution from codingbat.com
Message-ID<07898d55-9606-44a7-8298-8f5f1c9094c6@googlegroups.com>
The solution in question is the doubleX problem found at...

http://codingbat.com/prob/p186759

Which states "Given a string, return true if the first instance of "x" in the string is immediately followed by another "x". 

doubleX("axxbb") → true
doubleX("axaxax") → false
doubleX("xxxxx") → true"

What the author has for a solution is....

boolean doubleX(String str) {
  int i = str.indexOf("x");
  if (i == -1) return false; // no "x" at all

  // Is char at i+1 also an "x"?
  if (i+1 >= str.length()) return false; // check i+1 in bounds?
  return str.substring(i+1, i+2).equals("x");
  
  // Another approach -- .startsWith() simplifies the logic
  // String x = str.substring(i);
  // return x.startsWith("xx");
} 


However, what I came up with is....

boolean doubleX(String str) {
  for(int i = 0; i < str.length() - 1; i++) {
     if(str.charAt(i) == 'x' && str.charAt(i+1) == 'x'){
         return true;
     }
  }//end for
  return false;
}

So the question becomes, is there any particular advantage to the author's solution? Also, i not quite getting when something like

if (i+1 >= str.length()) return false; // check i+1 in bounds?

would actually happen.

Chad

[toc] | [next] | [standalone]


#29288

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-07-24 20:47 -0400
Message-ID<53d1a920$0$291$14726298@news.sunsite.dk>
In reply to#29287
On 7/24/2014 8:26 PM, cdalten@gmail.com wrote:
> The solution in question is the doubleX problem found at...
>
> http://codingbat.com/prob/p186759
>
> Which states "Given a string, return true if the first instance of "x" in the string is immediately followed by another "x".
>
> doubleX("axxbb") → true
> doubleX("axaxax") → false
> doubleX("xxxxx") → true"
>
> What the author has for a solution is....
>
> boolean doubleX(String str) {
>    int i = str.indexOf("x");
>    if (i == -1) return false; // no "x" at all
>
>    // Is char at i+1 also an "x"?
>    if (i+1 >= str.length()) return false; // check i+1 in bounds?
>    return str.substring(i+1, i+2).equals("x");
>
>    // Another approach -- .startsWith() simplifies the logic
>    // String x = str.substring(i);
>    // return x.startsWith("xx");
> }
>
>
> However, what I came up with is....
>
> boolean doubleX(String str) {
>    for(int i = 0; i < str.length() - 1; i++) {
>       if(str.charAt(i) == 'x' && str.charAt(i+1) == 'x'){
>           return true;
>       }
>    }//end for
>    return false;
> }
>
> So the question becomes, is there any particular advantage to the author's solution?

I don't think your code and his code are doing exactly the same.

I would expect different results from "axbxxc".

>                                       Also, i not quite getting when something like
>
> if (i+1 >= str.length()) return false; // check i+1 in bounds?
>
> would actually happen.

if (i+1 >= str.length()) return false; // check i+1 in bounds?

handle the case where the first 'x' is the last char in the string (it
is not necesarry and will result in an exception to try and look at the 
next char).

Arne


[toc] | [prev] | [next] | [standalone]


#29290

Fromcdalten@gmail.com
Date2014-07-24 18:39 -0700
Message-ID<da5efc57-5188-427b-988b-7975d28e7c3f@googlegroups.com>
In reply to#29288
 I don't think your code and his code are doing exactly the same. I would expect different results from "axbxxc". 

Why does this produce different results?

Chad

[toc] | [prev] | [next] | [standalone]


#29291

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-07-24 21:43 -0400
Message-ID<53d1b655$0$302$14726298@news.sunsite.dk>
In reply to#29290
On 7/24/2014 9:39 PM, cdalten@gmail.com wrote:
>#   I don't think your code and his code are doing exactly the same. I would expect different results from "axbxxc".
>
> Why does this produce different results?

His code stops after first x. Your code loops through the
entire string.

Arne

[toc] | [prev] | [next] | [standalone]


#29293

Fromthinbluernime <thinbluernime3@gmail.com>
Date2014-07-25 03:52 +0000
Message-ID<lqsk93$2bo$1@speranza.aioe.org>
In reply to#29290
On Thu, 24 Jul 2014 18:39:12 -0700, cdalten wrote:

> I don't think your code and his code are doing exactly the same. I would
> expect different results from "axbxxc".
> 
> Why does this produce different results?

This version shouldn't:

boolean doubleX(String str) {
  for(int i = 0; i < str.length() - 1; i++) {
     if(str.charAt(i) == 'x'){
         return str.charAt(i+1) == 'x';
     }
  }//end for
  return false;
}

[toc] | [prev] | [next] | [standalone]


#29300

FromJoerg Meier <joergmmeier@arcor.de>
Date2014-07-25 08:05 +0200
Message-ID<17izj5v979k3o.zxnfiw8rjh12$.dlg@40tude.net>
In reply to#29287
On Thu, 24 Jul 2014 17:26:17 -0700 (PDT), cdalten@gmail.com wrote:

> What the author has for a solution is....

In the spirit of many cooks and all that, I felt I needed to contribute my
own solution, with no guarantee for correctness, since I just woke up:

boolean doubleX(String str) {
    return str.indexOf("x") > -1 && str.indexOf("x") == str.indexOf("xx");
} 

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#29301

FromLeif Roar Moldskred <leifm@huldreskog.lan>
Date2014-07-25 01:19 -0500
Message-ID<hYudnW7UBpFCa0zOnZ2dnUVZ7s-dnZ2d@giganews.com>
In reply to#29300
Joerg Meier <joergmmeier@arcor.de> wrote:
> On Thu, 24 Jul 2014 17:26:17 -0700 (PDT), cdalten@gmail.com wrote:
> 
>> What the author has for a solution is....
> 
> In the spirit of many cooks and all that, I felt I needed to contribute my
> own solution, with no guarantee for correctness, since I just woke up:
> 
> boolean doubleX(String str) {
>    return str.indexOf("x") > -1 && str.indexOf("x") == str.indexOf("xx");
> } 
> 

This one works, but it's not a very time-efficient algorithm since you're
doing three potentially expensive indexOf() searches rather than one. Depending
on the particulars of the problem that may or may not be an issue.

I'd do it thus (untested, and WBC[1]):

  boolean doubleX( String str ) {
    int idx = str.indexOf( 'X' );
    return idx >= 0 && idx <= str.length() - 2 && str.charAt( idx + 1 ) == 'X';
  }


[1] Written Before Coffee

-- 
Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#29333

Fromrossano.basile@tiscali.it (Rossano Basile)
Date2014-07-26 10:05 +0200
Message-ID<1lpdyjk.15qjk7uxkafkwN%rossano.basile@tiscali.it>
In reply to#29287
<cdalten@gmail.com> wrote:

> The solution in question is the doubleX problem found at...
> 
> http://codingbat.com/prob/p186759
> 
> Which states "Given a string, return true if the first instance of "x" in
> the string is immediately followed by another "x".
> 
> doubleX("axxbb") ? true
> doubleX("axaxax") ? false
> doubleX("xxxxx") ? true"
> 
> [...]

  static boolean doubleX(String str) {
      return str.matches("^[^x]*x{2,}.*$");     
  }

-- 
Questa firma è stata lasciata intenzionalmente vuota.

[toc] | [prev] | [next] | [standalone]


#29371

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2014-07-27 23:02 +0000
Message-ID<slrnltb186.fp3.avl@login.logic.tuwien.ac.at>
In reply to#29333
Rossano Basile <rossano.basile@tiscali.it> wrote:
><cdalten@gmail.com> wrote:
>> The solution in question is the doubleX problem found at...
>> http://codingbat.com/prob/p186759
>
>   static boolean doubleX(String str) {
>       return str.matches("^[^x]*x{2,}.*$");     
>   }

You might need to insert "(?s)" at the begin of the RE, or it might fail
to work with input containing newline characters behind the x's. (*) ;-)
Alternatively, you can also just omit the ".*$" ...

(*): http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#DOTALL

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web