Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Anagram in the CoffeeScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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

Refer to the Exercism CoffeScript page for getting started with CoffeeScript.

In order to run the test, you can run the test file from the exercise directory:

jasmine-node --coffee .

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.

anagram_test.spec.coffee

Anagram = require "./anagram"
describe "Anagram", ->
  it "no matches", ->
    detector = new Anagram "diaper"
    matches = detector.match ["hello", "world", "zombies", "pants"]
    expect(matches).toEqual []

  xit "detects simple anagram", ->
    detector = new Anagram "ant"
    matches = detector.match ["tan", "stand", "at"]
    expect(matches).toEqual ["tan"]

  xit "does not detect false positives", ->
    detector = new Anagram "galea"
    matches = detector.match ["eagle"]
    expect(matches).toEqual []

  xit "detects multiple anagrams", ->
    detector = new Anagram "master"
    matches = detector.match ["stream", "pigeon", "maters"]
    expect(matches).toEqual ["stream", "maters"]

  xit "does not detect anagram subsets", ->
    detector = new Anagram "good"
    matches = detector.match ["dog", "goody"]
    expect(matches).toEqual []

  xit "detects anagram", ->
    detector = new Anagram "listen"
    matches = detector.match ["enlists", "google", "inlets", "banana"]
    expect(matches).toEqual ["inlets"]

  xit "detects multiple anagrams", ->
    detector = new Anagram "allergy"
    matches = detector.match ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]
    expect(matches).toEqual ["gallery", "regally", "largely"]

  xit "does not detect identical words", ->
    detector = new Anagram "corn"
    matches = detector.match ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"]
    expect(matches).toEqual ["cron"]

  xit "does not detect non-anagrams with identical checksum", ->
    detector = new Anagram "mass"
    matches = detector.match ["last"]
    expect(matches).toEqual []

  xit "detects anagrams using case-insensitive subject", ->
    detector = new Anagram "Orchestra"
    matches = detector.match ["cashregister", "carthorse", "radishes"]
    expect(matches).toEqual ["carthorse"]

  xit "detects anagrams using case-insensitive candidates", ->
    detector = new Anagram "orchestra"
    matches = detector.match ["cashregister", "Carthorse", "radishes"]
    expect(matches).toEqual ["carthorse"]
module.exports = class Anagram
  constructor: (w) ->
    @detector_word = w.toLowerCase()
    @detector_sorted = @detector_word.split('').sort().join()

  match: (words) ->
    words = words.map (word) -> word.toLowerCase()
    words.filter (word) =>
      return false if word is @detector_word
      word.split('').sort().join() is @detector_sorted

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?