0

I am practicing with codewars and started doing my first challenge that is about the following:

The set of words is given. Words are joined if the last letter of one word and the first letter of another word are the same. Return true if all words of the set can be combined into one word. Each word can and must be used only once. Otherwise return false.

Input Array of 3 to 7 words of random length. No capital letters.

Example true Set: excavate, endure, desire, screen, theater, excess, night. Millipede: desirE EndurE ExcavatE ExcesS ScreeN NighT Theater.

Example false Set: trade, pole, view, grave, ladder, mushroom, president. Millipede: presidenT Trade.

Here is my code :

public class Millipede {
  public static boolean check(String[] millipede) {
            boolean itCombines = true;
            String setOfWords1 = "";
            String setOfWords2 = "";
            for (int i = 0; i < millipede.length - 1; i++) {

                if (!millipede[i].equals(millipede[i + 1])) {
                    setOfWords1 = millipede[i];
                    setOfWords2 = millipede[i + 1];
                    if (setOfWords1.charAt(setOfWords1.length() - 1) == setOfWords2.charAt(0)) {
                            return itCombines;
                    } else {
                        return !itCombines;
                    }
                    }
                } return itCombines;
            } 
  }

When I click on the TEST button, everything goes fine and all tests pass, but when I click on the ATTEMPT button it is failing 3 but passing 4.

FAILS:
fiveWords()
screen, desire, theater, excess, night ==> expected: <true> but was: <false>

randomTests()
engine, entertainment, extract, thesis ==> expected: <false> but was: <true>

oneLetterWords()
east, e, e, t, t, e, time ==> expected: <true> but was: <false>

I was trying to use the Random class to get a random index in order to compare Strings in different positions but it was making the test to fail.

starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    If you're a beginner at Java, then Codewars is *not* studying. It's not even a substitute for studying. Studying is reading tutorials or books or other actual learning material. Codewars is something you do for fun once you already understand the language and tooling. You will not master Java by solving isolated coding problems that test all of the weird parts of the language. You will master Java by writing real, useful programs that solve real problems. – Silvio Mayolo Dec 27 '22 at 19:34
  • @SilvioMayolo I disagree with this advice, solving coding problems will make you better with the language and a better engineer in general. Saying it's "not studying" is wrong. – Kon Dec 27 '22 at 19:42
  • There are easier sites for beginners, like codingbat.com. – Nathan Hughes Dec 27 '22 at 20:17
  • Thank you for the comments and a really appreciate all the advices here. I went to code wars as is the only reference I have to practice and keep my mind busy. And actually, I was just thinking about to get involved into coding with actual codes instead of readings and other supporting materials. I'll try to solve this and then go to the other websites that are "easier" for a better start up – Luis Lopez Dec 29 '22 at 05:41

1 Answers1

-1

Did you start your journey the right way. Going straight into codewars is challenging and frustrating for green pals.

And I can see in your code you made quite a mistake. You basically check if next word is acceptable to be joined, if it is, you return true. However, you are supposed to check all words and all possible combinations between them and only then return true or false(one of the possible approaches).

Try to solve simpler questions and learn through books/tutorials and solving those problems for now are not the key way to learn programming as a beginner. There are many sources that can help you for free: youtube/geek4geeks/tutorialspoint/baeldung and more

Aitsuken
  • 126
  • 10