Avatar of artemkorsakov

artemkorsakov's solution

to Acronym in the Java Track

Published at Jan 29 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

Julien Vanier https://github.com/monkbroc

Submitting Incomplete Solutions

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

AcronymTest.java

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

import static org.junit.Assert.assertEquals;

public class AcronymTest {

    @Test
    public void basic() {
        String phrase = "Portable Network Graphics";
        String expected = "PNG";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

    @Ignore("Remove to run test")
    @Test
    public void lowercaseWords() {
        String phrase = "Ruby on Rails";
        String expected = "ROR";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

    @Ignore("Remove to run test")
    @Test
    public void punctuation() {
        String phrase = "First In, First Out";
        String expected = "FIFO";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

    @Ignore("Remove to run test")
    @Test
    public void nonAcronymAllCapsWord() {
        String phrase = "GNU Image Manipulation Program";
        String expected = "GIMP";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

    @Ignore("Remove to run test")
    @Test
    public void punctuationWithoutWhitespace() {
        String phrase = "Complementary metal-oxide semiconductor";
        String expected = "CMOS";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

    @Ignore("Remove to run test")
    @Test
    public void veryLongAbbreviation() {
        String phrase = "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me";
        String expected = "ROTFLSHTMDCOALM";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

    @Ignore("Remove to run test")
    @Test
    public void consecutiveDelimiters() {
        String phrase = "Something - I made up from thin air";
        String expected = "SIMUFTA";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

    @Ignore("Remove to run test")
    @Test
    public void apostrophes() {
        String phrase = "Halley's Comet";
        String expected = "HC";
        assertEquals(expected, new Acronym(phrase).getAcronym());
    }

}
import java.util.Arrays;
import java.util.Objects;

class Acronym {

    private String[] words;

    Acronym(String phrase) {
        words = phrase.split("[^a-zA-Z']");
    }

    String getAcronym() {
        StringBuilder sb = new StringBuilder();
        Arrays.stream(words).filter(Objects::nonNull).forEach(w -> sb.append(w.charAt(0)));
        return sb.toString().toUpperCase();
    }

}

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?