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/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 methods implemented in this module are
7
- +contains?+ (linear search returning true or false),
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
- =end
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
- module RubyLabs
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
- def contains?(a, k)
25
- a.each { |x| return true if x == k}
26
- return false
27
- end
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
- =begin rdoc
31
- Do a linear search of array +a+ to find item +k+, returning
32
- the index of the first occurrence of +k+ or +nil+ if +k+ is
33
- not found.
34
- =end
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
- def search(a, k)
38
- i = 0
39
- while i < a.length
40
- return i if a[i] == k
41
- i += 1
42
- end
43
- return nil
44
- end
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
- =begin rdoc
48
- Return a copy of +array+, sorted with the insertion sort algorithm.
49
- On each iteration remove an item from the
50
- array, find a location for it to the left of its original position,
51
- and insert it back into the array at the new location.
52
- =end
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
- def isort(array)
56
- a = array.clone # don't modify the input array....
57
- i = 1
58
- while i < a.length
59
- move_left(a, i) # find a place for a[i] somewhere to the left
60
- i += 1
61
- end
62
- return a
63
- end
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) # remove the item at location i
69
- j = i-1 # start scanning from the left of i
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 # move left
109
+ j = j-1 # move left
72
110
  end
73
- a.insert(j+1, x) # insert x back into a at location j
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
- =begin rdoc
84
- A helper method that can be called from a probe to display the contents
85
- of an array during a search or sort.
86
- =end
87
-
88
- def brackets(a, i)
89
- if i <= 0
90
- return ("[" + a.join(" ") + "]")
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
- end
98
- end
99
-
153
+ end
154
+ end
155
+
100
156
  end # IterationLab
101
157
 
102
158
  end # RubyLabs