-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_path_with_recursion.rb
119 lines (97 loc) · 2.48 KB
/
find_path_with_recursion.rb
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env ruby
require 'optparse'
require 'set'
require 'pp'
#response = Load_words.get('http://www.puzzlers.org/pub/wordlists/ospd.txt')
# Put data into an array
#a_body = response.body.split("\n").to_set
#d_body = Hash[a_body.map {|x| [x, x]}]
all_words = []
File.open('ospd.txt').each do | line |
all_words.push(line)
end
test_words = [ 'apple', 'zebra', 'eggs' ]
def make_prefix_hash( words )
result = {}
words.each do | word |
prefix = word
while prefix.length > 0 do
result[prefix] = word
prefix = prefix.chop
end
end
return result
end
#puts make_prefix_hash(test_words)
@megahash = make_prefix_hash(all_words)
def OLDsearch4awesome( words, prefix )
words.each { |word|
if /^#{prefix}/ =~ word
return true
end
}
return false
end
def search4awesome( words, prefix )
words.each { |word|
if /^#{prefix}/ =~ word
return true
end
}
return false
end
def NEWsearch4awesome( prefix )
return @megahash[prefix]
end
class LessAwesomeSearch
def initialize(word_list)
@my_list = word_list
end
def search(prefix)
@my_list.each { |word|
if /^#{prefix}/ =~ word
return true
end
}
return false
end
end
class AwesomeSearch
def initialize(word_list)
@my_hash = make_prefix_hash(word_list)
end
def search(prefix)
return @my_hash[prefix]
end
end
test_words_lookup = AwesomeSearch.new(test_words)
#other_test_words_lookup = AwesomeSearch.new({"mark" => "mark", "alaric" => "alaric"})
#test_words_lookup = LessAwesomeSearch.new(test_words)
if test_words_lookup.search( 'soup' )
puts 'test failed soup should not have been found'
end
if not test_words_lookup.search( 'zebra' )
puts 'test failed zebra should have been found'
end
if not test_words_lookup.search( 'zeb' )
puts 'test failed zeb should have been found in zebra'
end
if test_words_lookup.search( 'ebra' )
puts 'test failed ebra should not have been found'
end
#
#for i in 0...10 do
# rand_word = (0...8).map { ('a'.ord + rand(26)).chr }.join
# puts rand_word
# all_words_lookup.search(rand_word)
# test_words_lookup.search(rand_word)
#end
all_words_lookup = AwesomeSearch.new(all_words)
also_all_words_lookup = LessAwesomeSearch.new(all_words)
#for i in 0...10 do
# rand_word = (0...2).map { ('a'.ord + rand(26)).chr }.join
# puts rand_word
# puts test_words_lookup.search(rand_word)
# puts all_words_lookup.search(rand_word)
# puts also_all_words_lookup.search(rand_word)
#end