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


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

Writing Java code while drunk and stoned take 2

Started bychad altenburg <cdalten@gmail.com>
First post2020-02-27 06:00 -0800
Last post2020-03-06 05:52 -0800
Articles 20 on this page of 27 — 7 participants

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


Contents

  Writing Java code while drunk and stoned take 2 chad altenburg <cdalten@gmail.com> - 2020-02-27 06:00 -0800
    Re: Writing Java code while drunk and stoned take 2 nobody@example.org (Scott) - 2020-02-27 16:39 +0000
    Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-02-27 09:05 -0800
    Re: Writing Java code while drunk and stoned take 2 Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2020-02-27 19:04 +0100
      Re: Writing Java code while drunk and stoned take 2 Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2020-02-27 19:05 +0100
      Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-02-27 10:25 -0800
    Re: Writing Java code while drunk and stoned take 2 Arne Vajhøj <arne@vajhoej.dk> - 2020-02-27 18:41 -0500
      Re: Writing Java code while drunk and stoned take 2 Joerg Meier <joergmmeier@arcor.de> - 2020-03-01 22:50 +0100
        Re: Writing Java code while drunk and stoned take 2 Arne Vajhøj <arne@vajhoej.dk> - 2020-03-01 19:53 -0500
          Re: Writing Java code while drunk and stoned take 2 chad altenburg <cdalten@gmail.com> - 2020-03-03 04:54 -0800
            Re: Writing Java code while drunk and stoned take 2 Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2020-03-03 14:20 +0100
            Re: Writing Java code while drunk and stoned take 2 Joerg Meier <joergmmeier@arcor.de> - 2020-03-03 15:00 +0100
              Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-03-03 08:19 -0800
                Re: Writing Java code while drunk and stoned take 2 chad altenburg <cdalten@gmail.com> - 2020-03-04 05:53 -0800
                  Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-03-04 09:59 -0800
                    Re: Writing Java code while drunk and stoned take 2 chad altenburg <cdalten@gmail.com> - 2020-03-05 05:53 -0800
                      Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-03-05 09:29 -0800
                        Re: Writing Java code while drunk and stoned take 2 "C.D. Altenburg, Ph.D," <cdalten@gmail.com> - 2020-03-06 05:49 -0800
                          Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-03-06 07:00 -0800
                            Re: Writing Java code while drunk and stoned take 2 "C.D. Altenburg, Ph.D," <cdalten@gmail.com> - 2020-03-07 05:40 -0800
                          Re: Writing Java code while drunk and stoned take 2 Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2020-03-06 17:47 +0100
                            Re: Writing Java code while drunk and stoned take 2 "C.D. Altenburg, Ph.D," <cdalten@gmail.com> - 2020-03-07 05:42 -0800
                          Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-03-06 09:03 -0800
                    Re: Writing Java code while drunk and stoned take 2 chad altenburg <cdalten@gmail.com> - 2020-03-05 07:22 -0800
                      Re: Writing Java code while drunk and stoned take 2 Eric Douglas <e.d.programmer@gmail.com> - 2020-03-05 07:48 -0800
                        Re: Writing Java code while drunk and stoned take 2 "C.D. Altenburg, Ph.D," <cdalten@gmail.com> - 2020-03-06 05:50 -0800
                        Re: Writing Java code while drunk and stoned take 2 "C.D. Altenburg, Ph.D," <cdalten@gmail.com> - 2020-03-06 05:52 -0800

Page 1 of 2  [1] 2  Next page →


#39310 — Writing Java code while drunk and stoned take 2

Fromchad altenburg <cdalten@gmail.com>
Date2020-02-27 06:00 -0800
SubjectWriting Java code while drunk and stoned take 2
Message-ID<1747103f-c38a-411e-92de-a44a52d9c761@googlegroups.com>
I was drunk and stoned because my girlfriend had just dump me. And I needed something to take my mind off the situation. So I attacked one problem that I couldn't solve on coding bat. Here is a link to the problem..

https://codingbat.com/prob/p180759

And here is the problem statement...

Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".

zipZap("zipXzap") → "zpXzp"
zipZap("zopzop") → "zpzp"
zipZap("zzzopzop") → "zzzpzp"


But the solution that I somehow managed to come up with when I was drunk, stoned, depressed, and pissed off, is well, ugly...

Here is the solution

public String zipZap(String str) {
  String newString = "";
  
  if (str.length() < 3) {
    return str;
  }
  
  for (int i = 0; i < str.length() - 3; i++) {
    if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
      newString = newString +  "zp";
      i = i + 2;
    }
    else {
      newString = newString + str.charAt(i);
    }
    
  }
  
  for (int i = str.length() - 3; i < str.length(); i++ ) {
    if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
      newString = newString +  "zp";
      i = i + 2;
    }
    else {
      newString = newString + str.charAt(i);
    }
  }
  
  return newString;
}


The problem is that I have two loops. This is because I didn't know how to attack the case when I have to search for a pattern at the *end* of the string. If I use str.length(), I go out of bounds. So I ended up using a second loop to handle the end of the string case. 

My gut feeling says this isn't right. But I have no idea how this could be done in one loop.

[toc] | [next] | [standalone]


#39311

Fromnobody@example.org (Scott)
Date2020-02-27 16:39 +0000
Message-ID<5e57edbc.364432453@core>
In reply to#39310
On Thu, 27 Feb 2020 06:00:53 -0800 (PST), chad altenburg
<cdalten@gmail.com> wrote:

>I was drunk and stoned because my girlfriend had just dump me. And I needed=
> something to take my mind off the situation. So I attacked one problem tha=
>t I couldn't solve on coding bat. Here is a link to the problem..
>
>https://codingbat.com/prob/p180759
...
>The problem is that I have two loops. This is because I didn't know how to =
>attack the case when I have to search for a pattern at the *end* of the str=
>ing. If I use str.length(), I go out of bounds. So I ended up using a secon=
>d loop to handle the end of the string case.=20
>
>My gut feeling says this isn't right. But I have no idea how this could be =
>done in one loop.

You're close already. My solution omits the i++ clause from the for
statement - what you do with i during each iteration depends on
whether you found an instance of the pattern at str[i]. If you did,
you advance i by three to consume the pattern; if not, advance by one
to consume the non-matching character at that index. Append to
newString appropriately in either case. After the loop, anything left
in str is too short to match the pattern, so you just append it to
newString.

Two loops. My god, man, what would the neighbors say!?

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


#39312

FromEric Douglas <e.d.programmer@gmail.com>
Date2020-02-27 09:05 -0800
Message-ID<4467fe7d-2c91-47c8-98ac-caa1df1d32b4@googlegroups.com>
In reply to#39310
On Thursday, February 27, 2020 at 9:01:04 AM UTC-5, chad altenburg wrote:
> I was drunk and stoned because my girlfriend had just dump me. And I needed something to take my mind off the situation. So I attacked one problem that I couldn't solve on coding bat. Here is a link to the problem..
> 
> https://codingbat.com/prob/p180759
> 
> And here is the problem statement...
> 
> Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
> 
> zipZap("zipXzap") → "zpXzp"
> zipZap("zopzop") → "zpzp"
> zipZap("zzzopzop") → "zzzpzp"
> 
> 
> But the solution that I somehow managed to come up with when I was drunk, stoned, depressed, and pissed off, is well, ugly...
> 
> Here is the solution
> 
> public String zipZap(String str) {
>   String newString = "";
>   
>   if (str.length() < 3) {
>     return str;
>   }
>   
>   for (int i = 0; i < str.length() - 3; i++) {
>     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
>       newString = newString +  "zp";
>       i = i + 2;
>     }
>     else {
>       newString = newString + str.charAt(i);
>     }
>     
>   }
>   
>   for (int i = str.length() - 3; i < str.length(); i++ ) {
>     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
>       newString = newString +  "zp";
>       i = i + 2;
>     }
>     else {
>       newString = newString + str.charAt(i);
>     }
>   }
>   
>   return newString;
> }
> 
> 
> The problem is that I have two loops. This is because I didn't know how to attack the case when I have to search for a pattern at the *end* of the string. If I use str.length(), I go out of bounds. So I ended up using a second loop to handle the end of the string case. 
> 
> My gut feeling says this isn't right. But I have no idea how this could be done in one loop.

I'm sure there's a few ways to solve it.  If execution time doesn't matter just look for the most readable.  Not sure why you're on that particular site, we're not here to do homework for you, but here's the simplest I could come up with.

    public String zipZap(String str) {
        String s1 = "";
        String s2 = "";
        String s3 = "";
        for (int n = 0; n < str.length(); n++) {
            s1 = str.substring(n, n+1);
            if (!s3.endsWith("z") || !s1.equals("p")) {
                s3 += s2;
            }
            s2 = s1;
        }
        return s3 + s2;
    }

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


#39314

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2020-02-27 19:04 +0100
Message-ID<r390av$8ue$1@dont-email.me>
In reply to#39310
On 2020-02-27 15:00, chad altenburg wrote:
> I was drunk and stoned because my girlfriend had just dump me. And I needed something to take my mind off the situation. So I attacked one problem that I couldn't solve on coding bat. Here is a link to the problem..
> 
> https://codingbat.com/prob/p180759
> 
> And here is the problem statement...
> 
> Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
> 
> zipZap("zipXzap") → "zpXzp"
> zipZap("zopzop") → "zpzp"
> zipZap("zzzopzop") → "zzzpzp"
> 
> 
> But the solution that I somehow managed to come up with when I was drunk, stoned, depressed, and pissed off, is well, ugly...
> 
> Here is the solution
> 
> public String zipZap(String str) {
>   String newString = "";
>   
>   if (str.length() < 3) {
>     return str;
>   }
>   
>   for (int i = 0; i < str.length() - 3; i++) {
>     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
>       newString = newString +  "zp";
>       i = i + 2;
>     }
>     else {
>       newString = newString + str.charAt(i);
>     }
>     
>   }
>   
>   for (int i = str.length() - 3; i < str.length(); i++ ) {
>     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
>       newString = newString +  "zp";
>       i = i + 2;
>     }
>     else {
>       newString = newString + str.charAt(i);
>     }
>   }
>   
>   return newString;
> }
> 
> 
> The problem is that I have two loops. This is because I didn't know how to attack the case when I have to search for a pattern at the *end* of the string. If I use str.length(), I go out of bounds. So I ended up using a second loop to handle the end of the string case. 
> 
> My gut feeling says this isn't right. But I have no idea how this could be done in one loop.
> 

When you're drunk, stoned and heartbroken is *definitely* the best time
to learn about Regexes. You'll never want to be sober, or laid, again.

-- 
DF.

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


#39315

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2020-02-27 19:05 +0100
Message-ID<r390dc$8ue$2@dont-email.me>
In reply to#39314
On 2020-02-27 19:04, Daniele Futtorovic wrote:
> On 2020-02-27 15:00, chad altenburg wrote:
>> I was drunk and stoned because my girlfriend had just dump me. And I needed something to take my mind off the situation. So I attacked one problem that I couldn't solve on coding bat. Here is a link to the problem..
>>
>> https://codingbat.com/prob/p180759
>>
>> And here is the problem statement...
>>
>> Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
>>
>> zipZap("zipXzap") → "zpXzp"
>> zipZap("zopzop") → "zpzp"
>> zipZap("zzzopzop") → "zzzpzp"
>>
>>
>> But the solution that I somehow managed to come up with when I was drunk, stoned, depressed, and pissed off, is well, ugly...
>>
>> Here is the solution
>>
>> public String zipZap(String str) {
>>   String newString = "";
>>   
>>   if (str.length() < 3) {
>>     return str;
>>   }
>>   
>>   for (int i = 0; i < str.length() - 3; i++) {
>>     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
>>       newString = newString +  "zp";
>>       i = i + 2;
>>     }
>>     else {
>>       newString = newString + str.charAt(i);
>>     }
>>     
>>   }
>>   
>>   for (int i = str.length() - 3; i < str.length(); i++ ) {
>>     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
>>       newString = newString +  "zp";
>>       i = i + 2;
>>     }
>>     else {
>>       newString = newString + str.charAt(i);
>>     }
>>   }
>>   
>>   return newString;
>> }
>>
>>
>> The problem is that I have two loops. This is because I didn't know how to attack the case when I have to search for a pattern at the *end* of the string. If I use str.length(), I go out of bounds. So I ended up using a second loop to handle the end of the string case. 
>>
>> My gut feeling says this isn't right. But I have no idea how this could be done in one loop.
>>
> 
> When you're drunk, stoned and heartbroken is *definitely* the best time
> to learn about Regexes. You'll never want to be sober, or laid, again.
> 

PS: for added volcel effect, learn Perl while you're at it.

-- 
DF.

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


#39316

FromEric Douglas <e.d.programmer@gmail.com>
Date2020-02-27 10:25 -0800
Message-ID<97b4b790-6ff3-48d4-82ac-387f5a38a8d9@googlegroups.com>
In reply to#39314
On Thursday, February 27, 2020 at 1:04:27 PM UTC-5, Daniele Futtorovic wrote:
> On 2020-02-27 15:00, chad altenburg wrote:
> > I was drunk and stoned because my girlfriend had just dump me. And I needed something to take my mind off the situation. So I attacked one problem that I couldn't solve on coding bat. Here is a link to the problem..
> > 
> > https://codingbat.com/prob/p180759
> > 
> > And here is the problem statement...
> > 
> > Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
> > 
> > zipZap("zipXzap") → "zpXzp"
> > zipZap("zopzop") → "zpzp"
> > zipZap("zzzopzop") → "zzzpzp"
> > 
> > 
> > But the solution that I somehow managed to come up with when I was drunk, stoned, depressed, and pissed off, is well, ugly...
> > 
> > Here is the solution
> > 
> > public String zipZap(String str) {
> >   String newString = "";
> >   
> >   if (str.length() < 3) {
> >     return str;
> >   }
> >   
> >   for (int i = 0; i < str.length() - 3; i++) {
> >     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
> >       newString = newString +  "zp";
> >       i = i + 2;
> >     }
> >     else {
> >       newString = newString + str.charAt(i);
> >     }
> >     
> >   }
> >   
> >   for (int i = str.length() - 3; i < str.length(); i++ ) {
> >     if (str.charAt(i) == 'z' && str.charAt(i + 2) == 'p' ) {
> >       newString = newString +  "zp";
> >       i = i + 2;
> >     }
> >     else {
> >       newString = newString + str.charAt(i);
> >     }
> >   }
> >   
> >   return newString;
> > }
> > 
> > 
> > The problem is that I have two loops. This is because I didn't know how to attack the case when I have to search for a pattern at the *end* of the string. If I use str.length(), I go out of bounds. So I ended up using a second loop to handle the end of the string case. 
> > 
> > My gut feeling says this isn't right. But I have no idea how this could be done in one loop.
> > 
> 
> When you're drunk, stoned and heartbroken is *definitely* the best time
> to learn about Regexes. You'll never want to be sober, or laid, again.
> 
> -- 
> DF.

Study hard at that Java and within 10 short years of experience can land a nice job and easily get a better girlfriend.

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


#39317

FromArne Vajhøj <arne@vajhoej.dk>
Date2020-02-27 18:41 -0500
Message-ID<r39k3b$5hl$1@gioia.aioe.org>
In reply to#39310
On 2/27/2020 9:00 AM, chad altenburg wrote:
> I was drunk and stoned because my girlfriend had just dump me. And I needed something to take my mind off the situation. So I attacked one problem that I couldn't solve on coding bat. Here is a link to the problem..
> 
> https://codingbat.com/prob/p180759
> 
> And here is the problem statement...
> 
> Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
> 
> zipZap("zipXzap") → "zpXzp"
> zipZap("zopzop") → "zpzp"
> zipZap("zzzopzop") → "zzzpzp"
> 
> 
> But the solution that I somehow managed to come up with when I was drunk, stoned, depressed, and pissed off, is well, ugly...

There are many possible solutions.

One must be:

	public static String zipZap(String s) {
		return s.replaceAll("(z).(p)", "$1$2");
	}

Arne

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


#39319

FromJoerg Meier <joergmmeier@arcor.de>
Date2020-03-01 22:50 +0100
Message-ID<1tqy1ikulwdol.1kgu0mlctl0av.dlg@40tude.net>
In reply to#39317
On Thu, 27 Feb 2020 18:41:32 -0500, Arne Vajhøj wrote:

> 	public static String zipZap(String s) {
> 		return s.replaceAll("(z).(p)", "$1$2");
> 	}

Might just go with replaceAll("z.p", "zp"). Weird coding problems.

Liebe Gruesse,
		Joerg

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

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


#39320

FromArne Vajhøj <arne@vajhoej.dk>
Date2020-03-01 19:53 -0500
Message-ID<r3hlef$14g9$1@gioia.aioe.org>
In reply to#39319
On 3/1/2020 4:50 PM, Joerg Meier wrote:
> On Thu, 27 Feb 2020 18:41:32 -0500, Arne Vajhøj wrote:
>> 	public static String zipZap(String s) {
>> 		return s.replaceAll("(z).(p)", "$1$2");
>> 	}
> 
> Might just go with replaceAll("z.p", "zp"). Weird coding problems.

True.

The group substitution is only needed if there is more than
one possible start/end marker.

Arne

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


#39321

Fromchad altenburg <cdalten@gmail.com>
Date2020-03-03 04:54 -0800
Message-ID<8d21b2bb-009e-44b4-b873-8b4bd89ada95@googlegroups.com>
In reply to#39320
On Sunday, March 1, 2020 at 4:53:45 PM UTC-8, Arne Vajhøj wrote:
> On 3/1/2020 4:50 PM, Joerg Meier wrote:
> > On Thu, 27 Feb 2020 18:41:32 -0500, Arne Vajhøj wrote:
> >> 	public static String zipZap(String s) {
> >> 		return s.replaceAll("(z).(p)", "$1$2");
> >> 	}
> > 
> > Might just go with replaceAll("z.p", "zp"). Weird coding problems.
> 
> True.
> 
> The group substitution is only needed if there is more than
> one possible start/end marker.
> 

I'd like to point out that part of the problem stated that I had to use a for loop. Presumably because the author of this site wanted us, to like, uhh...learn how to use a for loop.

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


#39322

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2020-03-03 14:20 +0100
Message-ID<r3lljf$jk5$1@dont-email.me>
In reply to#39321
On 2020-03-03 13:54, chad altenburg wrote:
> I'd like to point out that part of the problem stated that I had to use a for loop. Presumably because the author of this site wanted us, to like, uhh...learn how to use a for loop.
> 

But you know perfectly well how to write a for-loop!

Anyway, this might do the trick:
for (int i = 0; i < str.length(); i++) {
    if (i <= str.length() - 3 && str.charAt(i) == 'z' && str.charAt(i +
2) == 'p' ) {
      newString += "zp";
      i += 2;
    }
    else {
      newString += str.charAt(i);
    }
}

-- 
DF.

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


#39323

FromJoerg Meier <joergmmeier@arcor.de>
Date2020-03-03 15:00 +0100
Message-ID<vp2mh3d4ongl.veeir29t4q89.dlg@40tude.net>
In reply to#39321
On Tue, 3 Mar 2020 04:54:55 -0800 (PST), chad altenburg wrote:

> On Sunday, March 1, 2020 at 4:53:45 PM UTC-8, Arne Vajhøj wrote:
>> On 3/1/2020 4:50 PM, Joerg Meier wrote:
>>> On Thu, 27 Feb 2020 18:41:32 -0500, Arne Vajhøj wrote:
>>>> 	public static String zipZap(String s) {
>>>> 		return s.replaceAll("(z).(p)", "$1$2");
>>>> 	}
>>> Might just go with replaceAll("z.p", "zp"). Weird coding problems.
>> True.

>> The group substitution is only needed if there is more than
>> one possible start/end marker.
> I'd like to point out that part of the problem stated that I had to use a for loop. Presumably because the author of this site wanted us, to like, uhh...learn how to use a for loop.

I'd like to point out that that isn't actually true. Neither your post nor
your link stated anything about using for loops. In fact, the following
line of code:

public String zipZap(String s) { return s.replaceAll("z.p", "zp"); }

leads to a successful "All Correct" response from your very own link.

Liebe Gruesse,
		Joerg

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

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


#39325

FromEric Douglas <e.d.programmer@gmail.com>
Date2020-03-03 08:19 -0800
Message-ID<94cabb13-6e8b-4d87-a33e-649ac92242af@googlegroups.com>
In reply to#39323
On Tuesday, March 3, 2020 at 9:01:02 AM UTC-5, Joerg Meier wrote:
> I'd like to point out that that isn't actually true. Neither your post nor
> your link stated anything about using for loops. In fact, the following
> line of code:
> 

If you plug in that single line solution it does show "all correct", though that page does show you're in the String-2 problems section.  If you go out to the main page https://codingbat.com/java it describes String-2 as "Medium String problems -- 1 loop". (technically you're still executing a loop if you call .replaceAll).

I think if you spell out the loop as I did it might be easier for a beginner to follow, and it's more flexible.  I wrote a loop like that in a filtering class for a JTextField which was a lot more complicated, validating every character by position against a mask.

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


#39326

Fromchad altenburg <cdalten@gmail.com>
Date2020-03-04 05:53 -0800
Message-ID<e195936c-970c-4025-a62e-f51be779d88b@googlegroups.com>
In reply to#39325
On Tuesday, March 3, 2020 at 8:19:34 AM UTC-8, Eric Douglas wrote:
> On Tuesday, March 3, 2020 at 9:01:02 AM UTC-5, Joerg Meier wrote:
> > I'd like to point out that that isn't actually true. Neither your post nor
> > your link stated anything about using for loops. In fact, the following
> > line of code:
> > 
> 
> If you plug in that single line solution it does show "all correct", though that page does show you're in the String-2 problems section.  If you go out to the main page https://codingbat.com/java it describes String-2 as "Medium String problems -- 1 loop". (technically you're still executing a loop if you call .replaceAll).
> 
> I think if you spell out the loop as I did it might be easier for a beginner to follow, and it's more flexible.  I wrote a loop like that in a filtering class for a JTextField which was a lot more complicated, validating every character by position against a mask.

Some of those problems aren't for beginners. Seriously. There is one section that has a series of problems related to backtracking. And the last time I checked, backtracking wasn't taught in an introductory programming class.

Actually, now that I think about it, I did see a backtracking algorithm during a compiler and construction theory class that I took during my junior year in college. But the professor did the algorithm for and then mentioned that such a topic a bit over our heads. He went on to say that he'd give anyone extra credit that could implement a backtracking algorithm such that the compiler could produce the correct error messages. 

There was only one person that accepted that challenge. She did it in three days. She ended up going on to studying Biochemistry at Harvard and is now some kind of Community Relations Manager at Facebook.

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


#39327

FromEric Douglas <e.d.programmer@gmail.com>
Date2020-03-04 09:59 -0800
Message-ID<71d76d2f-f3f1-4f84-b74b-0a871289b509@googlegroups.com>
In reply to#39326
On Wednesday, March 4, 2020 at 8:53:22 AM UTC-5, chad altenburg wrote:
> Some of those problems aren't for beginners. Seriously. There is one section that has a series of problems related to backtracking. And the last time I checked, backtracking wasn't taught in an introductory programming class.
> 
They all sound like beginner problems.  What section is that in?

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


#39336

Fromchad altenburg <cdalten@gmail.com>
Date2020-03-05 05:53 -0800
Message-ID<47e1894f-0349-4d5a-87e0-a6bca3e8aa9b@googlegroups.com>
In reply to#39327
On Wednesday, March 4, 2020 at 9:59:30 AM UTC-8, Eric Douglas wrote:
> On Wednesday, March 4, 2020 at 8:53:22 AM UTC-5, chad altenburg wrote:
> > Some of those problems aren't for beginners. Seriously. There is one section that has a series of problems related to backtracking. And the last time I checked, backtracking wasn't taught in an introductory programming class.
> > 
> They all sound like beginner problems.  What section is that in?

Again, I don't think these are beginner problems. Because again, I was never taught this kind of stuff in the lower division CS classes in undergrad school. For us, the beginner problems included simple for and while loops.

And the section with the backtracking problems are...

https://codingbat.com/java/Recursion-2

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


#39341

FromEric Douglas <e.d.programmer@gmail.com>
Date2020-03-05 09:29 -0800
Message-ID<7dd0aadc-2fdc-4eee-9bfd-fae490d4725b@googlegroups.com>
In reply to#39336
On Thursday, March 5, 2020 at 8:53:46 AM UTC-5, C.D. Altenburg, Ph.D, wrote:
> > They all sound like beginner problems.  What section is that in?
> 
> Again, I don't think these are beginner problems. Because again, I was never taught this kind of stuff in the lower division CS classes in undergrad school. For us, the beginner problems included simple for and while loops.
> 
> And the section with the backtracking problems are...
> 
> https://codingbat.com/java/Recursion-2

For the first problem in that section, not sure why they said do it with no loops?  The only obvious answer involves a loop.
public boolean groupSum(int start, int[] nums, int target) {
        if (target == 0) {
            return true;
        }
        if (start == nums.length - 1) {
            return nums[start] == target;
        }
        for (int n = start + 1; n < nums.length; n++) {
            if (groupSum(n,nums,target) || groupSum(n,nums,target-nums[start])) {
                return true;
            }
        }
        return false;
}

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


#39342

From"C.D. Altenburg, Ph.D," <cdalten@gmail.com>
Date2020-03-06 05:49 -0800
Message-ID<9e086d27-ba88-42d1-83ae-1ee2b9cfa2bd@googlegroups.com>
In reply to#39341
On Thursday, March 5, 2020 at 9:29:17 AM UTC-8, Eric Douglas wrote:
> On Thursday, March 5, 2020 at 8:53:46 AM UTC-5, C.D. Altenburg, Ph.D, wrote:
> > > They all sound like beginner problems.  What section is that in?
> > 
> > Again, I don't think these are beginner problems. Because again, I was never taught this kind of stuff in the lower division CS classes in undergrad school. For us, the beginner problems included simple for and while loops.
> > 
> > And the section with the backtracking problems are...
> > 
> > https://codingbat.com/java/Recursion-2
> 
> For the first problem in that section, not sure why they said do it with no loops?  The only obvious answer involves a loop.
> public boolean groupSum(int start, int[] nums, int target) {
>         if (target == 0) {
>             return true;
>         }
>         if (start == nums.length - 1) {
>             return nums[start] == target;
>         }
>         for (int n = start + 1; n < nums.length; n++) {
>             if (groupSum(n,nums,target) || groupSum(n,nums,target-nums[start])) {
>                 return true;
>             }
>         }
>         return false;
> }

That's what makes it hard. You're supposed to use no for loops. And here is the solution without a for loop

public boolean groupSum(int start, int[] nums, int target) {
  // Base case: if there are no numbers left, then there is a
  // solution only if target is 0.
  if (start >= nums.length) return (target == 0);
  
  // Key idea: nums[start] is chosen or it is not.
  // Deal with nums[start], letting recursion
  // deal with all the rest of the array.
  
  // Recursive call trying the case that nums[start] is chosen --
  // subtract it from target in the call.
  if (groupSum(start + 1, nums, target - nums[start])) return true;
  
  // Recursive call trying the case that nums[start] is not chosen.
  if (groupSum(start + 1, nums, target)) return true;
  
  // If neither of the above worked, it's not possible.
  return false;
}

Dumbass.

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


#39345

FromEric Douglas <e.d.programmer@gmail.com>
Date2020-03-06 07:00 -0800
Message-ID<7799632a-df74-48a1-81dd-926f653b3c38@googlegroups.com>
In reply to#39342
On Friday, March 6, 2020 at 8:49:23 AM UTC-5, C.D. Altenburg, Ph.D, wrote:
> 
> That's what makes it hard. You're supposed to use no for loops. And here is the solution without a for loop
> 
It does say no loops are needed.  It does not say no loops are allowed.  It did mark my guess as all correct.  If a loop is not allowed they should clarify the instructions and fix the answer key.

Is this site trying to train people to write working code, or to spend extra time coming up with code that specifically tries to not use available basic language functionality?  Forcing someone to write a method without a loop when a working readable solution can be found faster with one is more a puzzle site, less a training site.  I don't mind a good puzzle myself but this has no practical application.  Good luck to you if you intend to get a job in coding and this is your approach to actual work.

> 
> Dumbass.

Name calling is a sign of low intelligence.
Which ivy league school did you graduate from?
I don't know what kind of high income job requires an expensive ivy league education.  The best jobs near me certainly do not.

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


#39360

From"C.D. Altenburg, Ph.D," <cdalten@gmail.com>
Date2020-03-07 05:40 -0800
Message-ID<e4d1c0ca-a9ba-4bf7-84a0-f8394856aacb@googlegroups.com>
In reply to#39345
On Friday, March 6, 2020 at 7:01:04 AM UTC-8, Eric Douglas wrote:
> On Friday, March 6, 2020 at 8:49:23 AM UTC-5, C.D. Altenburg, Ph.D, wrote:
> > 
> > That's what makes it hard. You're supposed to use no for loops. And here is the solution without a for loop
> > 
> It does say no loops are needed.  It does not say no loops are allowed.  It did mark my guess as all correct.  If a loop is not allowed they should clarify the instructions and fix the answer key.
> 
> Is this site trying to train people to write working code, or to spend extra time coming up with code that specifically tries to not use available basic language functionality?  Forcing someone to write a method without a loop when a working readable solution can be found faster with one is more a puzzle site, less a training site.  I don't mind a good puzzle myself but this has no practical application.  Good luck to you if you intend to get a job in coding and this is your approach to actual work.
> 

I work as a Software Engineer for a firm in San Francisco.

> > 
> > Dumbass.
> 
> Name calling is a sign of low intelligence.
> Which ivy league school did you graduate from?

I didn't. Just like you, I don't have what it takes to get into Harvard or Princeton.

> I don't know what kind of high income job requires an expensive ivy league education.  The best jobs near me certainly do not.

Go back to Burger King

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


Page 1 of 2  [1] 2  Next page →

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


csiph-web