experchange > java

chad altenburg (02-27-20, 04:00 PM)
I was drunk and stoned because my girlfriend had just dump me. And I neededsomething 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..



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, themiddle 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.
Scott (02-27-20, 06:39 PM)
On Thu, 27 Feb 2020 06:00:53 -0800 (PST), chad altenburg
<cdalten> 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..
> ....
>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!?
Eric Douglas (02-27-20, 07:05 PM)
On Thursday, February 27, 2020 at 9:01:04 AM UTC-5, chad altenburg wrote:
[..]
> }
> 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;
}
Daniele Futtorovic (02-27-20, 08:04 PM)
On 2020-02-27 15:00, chad altenburg wrote:
[..]
> }
> 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.
Daniele Futtorovic (02-27-20, 08:05 PM)
On 2020-02-27 19:04, Daniele Futtorovic wrote:
> On 2020-02-27 15:00, chad altenburg wrote:
> 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.
Eric Douglas (02-27-20, 08:25 PM)
On Thursday, February 27, 2020 at 1:04:27 PM UTC-5, Daniele Futtorovic wrote:
> On 2020-02-27 15:00, chad altenburg wrote:
> 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.
Arne Vajhøj (02-28-20, 01:41 AM)
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..
>
> 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
Joerg Meier (03-01-20, 11:50 PM)
On Thu, 27 Feb 2020 18:41:32 -0500, Arne Vajhj 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
Arne Vajhøj (03-02-20, 02:53 AM)
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
chad altenburg (03-03-20, 02:54 PM)
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:
> 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.
Daniele Futtorovic (03-03-20, 03:20 PM)
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);
}
}
Joerg Meier (03-03-20, 04:00 PM)
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 Vajhj wrote:


>> 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
Eric Douglas (03-03-20, 06:19 PM)
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", thoughthat page does show you're in the String-2 problems section. If you go out to the main page 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.
chad altenburg (03-04-20, 03:53 PM)
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 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 threedays. She ended up going on to studying Biochemistry at Harvard and is nowsome kind of Community Relations Manager at Facebook.
Eric Douglas (03-04-20, 07:59 PM)
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?

Similar Threads