Avatar of Albert-Wang-Hipmunk

Albert-Wang-Hipmunk's solution

to Acronym in the Kotlin Track

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

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).

Setup

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

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

Making the test suite pass

Execute the tests with:

$ gradlew 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 annotation.

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.kt

import org.junit.Test
import org.junit.Ignore
import kotlin.test.assertEquals

class AcronymTest {

    @Test
    fun fromTitleCasedPhrases() {
        val phrase = "Portable Network Graphics"
        val expected = "PNG"
        assertEquals(expected, Acronym.generate(phrase))
    }

    @Ignore
    @Test
    fun fromOtherTitleCasedPhrases() {
        val phrase = "Ruby on Rails"
        val expected = "ROR"
        assertEquals(expected, Acronym.generate(phrase))
    }

    @Ignore
    @Test
    fun fromPhrasesWithPunctuation() {
        val phrase = "First In, First Out"
        val expected = "FIFO"
        assertEquals(expected, Acronym.generate(phrase))
    }

    @Ignore
    @Test
    fun fromNonAcronymAllCapsWord() {
        val phrase = "GNU Image Manipulation Program"
        val expected = "GIMP"
        assertEquals(expected, Acronym.generate(phrase))
    }

    @Ignore
    @Test
    fun fromPhrasesWithPunctuationAndSentenceCasing() {
        val phrase = "Complementary metal-oxide semiconductor"
        val expected = "CMOS"
        assertEquals(expected, Acronym.generate(phrase))
    }

    @Ignore
    @Test
    fun fromVeryLongAbbreviation() {
        val phrase = "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"
        val expected = "ROTFLSHTMDCOALM"
        assertEquals(expected, Acronym.generate(phrase))
    }

    @Ignore
    @Test
    fun fromConsecutiveDelimiters() {
        val phrase = "Something - I made up from thin air"
        val expected = "SIMUFTA"
        assertEquals(expected, Acronym.generate(phrase))
    }
}
class Acronym {
    companion object {
        fun generate(phrase: String): String {
            return phrase.split(Regex("[\\s,-]")).filter { it.isNotEmpty() }.map { it.first().toUpperCase() }.joinToString("")
        }
    }
}

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?