• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

array123

 
Ranch Hand
Posts: 842
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying below
https://codingbat.com/prob/p136041

i failed one test


can you point me to what mistakes i am doing?
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the poor formatting without spaces round your binary operators, you mean?
Use a pencil and paper and write down the course of execution for one of the examples you got wrong. It is particularly the loop you need to follow.
 
Rancher
Posts: 4754
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can the code set res to false after it has set res to true?
 
Bartender
Posts: 4977
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is not final.

If an element is 123, does that count?
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can the code set res . . . after . . .

That is what going through the loop will show.

Piet Souris wrote:. . . 123 . . .

No, that doesn't count.
 
Norm Radder
Rancher
Posts: 4754
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now we wait until CB comes and shows the OP what code changes are needed to fix the problem.
 
Piet Souris
Bartender
Posts: 4977
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Rancher
Posts: 1029
27
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,

you are trying to show existence, so start with your found flag set to false and only set it to true when you find the occurrences you are looking for, but never reset it (during each trial).  make the changes you need to continue through your look and continue checking or use "break" to exit the loop because your case has been met for true (what you are trying to show).

sai rama krishna wrote:Trying below
https://codingbat.com/prob/p136041

i failed one test


can you point me to what mistakes i am doing?

 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful; I think we have given enough hints.
 
sai rama krishna
Ranch Hand
Posts: 842
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. I got it. it worked as below

 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if you delete lines 8‑10?
 
sai rama krishna
Ranch Hand
Posts: 842
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just deleted as below

It worked fine as for expressions not satisfying above if conditions default res false value seems getting returned
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:. . . It worked fine as for expressions not satisfying above if conditions default res false value seems getting returned

Don't understand that bit, but if you delete those three lines and the break; I would expect your program to give the same results. Of course it is more efficient to exit the loop as soon as possible
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:. . .. . .

Find the old Sun coding conventions and it tells you not to use if‑else to return values. I think you should take the same approach to assignments. You can omit the if‑else altogether like this:-Spacing corrected. Please note where the spaces belong. Of course we already know that code is incorrect.
 
sai rama krishna
Ranch Hand
Posts: 842
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting old sun link. Thank you for sharing.


I mean to say below code passed all tests



Like you mentioned let me refactor to remove those if else statements as well
 
sai rama krishna
Ranch Hand
Posts: 842
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Above refactored code failing some reason one test


whereas below further refactored code passing all tests





Looks like refactored code 1 is checking whole array which is not right approach for this challenge.
Refactored code 2 check part of array and as soon as finds 1,2, 3 next to each other returns true and program exited
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:. . . Looks like refactored code 1 is checking whole array which is not right approach for this challenge. . . .

Well...., sort of......

What is happening in the frist version is that you continued testing after you found 123; you have been told about that error several times. It means you only get the result for the last three elements in the array.
 
Piet Souris
Bartender
Posts: 4977
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution assumes that the sequence 1, 2, 3 should be consecutive. What if that need not be the case, so that say 1,5,2,7,3 would also be true?
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Your solution assumes that the sequence 1, 2, 3 should be consecutive. . . .

That is how I would read the question.
 
sai rama krishna
Ranch Hand
Posts: 842
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. Lesson learnt Never test further when found required result

If 1, 2, 3 comes in random positions my solution will not work. May be I need one other for loop. I have to try that test scenario
 
Piet Souris
Bartender
Posts: 4977
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My thougt was that a substring is always a subsequence, but not necessarily the other way round.

@Sai
a way could be that a 2 only counts if a 1 has been seen, and a 3 only if a 2 has been seen.
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:. . . If 1, 2, 3 comes in random positions . . .

They aren't random, but in sequence. The sequence may be consecutive or interrupted by other values.

May be I need one other for loop. . . .

You don't. It is straightforward enough with a single for‑each loop.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic