Last active
December 21, 2015 13:29
-
-
Save irissilvermoon/6312417 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module UniqueCharacter | |
#put all the characters in the string into two sets | |
#first set is Unique Characters, second set is #Non-Unique characters | |
#check to see if the character is in the unique characters set | |
#if not, place it there, if it is, put it in non_unique characters | |
#return the first character in unique characters set. | |
require 'set' | |
def self.first_unique(string) | |
unique = Set.new() | |
repeat = Set.new() | |
string.each_char do |s| | |
if unique.include?(s) | |
unique.delete(s) | |
#if true, remove from unique and put into repeat | |
repeat << s | |
end | |
unique << s | |
end | |
return unique.first | |
end | |
end | |
require 'minitest/spec' | |
require 'minitest/autorun' | |
describe UniqueCharacter do | |
it "finds the first non-repeating character" do | |
result = UniqueCharacter.first_unique("aabafzabba") | |
result.must_equal('f') | |
end | |
it "should not put items back into unique" do | |
result = UniqueCharacter.first_unique("aabafzbb") | |
result.must_equal('f') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment