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

evangelinux's solution

to Anagram in the Crystal 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".

Setup

Follow the setup instructions for Crystal here:

http://exercism.io/languages/crystal

More help installing can be found here:

http://crystal-lang.org/docs/installation/index.html

Making the Test Suit Pass

Execute the tests with:

$ crystal spec

In each test suite all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by changing pending to it.

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_spec.cr

require "spec"
require "../src/*"

describe "Anagram" do
  describe "#find" do
    it "detects no anagrams" do
      Anagram.find("diaper", ["hello", "world", "zombies", "pants"]).should eq [] of String
    end

    pending "detects one anagram" do
      Anagram.find("ant", ["tan", "stand", "at"]).should eq ["tan"]
    end

    pending "detects multiple anagrams" do
      Anagram.find("master", ["stream", "pigeon", "maters"]).should eq ["stream", "maters"]
    end

    pending "detects multiple anagrams 2" do
      Anagram.find("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]).should eq ["gallery", "regally", "largely"]
    end

    pending "does not detect false positives" do
      Anagram.find("galea", ["eagle"]).should eq [] of String
    end

    pending "does not detect anagram subsets" do
      Anagram.find("good", ["dog", "goody"]).should eq [] of String
    end

    pending "does not detect identical words" do
      Anagram.find("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"]).should eq ["cron"]
    end

    pending "detects anagrams case-insensitively" do
      Anagram.find("Orchestra", ["cashregister", "carthorse", "radishes"]).should eq ["carthorse"]
    end

    pending "does not detect a word as its own anagram" do
      Anagram.find("banana", ["Banana"]).should eq [] of String
    end

    pending "anagrams must use all letters exactly once" do
      Anagram.find("tapper", ["patter"]).should eq [] of String
    end
  end
end
# Please implement your solution to anagram in this file
module Anagram
  def self.find(str : String, word : Array(String)) : Array(String)
    word.select { |w| anagram(str, w) }
  end

  def self.anagram(str_a : String, str_b : String) : Bool
    a = str_a.downcase
    b = str_b.downcase

    return false if a.size != b.size
    return false if a == b
    chars_in_a = a.chars
    b.downcase.each_char do |cb|
      if i = chars_in_a.index { |ca| ca == cb }
        chars_in_a.delete_at(i)
      else
        return false
      end
    end
    chars_in_a.empty?
  end
end

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?