Avatar of mastershif

mastershif's solution

to Isogram in the Java Track

Published at Oct 20 2019 · 0 comments
Instructions
Test suite
Solution

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

Tips

If you find that testWorksWithGermanLetters fails even though you think you've implemented it correctly, this could be because of the JVM encoding. Try setting the JAVA_TOOL_OPTIONS environment variable to -Dfile.encoding=UTF8 to fix this.

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

Wikipedia https://en.wikipedia.org/wiki/Isogram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

IsogramCheckerTest.java

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.*;

public class IsogramCheckerTest {

    @Test
    public void testEmptyString() {
        IsogramChecker iso = new IsogramChecker();
        assertTrue(iso.isIsogram(""));
    }

    @Ignore("Remove to run test")
    @Test
    public void testLowercaseIsogram() {
        IsogramChecker iso = new IsogramChecker();
        assertTrue(iso.isIsogram("isogram"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testNotIsogram() {
        IsogramChecker iso = new IsogramChecker();
        assertFalse(iso.isIsogram("eleven"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testDuplicateEndAlphabet() {
        IsogramChecker iso = new IsogramChecker();
        assertFalse(iso.isIsogram("zzyzx"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testMediumLongIsogram() {
        IsogramChecker iso = new IsogramChecker();
        assertTrue(iso.isIsogram("subdermatoglyphic"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testCaseInsensitive() {
        IsogramChecker iso = new IsogramChecker();
        assertFalse(iso.isIsogram("Alphabet"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testDuplicatMixedCase() {
        IsogramChecker iso = new IsogramChecker();
        assertFalse(iso.isIsogram("alphAbet"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testIsogramWithHyphen() {
        IsogramChecker iso = new IsogramChecker();
        assertTrue(iso.isIsogram("thumbscrew-japingly"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testIsogramWithDuplicatedCharAfterHyphen() {
        IsogramChecker iso = new IsogramChecker();
        assertFalse(iso.isIsogram("thumbscrew-jappingly"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testIsogramWithDuplicatedHyphen() {
        IsogramChecker iso = new IsogramChecker();
        assertTrue(iso.isIsogram("six-year-old"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testMadeUpNameThatIsAnIsogram() {
        IsogramChecker iso = new IsogramChecker();
        assertTrue(iso.isIsogram("Emily Jung Schwartzkopf"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testDuplicatedCharacterInTheMiddleIsNotIsogram() {
        IsogramChecker iso = new IsogramChecker();
        assertFalse(iso.isIsogram("accentor"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSameFirstAndLast() {
        IsogramChecker iso = new IsogramChecker();
        assertFalse(iso.isIsogram("angola"));
    }

}
import java.util.Arrays;

class IsogramChecker {

    boolean isIsogram(String phrase) {
        char[] inputArray = phrase.toCharArray();
        Arrays.sort(inputArray);
        for (int i = 0; i < inputArray.length - 1; i++) {
            if (isLetter(inputArray[i]) 
                    && Character.toLowerCase(inputArray[i]) == Character.toLowerCase(inputArray[i + 1])) {
                return false;
            }
        }
        return true;
    }
    
    boolean isLetter(char c) {
        return Character.isLetter(c);
    }

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?