Determine if a word or phrase is an isogram.
An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.
Examples of isograms:
The word isograms, however, is not an isogram, because the s repeats.
For installation and learning resources, refer to the Ruby resources page.
For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:
gem install minitest
If you would like color output, you can require 'minitest/pride'
in
the test file, or note the alternative instruction, below, for running
the test file.
Run the tests from the exercise directory using the following command:
ruby isogram_test.rb
To include color from the command line:
ruby -r minitest/pride isogram_test.rb
Wikipedia https://en.wikipedia.org/wiki/Isogram
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
require 'minitest/autorun'
require_relative 'isogram'
# Common test data version: 1.7.0 74869e8
class IsogramTest < Minitest::Test
def test_empty_string
# skip
input = ""
assert Isogram.isogram?(input), "Expected true, '#{input}' is an isogram"
end
def test_isogram_with_only_lower_case_characters
skip
input = "isogram"
assert Isogram.isogram?(input), "Expected true, '#{input}' is an isogram"
end
def test_word_with_one_duplicated_character
skip
input = "eleven"
refute Isogram.isogram?(input), "Expected false, '#{input}' is not an isogram"
end
def test_word_with_one_duplicated_character_from_the_end_of_the_alphabet
skip
input = "zzyzx"
refute Isogram.isogram?(input), "Expected false, '#{input}' is not an isogram"
end
def test_longest_reported_english_isogram
skip
input = "subdermatoglyphic"
assert Isogram.isogram?(input), "Expected true, '#{input}' is an isogram"
end
def test_word_with_duplicated_character_in_mixed_case
skip
input = "Alphabet"
refute Isogram.isogram?(input), "Expected false, '#{input}' is not an isogram"
end
def test_word_with_duplicated_character_in_mixed_case_lowercase_first
skip
input = "alphAbet"
refute Isogram.isogram?(input), "Expected false, '#{input}' is not an isogram"
end
def test_hypothetical_isogrammic_word_with_hyphen
skip
input = "thumbscrew-japingly"
assert Isogram.isogram?(input), "Expected true, '#{input}' is an isogram"
end
def test_hypothetical_word_with_duplicated_character_following_hyphen
skip
input = "thumbscrew-jappingly"
refute Isogram.isogram?(input), "Expected false, '#{input}' is not an isogram"
end
def test_isogram_with_duplicated_hyphen
skip
input = "six-year-old"
assert Isogram.isogram?(input), "Expected true, '#{input}' is an isogram"
end
def test_made_up_name_that_is_an_isogram
skip
input = "Emily Jung Schwartzkopf"
assert Isogram.isogram?(input), "Expected true, '#{input}' is an isogram"
end
def test_duplicated_character_in_the_middle
skip
input = "accentor"
refute Isogram.isogram?(input), "Expected false, '#{input}' is not an isogram"
end
def test_same_first_and_last_characters
skip
input = "angola"
refute Isogram.isogram?(input), "Expected false, '#{input}' is not an isogram"
end
end
# frozen_string_literal: true
class String
ALPHABET = %w[a b c d e f g h i j k l m n o p q r s t u v w x y z].freeze
def with_repeated_letter?
characters = downcase.chars.to_a
counts = ALPHABET.map { |character| characters.count character }
counts.select { |count| count > 1 }.sum.positive?
end
end
class Isogram
def self.isogram?(string)
!string.with_repeated_letter?
end
end
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments