This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Java searching algorithm help  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I implement this searching algorithm in java in here?

public boolean equal(String s, String t, int n) {
     
   }
Screenshot-2019-03-13-at-13.26.31.png
[Thumbnail for Screenshot-2019-03-13-at-13.26.31.png]
 
Saloon Keeper
Posts: 5289
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like an exercise you should be doing in order to learn something. So: what do you have so far, and where are you stuck making progress? If you haven't started, what ideas have you had?
 
a syed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:That looks like an exercise you should be doing in order to learn something. So: what do you have so far, and where are you stuck making progress? If you haven't started, what ideas have you had?



I've done this so far:


Which fulfils if both have the same length and the same characters. but how do I return true if the first n elements of the two arrays are equal, or, if either is shorter than n.
 
Sheriff
Posts: 5750
149
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you will need a loop.  Have you learned about those?

Also, when you are tempted to write

if (something) {
 return ture;
} else {
 return false;
}


...just write instead:
return something;
 
a syed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:It looks like you will need a loop.  Have you learned about those?

Also, when you are tempted to write

if (something) {
 return ture;
} else {
 return false;
}


...just write instead:
return something;



if this fine?

public boolean equal(String s, String t, int n) {
       for (int i=0; i<n; i++){
           if (s.length() != t.length() && s.charAt(i) != t.charAt(i)){
                   return false;
           }
       }return true;
   }

Gives a correct output.
 
Knute Snortum
Sheriff
Posts: 5750
149
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, because the "if" doesn't have an "else".
 
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sy ab wrote:. . . if this fine? . . ..

No. Look at the old sun style guide. You should write return s1.length() == ...;
And what happens when you try running ...equals("bind", "band", 5) ?
Try to organise your boolean expressions so they evaluate to true because that will make the code much easier to read than if the expression evaluates to false.
Always use the code button; because you are new (welcome to the Ranch ) I edited your first lot of code and doesn't it look better 
 
a syed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I do this one, So far I've done:

public boolean lessThan(String s, String t, int n) {
       for (int i=0; i<n; i++) {
           if (s.charAt(i) != t.charAt(i)) {
               return true;
           }
       }return false;
   }
Im correct for the first 3 Bullet  but don't know what to do for the last 2.
Screenshot-2019-03-13-at-13.26.31.png
[Thumbnail for Screenshot-2019-03-13-at-13.26.31.png]
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sy ab wrote:. . . Im correct for the first 3 Bullet . . .

No, that isn't correct code. It gives the expected answer for the first case more by luck than good management.
 
a syed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

sy ab wrote:. . . if this fine? . . ..

No. Look at the old sun style guide. You should write return s1.length() == ...;
And what happens when you try running ...equals("bind", "band", 5) ?
Try to organise your boolean expressions so they evaluate to true because that will make the code much easier to read than if the expression evaluates to false.
Always use the code button; because you are new (welcome to the Ranch ) I edited your first lot of code and doesn't it look better 



I get true for bind band 4 which should be false.
what is wrong with m code?
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start, you are getting confused with your if‑elses.

Also, more important, you are trying to write code before you have worked out what you are going to do. Your code looks as if you were guessing about the algorithm, not as if you who had thought it through and got the algorithm right. You can guess 1,000,000 different times and there is a very good chance of one of the guesses being correct. Or you can work it out correctly and get it right first time.
 
a syed
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:For a start, you are getting confused with your if‑elses.

Also, more important, you are trying to write code before you have worked out what you are going to do. Your code looks as if you were guessing about the algorithm, not as if you who had thought it through and got the algorithm right. You can guess 1,000,000 different times and there is a very good chance of one of the guesses being correct. Or you can work it out correctly and get it right first time.



I understand what I have to do but I don't know how to implement it in java correctly. So where do I start?
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saying, “I don't know…,” is a very good start.

  • 1: Write down in words of one syllable what you think the assignment means.
  • 2: Write down in words of one syllable as far as possible how you would do it but not using any computing words.
  • 3: Change that to pseudo‑code.
  • 4: Once you get to pseudo‑code, you will find the real code much easier to construct
  •  
    Campbell Ritchie
    Marshal
    Posts: 63496
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maybe this is what the assignment means:-

    First, see if the two words are the same length.
    Then go through the two words as far as you have been told to, and see if the char in word 1 at that place is the same as the char at the same place in word 2.
    You get true if both tests pass; if you get one test to pass and one test fails, the whole thing counts as false.



    Just in case you didn't believe it could be done in words of one syllable You need to make it read simply enough that even I can follow it.
     
    Marshal
    Posts: 6595
    443
    BSD Linux Mac OS X VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'd like to highlight something about the given instructions and suggested method declaration. public boolean equal(String s, String t, int n).

    I understand you need to stick to that, but bear in mind, that parameter names of given method are poor and don't suggest anything about its functionality, they mislead you actually. "s" and "t" are especially confusing, because "s" somehow indirectly in my head is mapped to "seconds", while "t" to a "time". I guess I'm not the only one like that. Think for yourself how you could name them better. "word1" "word2" would be much better, even though not that great either. "n" could have been named "charactersDiffThreshold" or similar - so you'd get at least little idea what is the method about.

    Another thing, instructions says:

    This method should return true if the first n elements of the two arrays are equal...


    That's misleading too. I don't see any arrays in this method declaration, just Strings which need to be compared. I see where instructor is coming from, from String's implementation point of view, however, many of us believe, that going down that route teaching student about mechanics shifts students away from understanding things in more abstract way how to attack problem.

    And indeed in your solution you don't see any hints about the arrays, because they are implementation detail how Strings are implemented in Java, an array of chars.

    Such instructions would have read better in my opinion as:

    This method should return true if the first n characters of the two given strings...




    My point of this post was, that don't be afraid to challenge and question instructor and ask for clarification. This is what I'd have expected to see from you as for confusion: "I don't get, what's about the arrays in instructions...". To me those instructions were hard to understand to be honest, even though actual stuff is fairly simple.
     
    a syed
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Maybe this is what the assignment means:-

    First, see if the two words are the same length.
    Then go through the two words as far as you have been told to, and see if the char in word 1 at that place is the same as the char at the same place in word 2.
    You get true if both tests pass; if you get one test to pass and one test fails, the whole thing counts as false.



    Just in case you didn't believe it could be done in words of one syllable You need to make it read simply enough that even I can follow it.



    is my pseudocode fine?

    IF [Word1length]=[Word2length]
    AND
    Where i=1 i=i+1
    IF Char(i)Word1=Char(i)Word2
    return true


     
    Campbell Ritchie
    Marshal
    Posts: 63496
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No. You are all right with the first part, You need to take the result of the first part and incorporate it into your loop.
     
    a syed
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:No. You are all right with the first part, You need to take the result of the first part and incorporate it into your loop.



    public boolean equal(String s, String t, int n) {
           // replace the following line with your implementation
               for (int i=0; i<n; i++){ >
                   if (s.charAt(i) != t.charAt(i)){
                           return false;
               }else if (s.length()<n||t.length()><n){ >
                       return true;
                   }
           }return true;
       }

    this works
     
    Campbell Ritchie
    Marshal
    Posts: 63496
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    a syed wrote:. . .

    this works

    No it doesn't. Even if you remove the > symbols which have mysteriously appeared, you haven't tried it with inputs like “blank” “blankety‑blank” and 6. Or 6> like this: 6
     
    a syed
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    a syed wrote:. . .

    this works

    No it doesn't. Even if you remove the > symbols which have mysteriously appeared, you haven't tried it with inputs like “blank” “blankety‑blank” and 6. Or 6> like this: 6



    I don't why the > appeared

    here's my code:



    its meant to return true only if the n characters are the same and if n is larger than the number of characters then it should return true if the shorter words are the same words.
    but it doesn't work
     
    Campbell Ritchie
    Marshal
    Posts: 63496
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    a syed wrote:. . . I don't why the > appeared

    Nor do I; I shall see whether anybody else has any ideas. That sort of thing has happened before

    . . . it doesn't work

    No, it doesn't does it?

    You haven't, I don't think, explained what you are supposed to do in the dreaded words of one syllable (and I showed, just for the fun of it, that you can use words of one syllable). I don't think you have explained how to do it. Nor have you corrected the second half of your pseudo‑code; I only said the first half was correct.
     
    a syed
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    a syed wrote:. . . I don't why the > appeared

    Nor do I; I shall see whether anybody else has any ideas. That sort of thing has happened before

    . . . it doesn't work

    No, it doesn't does it?

    You haven't, I don't think, explained what you are supposed to do in the dreaded words of one syllable (and I showed, just for the fun of it, that you can use words of one syllable). I don't think you have explained how to do it. Nor have you corrected the second half of your pseudo‑code; I only said the first half was correct.



    Sorry man I'm new to this, can you give me the solution so I can try it and learn from it.
     
    Campbell Ritchie
    Marshal
    Posts: 63496
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You would learn nothing from a solution given you.
     
    pie. tiny ad:
    Become a Java guru with IntelliJ IDEA
    https://www.jetbrains.com/idea/
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!