• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringMatch Challenge

 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying below challenge
https://codingbat.com/prob/p198640
I wrote my code as below and failing few tests




can you please point me to my mistakes. To my eyes my code looks perfect but apparently not as per codingbat.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post comments on what the code is expected to do.  No link to another site.
Have you tried playing computer with a piece of paper and a pencil to see what that code does?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. Basically need to find count of match of 2 adjacent characters at same location
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Have you tried playing computer with a piece of paper and a pencil to see what that code does?



What are the values in the report?   The two numbers in green.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying in eclipse with debug mode on to play and see test results and flow.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The site you linked gives an example (I think, it doesn't actually say so):

stringMatch("abc", "abc") → 2



Which is clearly correct. But your post says

stringMatch("abc", "abc") → 2   0   X



Is that supposed to mean that your code returns 2, and there's a zero in there for no apparent reason, and then there's an X which says that 2 is wrong? The example says the answer is 2, reading the requirements says that the answer is 2, your code returns 2, so what's the problem?

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a well-known error here. Did you notice that all your counts are 0?

Put a println for count at line 6.5, and see what you get.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed all my results wrong coming 0. Trying to resolve that issue
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is first time I ever saw and wrote code with 2 checks in for loop both a length and b length checks. Is this type e of multiple condition checks are common in main stream coding?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you found and fixed the problem yet?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not yet. Just debugged in eclipse still noticing count 0. Looks like count variable declaration in local method vs instance variable vs static variable type of issue
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran out of all my ideas. Not sure why this is not working
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does the code change the value of count?  Is that statement being executed?
What is the value in count after that statement is executed?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 6

 count=count++;

this is where it is incrementing by 1
let me try to change count =count+1 if that makes any difference
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


wow it passed all tests like above.

May be count=count++ is not correct way to assign? But count = count+1 is correct way?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

count=count++ is not correct


Look at the definition of what the ++ postfix operator does.  Think about your statement and how it worked with that operator.
Describe here the steps that would be taken by that statement.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or have a look here: wiki
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree
count++ is post increment operator so always assign first to left hand side variable then increments.

I think it is safer to write
count=count+1;
even though it is little longer bug more bug proof code
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you should simply write count++;
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.

It worked as below


as long as below assignment is not done it seems fine
count=count++

If I do above every time 0 value is coming
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sai, I feel like you need to put some effort into your own debugging. We're here if you get stumped, but we shouldn't be you're first line of defense.

The easiest debugging method is to insert println() statements in your code to see what it's ACTUALLY doing.
That should at least narrow the problem down.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:I think it is safer to write
count=count+1;
even though it is little longer bug more bug proof code



That's what I do. Only I write "count += 1".
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree count += 1 or count=count++; also good approaches. Thank you all for your help with this challenge
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:. . . count += 1 or count=count++; . . .

No, one of them has already been shown to be incorrect.

Thank you all . . .

That's a pleasure
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sai rama,

Define your problem--what you want to do succinctly before you ever cut that very first line of code.  Do you understand what it is that you have to do? This is my take on it, and I'm pretty sure it's right.

Given two strings, check ever 2 character substring to see if it is the same in both. So you take the 1st 2 characters of string a and compare them to string b, if they match, you increment a count.  Move over 1 character and do the same thing again.  Do that until you have checked the last 2 characters of the shortest string.

You are walking down a String a character at a time (look in the String API and what methods my be handy)
hint: you may want to use a looping structure to give an index to walk down the String.
You are breaking a String into subStrings (look in the String API and see what to use to manipulate Strings).
Look at your data types: you are comparing Strings (look in the String API and see what to use to compare Strings).

Please look closely at the String equals method.

Les
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Les and all thank you for your great feedback. I am holding book and pencil now and make this my lifetime habit as long as i code.

once i change
count=count++ to
count=count+1

i see all tests passed. Check java 17 se api to find Sting Builder methods to follow that approach and keep you all posted soon
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it .

Is it safe to say
count++
and count=count+1;
are same thing but just different way of writing?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"n = n + 1" says "I'm a noob". It tells the reader "STOP, and read this VERY carefully because it is not normal syntax". Slows down the process of reading lots of lines of code. (like we do here every day)

Use  "n++". Don't fight it, get used to it, it's the way we do things.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thank you
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:"n = n + 1" says "I'm a noob". It tells the reader "STOP, and read this VERY carefully because it is not normal syntax". Slows down the process of reading lots of lines of code. (like we do here every day)

Use  "n++". Don't fight it, get used to it, it's the way we do things.


Sorry, but that is nonsense.

a += b is shorthand for a = a + b, and a++ is shorthand for a += 1. If you want to live with shorthands, don't use java in the first place.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Agree
count++ is post increment operator so always assign first to left hand side variable then increments. . . .

That might give the same result but it is not a correct explanation. The correct explanation is to be found in the JLS (=Java® Language Specification) here.

That JLS section wrote:The value of the postfix increment expression is the value of the variable before the new value is stored.

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:

Carey Brown wrote:"n = n + 1" says "I'm a noob". It tells the reader "STOP, and read this VERY carefully because it is not normal syntax". Slows down the process of reading lots of lines of code. (like we do here every day)

Use  "n++". Don't fight it, get used to it, it's the way we do things.


Sorry, but that is nonsense.

a += b is shorthand for a = a + b, and a++ is shorthand for a += 1. If you want to live with shorthands, don't use java in the first place.


Sorry, but that is nonsense.
Complete lineage:
n = n + 1
n += 1
n++
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but that is nonsense.

To prevent an infinite loop, you can have one more go and then we stop.    
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Sai

What happens if you leave out line 3 and a.length() is 1?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, looks like that test is not  necessary.
When I saw the requirements this is how I envisioned it;

 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A java 8+ version of this:

Your IDE will do the static imports for you.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lot of different ways of solving same challenge. To my eyes java 8 syntax looks less code but more hard to read. May be i have not used it often enough
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:A java 8+ version of this:

Your IDE will do the static imports for you.

I'm using Eclipse 2021-12 and it did not offer to include imports, static or otherwise.

Additionally, codebat.com won't let you add import statements at all so you have to use fully qualified names (ugh).

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic