首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scala中的Anagram

Scala中的Anagram
EN

Code Review用户
提问于 2016-06-20 18:23:15
回答 1查看 961关注 0票数 2

代码语言:javascript
复制
class Anagram(anagram: String) {
  def matches(candidates: Seq[String]) = {
    candidates.filter(isPermutation)
  }

  def isPermutation(candidate: String) = {
    (candidate.toLowerCase != anagram.toLowerCase) &&
        (candidate.toLowerCase.sortWith(_>_) ==
            anagram.toLowerCase.sortWith(_>_))
  }
}

测试套房

代码语言:javascript
复制
import org.scalatest._

class AnagramSpecs extends FlatSpec with Matchers {
  it should "detect no matches" in {
    val detector = new Anagram("diaper")
    val anagrams = detector.matches(Seq("hello", "world", "zombies", "pants"))
    anagrams should equal (Seq())
  }

  it should "detect simple anagrams" in {
    val detector = new Anagram("ant")
    val anagrams = detector.matches(Seq("tan", "stand", "at"))
    anagrams should equal (Seq("tan"))
  }

  it should "detect multiple anagrams" in {
    val detector = new Anagram("master")
    val anagrams = detector.matches(Seq("stream", "pigeon", "maters"))
    anagrams.sorted should equal (Seq("maters", "stream"))
  }

  it should "not confuse different duplicates" in {
    val detector = new Anagram("galea")
    val anagrams = detector.matches(Seq("eagle"))
    anagrams should equal (Seq())
  }

  it should "not include identical words" in {
    val detector = new Anagram("corn")
    val anagrams = detector.matches(Seq(
      "corn", "dark", "Corn", "rank", "CORN", "cron", "park"
    ))
    anagrams should equal (Seq("cron"))
  }

  it should "elimitate anagrams with same checksum" in {
    val detector = new Anagram("mass")
    detector.matches(Seq("last")) should equal (Seq())
  }

  it should "eliminate anagrams subsets" in {
    val detector = new Anagram("good")
    detector.matches(Seq("dog", "goody")) should equal (Seq())
  }

  it should "detect anagrams" in {
    val detector = new Anagram("listen")
    val anagrams = detector.matches(Seq("enlists", "google", "inlets", "banana"))
    anagrams should equal (Seq("inlets"))
  }

  it should "detect more anagrams" in {
    val detector = new Anagram("allergy")
    val anagrams = detector.matches(Seq(
      "gallery", "ballerina", "regally", "clergy", "largely", "leading"
    ))
    anagrams.sorted should equal (Seq("gallery", "largely", "regally"))
  }

  it should "treat anagrams as case insensitive" in {
    val detector = new Anagram("Orchestra")
    val anagrams = detector.matches(Seq(
      "cashregister", "Carthorse", "radishes"
    ))
    anagrams should equal(Seq("Carthorse"))
  }
}

虽然代码通过了所有的测试,但我仍然觉得还有改进的余地。目前,我对Scala和都是新手。

来源

EN

回答 1

Code Review用户

回答已采纳

发布于 2016-06-20 21:20:34

总体上,短和甜,与良好的详细测试。

性能

anagram字段( subject)被降低并多次排序。您可以在构建时执行这些操作一次。

当您只需执行一次时,candidate参数将被降低两次。

命名

Anagram类相当是一个AnagramMatcher

anagram字段实际上是一个subject

就主题而言,isPermutation相当isAnagram

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/132552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档