🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Anagram in the Scala Track

Published at Aug 19 2019 · 0 comments
Instructions
Test suite
Solution

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

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

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

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

AnagramTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.4.0 */
class AnagramTest extends FunSuite with Matchers {

  test("no matches") {
    Anagram.findAnagrams("diaper", List("hello", "world", "zombies", "pants")) should be(
      List())
  }

  test("detects two anagrams") {
    pending
    Anagram.findAnagrams("master", List("stream", "pigeon", "maters")) should be(
      List("stream", "maters"))
  }

  test("does not detect anagram subsets") {
    pending
    Anagram.findAnagrams("good", List("dog", "goody")) should be(List())
  }

  test("detects anagram") {
    pending
    Anagram.findAnagrams(
      "listen",
      List("enlists", "google", "inlets", "banana")) should be(List("inlets"))
  }

  test("detects three anagrams") {
    pending
    Anagram.findAnagrams("allergy",
                         List("gallery",
                              "ballerina",
                              "regally",
                              "clergy",
                              "largely",
                              "leading")) should be(
      List("gallery", "regally", "largely"))
  }

  test("does not detect non-anagrams with identical checksum") {
    pending
    Anagram.findAnagrams("mass", List("last")) should be(List())
  }

  test("detects anagrams case-insensitively") {
    pending
    Anagram.findAnagrams(
      "Orchestra",
      List("cashregister", "Carthorse", "radishes")) should be(
      List("Carthorse"))
  }

  test("detects anagrams using case-insensitive subject") {
    pending
    Anagram.findAnagrams(
      "Orchestra",
      List("cashregister", "carthorse", "radishes")) should be(
      List("carthorse"))
  }

  test("detects anagrams using case-insensitive possible matches") {
    pending
    Anagram.findAnagrams(
      "orchestra",
      List("cashregister", "Carthorse", "radishes")) should be(
      List("Carthorse"))
  }

  test("does not detect a anagram if the original word is repeated") {
    pending
    Anagram.findAnagrams("go", List("go Go GO")) should be(List())
  }

  test("anagrams must use all letters exactly once") {
    pending
    Anagram.findAnagrams("tapper", List("patter")) should be(List())
  }

  test("words are not anagrams of themselves (case-insensitive)") {
    pending
    Anagram.findAnagrams("BANANA", List("BANANA", "Banana", "banana")) should be(
      List())
  }
}
object Anagram {
  def findAnagrams(input: String, candidates: List[String]): List[String] = {
    val word: String = input.sortBy(_.toLower)
    candidates.filterNot(_ equalsIgnoreCase input)
      .filter(_.sortBy(_.toLower) equalsIgnoreCase word)
  }

}

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?