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


Groups > comp.lang.java.programmer > #39315

Re: Writing Java code while drunk and stoned take 2

Path csiph.com!4.us.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Writing Java code while drunk and stoned take 2
Date Thu, 27 Feb 2020 19:05:26 +0100
Organization A noiseless patient Spider
Lines 64
Message-ID <r390dc$8ue$2@dont-email.me> (permalink)
References <1747103f-c38a-411e-92de-a44a52d9c761@googlegroups.com> <r390av$8ue$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Injection-Date Thu, 27 Feb 2020 18:05:32 -0000 (UTC)
Injection-Info reader02.eternal-september.org; posting-host="93fb997cca3c8a80b7e25df1f942cafc"; logging-data="9166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19W+U/CrOFtGp19neyhBo7N"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0
Cancel-Lock sha1:S7Ddm5S70x86zBmp8Y6m/a2csq4=
In-Reply-To <r390av$8ue$1@dont-email.me>
X-Antivirus-Status Clean
Content-Language en-US
X-Antivirus AVG (VPS 200226-0, 02/26/2020), Outbound message
Xref csiph.com comp.lang.java.programmer:39315

Show key headers only | View raw


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.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web