rubylabs 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -6
- data/Rakefile +3 -0
- data/VERSION +1 -1
- data/lib/bitlab.rb +593 -328
- data/lib/demos.rb +20 -9
- data/lib/elizalab.rb +660 -507
- data/lib/hashlab.rb +289 -192
- data/lib/introlab.rb +33 -38
- data/lib/iterationlab.rb +117 -61
- data/lib/marslab.rb +608 -475
- data/lib/randomlab.rb +227 -121
- data/lib/recursionlab.rb +197 -140
- data/lib/rubylabs.rb +936 -390
- data/lib/sievelab.rb +32 -24
- data/lib/spherelab.rb +308 -220
- data/lib/tsplab.rb +634 -312
- data/test/bit_test.rb +4 -4
- data/test/tsp_test.rb +18 -0
- metadata +2 -2
data/lib/iterationlab.rb
CHANGED
@@ -1,102 +1,158 @@
|
|
1
|
+
module RubyLabs
|
1
2
|
|
2
3
|
=begin rdoc
|
3
4
|
|
4
5
|
== IterationLab
|
5
6
|
|
6
|
-
The
|
7
|
-
|
8
|
-
+location+ (linear search returning the index of specified item), and
|
9
|
-
+isort+ (insertion sort). See the RecursionLab module for more
|
10
|
-
sophisticated "divide and conquer" searching and sorting algorithms.
|
7
|
+
The IterationLab module has definitions of methods from Chapter 4
|
8
|
+
of <em>Explorations in Computing</em>.
|
11
9
|
|
12
|
-
|
10
|
+
The methods demonstrate how a simple strategy of repeatedly comparing
|
11
|
+
items in an array can be used to search the array and to sort it.
|
12
|
+
The module has two implementations of linear search (<tt>contains?</tt>
|
13
|
+
and +search+) and an implementation of insertion sort (+isort+).
|
13
14
|
|
14
|
-
|
15
|
+
Helper methods called by the searching and sorting methods are also
|
16
|
+
documented here.
|
15
17
|
|
16
|
-
module IterationLab
|
17
|
-
|
18
|
-
=begin rdoc
|
19
|
-
Do a linear search of array +a+ to find item +key+, returning
|
20
|
-
+true+ or +false+ depending on whether the item was found.
|
21
18
|
=end
|
22
19
|
|
20
|
+
module IterationLab
|
21
|
+
|
22
|
+
# The RubyLabs implementation of Ruby's <tt>include?</tt> method.
|
23
|
+
# Does a linear search of array +a+ to find item +k+, returning
|
24
|
+
# +true+ or +false+ depending on whether the item was found.
|
25
|
+
#
|
26
|
+
# Example:
|
27
|
+
# >> a = TestArray.new(10)
|
28
|
+
# => [89, 41, 69, 14, 4, 7, 8, 26, 81, 12]
|
29
|
+
# >> contains?(a, 7)
|
30
|
+
# => true
|
31
|
+
# >> contains?(a, 42)
|
32
|
+
# => false
|
33
|
+
#
|
34
|
+
# :call-seq:
|
35
|
+
# contains?(a,k) => Boolean
|
36
|
+
#
|
37
|
+
#--
|
23
38
|
# :begin :contains?
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
39
|
+
def contains?(a, k)
|
40
|
+
a.each { |x| return true if x == k}
|
41
|
+
return false
|
42
|
+
end
|
28
43
|
# :end :contains?
|
29
44
|
|
30
|
-
|
31
|
-
|
32
|
-
the index of the first occurrence of +k+ or +nil+ if +k+ is
|
33
|
-
not found.
|
34
|
-
|
35
|
-
|
45
|
+
# The RubyLabs implementation of Ruby's <tt>index</tt> method.
|
46
|
+
# Does a linear search of array +a+ to find item +k+, returning
|
47
|
+
# the index of the first occurrence of +k+ or +nil+ if +k+ is
|
48
|
+
# not found.
|
49
|
+
#
|
50
|
+
# Example:
|
51
|
+
# >> a = TestArray.new(10)
|
52
|
+
# => [89, 41, 69, 14, 4, 7, 8, 26, 81, 12]
|
53
|
+
# >> search(a, 42)
|
54
|
+
# => nil
|
55
|
+
# >> search(a, 7)
|
56
|
+
# => 5
|
57
|
+
#
|
58
|
+
# :call-seq:
|
59
|
+
# search(a,k) => Fixnum
|
60
|
+
#
|
61
|
+
#--
|
36
62
|
# :begin :search
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
63
|
+
def search(a, k)
|
64
|
+
i = 0
|
65
|
+
while i < a.length
|
66
|
+
return i if a[i] == k
|
67
|
+
i += 1
|
68
|
+
end
|
69
|
+
return nil
|
70
|
+
end
|
45
71
|
# :end :search
|
46
72
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
73
|
+
# Return a copy of +a+, sorted using the insertion sort algorithm.
|
74
|
+
# On each iteration remove an item from the
|
75
|
+
# array, find a location for it to the left of its original position,
|
76
|
+
# and insert it back into the array at the new location.
|
77
|
+
#
|
78
|
+
# Example:
|
79
|
+
# >> a = TestArray.new(10)
|
80
|
+
# => [97, 87, 16, 2, 81, 80, 7, 64, 5, 71]
|
81
|
+
# >> isort(a)
|
82
|
+
# => [2, 5, 7, 16, 64, 71, 80, 81, 87, 97]
|
83
|
+
#
|
84
|
+
# :call-seq:
|
85
|
+
# isort(a) => Array
|
86
|
+
#
|
87
|
+
#--
|
54
88
|
# :begin :isort :move_left :less
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
89
|
+
def isort(array)
|
90
|
+
a = array.clone # don't modify the input array....
|
91
|
+
i = 1
|
92
|
+
while i < a.length
|
93
|
+
move_left(a, i) # find a place for a[i] somewhere to the left
|
94
|
+
i += 1
|
95
|
+
end
|
96
|
+
return a
|
97
|
+
end
|
64
98
|
# :end :isort
|
65
99
|
|
100
|
+
# Helper method called by +isort+ to remove the item at locaton +i+
|
101
|
+
# and reinsert it at a location between 0 and +i+ (i.e. move the item
|
102
|
+
# to the left in the array).
|
103
|
+
#--
|
66
104
|
# :begin :move_left
|
67
105
|
def move_left(a, i)
|
68
|
-
x = a.slice!(i)
|
69
|
-
j = i-1
|
106
|
+
x = a.slice!(i) # remove the item at location i
|
107
|
+
j = i-1 # start scanning from the left of i
|
70
108
|
while j >= 0 && less(x, a[j])
|
71
|
-
j = j-1
|
109
|
+
j = j-1 # move left
|
72
110
|
end
|
73
|
-
a.insert(j+1, x)
|
111
|
+
a.insert(j+1, x) # insert x back into a at location j
|
74
112
|
end
|
75
113
|
# :end :move_left
|
76
114
|
|
115
|
+
# +less+ is called by +isort+ to compare two items.
|
116
|
+
# It is implemented as a helper method in order to allow users to
|
117
|
+
# attach a probe to count the number of comparisons.
|
118
|
+
#--
|
77
119
|
# :begin :less
|
78
120
|
def less(x, y)
|
79
121
|
return x < y
|
80
122
|
end
|
81
123
|
# :end :less
|
82
124
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
125
|
+
# A helper method that can be called from a probe to display the contents
|
126
|
+
# of an array during a search or sort.
|
127
|
+
#
|
128
|
+
# A call to <tt>brackets(a,i)</tt> will return a string that includes
|
129
|
+
# all the items in +a+, with a left bracket before <tt>a[i]</tt>
|
130
|
+
# and a right bracket after the last item.
|
131
|
+
#
|
132
|
+
# See also RecursionLab#brackets.
|
133
|
+
#
|
134
|
+
# Example:
|
135
|
+
# >> a = TestArray.new(10)
|
136
|
+
# => [55, 29, 72, 33, 14, 57, 85, 42, 26, 97]
|
137
|
+
# >> puts brackets(a, 3)
|
138
|
+
# 29 55 72 [33 14 57 85 42 26 97]
|
139
|
+
# => nil
|
140
|
+
#
|
141
|
+
# :call-seq:
|
142
|
+
# brackets(a,i) => String
|
143
|
+
#
|
144
|
+
def brackets(a, i)
|
145
|
+
if i <= 0
|
146
|
+
return ("[" + a.join(" ") + "]")
|
91
147
|
elsif i >= a.length
|
92
148
|
return " " + a.join(" ") + " [ ]"
|
93
149
|
else
|
94
150
|
pre = a.slice(0..(i-1))
|
95
151
|
post = a.slice(i..-1)
|
96
152
|
return " " + pre.join(" ") + " [" + post.join(" ") + "]"
|
97
|
-
|
98
|
-
|
99
|
-
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
100
156
|
end # IterationLab
|
101
157
|
|
102
158
|
end # RubyLabs
|