Avatar of maunobo

maunobo's solution

to Acronym in the Scala Track

Published at Sep 24 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).

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

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

import org.scalatest.{Matchers, FunSuite}

/** @version 1.6.0 */
class AcronymTest extends FunSuite with Matchers {

  test("basic") {
    Acronym.abbreviate("Portable Network Graphics") should be ("PNG")
  }

  test("lowercase words") {
    pending
    Acronym.abbreviate("Ruby on Rails") should be ("ROR")
  }

  test("punctuation") {
    pending
    Acronym.abbreviate("First In, First Out") should be ("FIFO")
  }

  test("all caps word") {
    pending
    Acronym.abbreviate("GNU Image Manipulation Program") should be ("GIMP")
  }

  test("punctuation without whitespace") {
    pending
    Acronym.abbreviate("Complementary metal-oxide semiconductor") should be ("CMOS")
  }

  test("very long abbreviation") {
    pending
    Acronym.abbreviate("Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me") should be ("ROTFLSHTMDCOALM")
  }

  test("consecutive delimiters") {
    pending
    Acronym.abbreviate("Something - I made up from thin air") should be ("SIMUFTA")
  }

  test("apostrophes") {
    pending
    Acronym.abbreviate("Halley's Comet") should be ("HC")
  }
}
object Acronym extends App {

  /*
     Should take the first letter of each word in a phrase, convert it .toUppercase
     concant and return a new string composed only by the acronym
     Should remove any commas, dots, apostrophes, dashes
   */

  def abbreviate(phrase: String): String = phrase
    // 1. split by spaces between letters, maybe find a regex for more chars than just spaces
    .split(" ")
    // 2. should make a map of every word ( _ ) => to be returned to string, take only the first letter of the word
    .map(_.charAt(0))
    // 3. only bring characters
    .filter(_.isLetter)
    // 4. from 2. mapped character made to string, required return type
    .mkString
    // 5. required to be Uppercase
    .toUpperCase
  
  println(Acronym.abbreviate("Halley's Comet")) // got a problem with this one
  println(Acronym.abbreviate("Oh 1 Hello 2 world 3")) // Should return OHW --> it does!
}

Community comments

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

maunobo's Reflection

It is a bit ugly but I have a small bug with (Halley's Comet)