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


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

Writing Java code while high and drunk

Started bychad altenburg <cdalten@gmail.com>
First post2020-02-17 06:57 -0800
Last post2020-02-17 22:48 +0100
Articles 3 — 3 participants

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


Contents

  Writing Java code while high and drunk chad altenburg <cdalten@gmail.com> - 2020-02-17 06:57 -0800
    Re: Writing Java code while high and drunk Arne Vajhøj <arne@vajhoej.dk> - 2020-02-17 12:58 -0500
    Re: Writing Java code while high and drunk Joerg Meier <joergmmeier@arcor.de> - 2020-02-17 22:48 +0100

#39299 — Writing Java code while high and drunk

Fromchad altenburg <cdalten@gmail.com>
Date2020-02-17 06:57 -0800
SubjectWriting Java code while high and drunk
Message-ID<2542dd61-cde8-4a30-89be-2abfca122ac9@googlegroups.com>
I *attempted* the following problem on codingbat.com while both high and drunk...

"
Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).

equalIsNot("This is not") → false
equalIsNot("This is notnot") → true
equalIsNot("noisxxnotyynotxisi") → true
"

Here is what I came up with...

public boolean equalIsNot(String str) {
  int numberIs = 0;
  int numberNot = 0;
  
  for (int i = 0 ; i <= str.length() - 2; i++) {
    if (str.substring(i, i+2).equals("is") ) {
      numberIs++;
    } 
  }
  

   for (int i = 0 ; i <= str.length() - 3; i++) {
    if (str.substring(i, i+3).equals("not") ) {
      numberNot++;
    }
   }
  
  if (numberIs == numberNot) return true;
  else return false;
}


And here is what I found via Google...

https://github.com/mirandaio/codingbat/blob/master/java/string-3/equalIsNot.java


I don't know which one is more correct. With that, I going to back to listening to my neighbor's having loud sex.

[toc] | [next] | [standalone]


#39300

FromArne Vajhøj <arne@vajhoej.dk>
Date2020-02-17 12:58 -0500
Message-ID<r2ek7g$qu3$1@gioia.aioe.org>
In reply to#39299
On 2/17/2020 9:57 AM, chad altenburg wrote:
(ignoring various non-Java pieces of information)> "
> Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).
> 
> equalIsNot("This is not") → false
> equalIsNot("This is notnot") → true
> equalIsNot("noisxxnotyynotxisi") → true
> "
> 
> Here is what I came up with...
> 
> public boolean equalIsNot(String str) {
>    int numberIs = 0;
>    int numberNot = 0;
>    
>    for (int i = 0 ; i <= str.length() - 2; i++) {
>      if (str.substring(i, i+2).equals("is") ) {
>        numberIs++;
>      }
>    }
>    
> 
>     for (int i = 0 ; i <= str.length() - 3; i++) {
>      if (str.substring(i, i+3).equals("not") ) {
>        numberNot++;
>      }
>     }
>    
>    if (numberIs == numberNot) return true;
>    else return false;
> }
> 
> 
> And here is what I found via Google...
> 
> https://github.com/mirandaio/codingbat/blob/master/java/string-3/equalIsNot.java
> 
> I don't know which one is more correct.

They are pretty similar.

I believe there are at least 3 different approaches:
1) simple for loop and equals
2) a tricky loop and indexOf
3) regex

Arne

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


#39301

FromJoerg Meier <joergmmeier@arcor.de>
Date2020-02-17 22:48 +0100
Message-ID<1sij8xdqylvah.nbjmo5nchhdv$.dlg@40tude.net>
In reply to#39299
On Mon, 17 Feb 2020 06:57:30 -0800 (PST), chad altenburg wrote:

> I *attempted* the following problem on codingbat.com while both high and drunk...

> "
> Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).

> equalIsNot("This is not") → false
> equalIsNot("This is notnot") → true
> equalIsNot("noisxxnotyynotxisi") → true
> "

> Here is what I came up with...
 
Is that missing instructions ? Because otherwise, you can just do:

Function<String, Boolean> equalIsNot = s -> s.split("is", -1).length ==
s.split("not", -1).length;
    

Runnable example:

<https://repl.it/repls/ActiveAlertFactorial>

Liebe Gruesse,
		Joerg

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

[toc] | [prev] | [standalone]


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


csiph-web