-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunique_test.rb
More file actions
49 lines (38 loc) · 1.16 KB
/
unique_test.rb
File metadata and controls
49 lines (38 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
else
unique << s unless repeat.include?(s)
end
end
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
it "should really not put items back into unique" do
result = UniqueCharacter.first_unique("aa")
result.must_equal(nil)
end
end