Module: RubyLabs::Demos

Defined in:
lib/demos.rb

Overview

:nodoc: all

Instance Method Summary collapse

Instance Method Details

#birthday(n, m) ⇒ Object

Verification of numbers shown in the table for the birthday paradox. Call

birthday(n,m) to make a table with n rows and fill it with m random words.
Return true if any row has more than one item.


172
173
174
175
176
177
178
# File 'lib/demos.rb', line 172

def birthday(n, m)
  t = HashTable.new(n)
  TestArray.new(m, :words).each { |s| t.insert(s) }
  # puts t
  t.table.each { |row| return true if row && row.length > 1 }
  return false
end

#celsius(f) ⇒ Object

Convert the temperature f (in degrees Fahrenheit) into the equivalent

temperature in degrees Celsius.


16
17
18
# File 'lib/demos.rb', line 16

def celsius(f)
  (f - 32) * 5 / 9
end

#compound_names(a) ⇒ Object

Demonstrate modifiers and if statements



138
139
140
# File 'lib/demos.rb', line 138

def compound_names(a)
  a.each { |s| puts s if s.include?(" ") }
end

#drink_cup(n) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/demos.rb', line 150

def drink_cup(n)
  if n == 12
    return "tall"
  elsif n == 16
    return "grande"
  elsif n == 20
    return "venti"
  else
    return n.to_s + " ounce"
  end
end

#emphasize(s) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/demos.rb', line 142

def emphasize(s)
  if s == "red" || s == "green" || s == "blue"
    s.upcase!
    s += "S"
  end
  return s
end

#insert(s, t) ⇒ Object

The HashLab module has preliminary versions of insert and lookup methods

that don't use buckets; these are the final versions, with buckets.


48
49
50
51
52
53
# File 'lib/demos.rb', line 48

def insert(s, t)
	i = h(s, t.length)
  t[i] = Array.new if t[i].nil?
  t[i] << s
  return i
end

#isnest(n) ⇒ Object

code used in figure next to nested



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/demos.rb', line 122

def isnest(n)
  i = 1
  while i < n
    j = i-1
    while j >= 0 
      puts "i = #{i}, j = #{j}"
      j = j-1
    end
    i = i+1
  end
end

#lookup(s, t) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/demos.rb', line 55

def lookup(s, t)
	i = h(s, t.length)
	if t[i] && t[i].include?(s)
	  return i
 else
   return nil
  end
end

#loops(n) ⇒ Object

Compare two ways of writing a loop



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/demos.rb', line 85

def loops(n)
  i = 1
  while i <= n
    puts i ** 2
    i += 1
  end
  
  for i in 1..n
    puts i**2
  end
end

#make_order(size, kind) ⇒ Object



162
163
164
# File 'lib/demos.rb', line 162

def make_order(size, kind)
  puts "I'll have a " + drink_cup(size) + " " + kind + ", please."
end

#nested(n) ⇒ Object

:begin :nested



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/demos.rb', line 105

def nested(n)
  count = 0
  i = 0
  while i < n
    j = 0
    while j < i
      count += 1
      j += 1
    end
    i += 1
  end
  return count
end

#sequence(i, j) ⇒ Object



39
40
41
# File 'lib/demos.rb', line 39

def sequence(i,j)
  Array(i..j)
end

#sieve(n) ⇒ Object

This version of the Sieve of Eratosthenes iterates until the worksheet is

empty.  Use it as a baseline for counting the number of iterations, to
compare it to the actual version that iterates until finding the first
prime greater than sqrt(n)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/demos.rb', line 27

def sieve(n)
  worksheet = Array(2..n)
	primes = []

  while worksheet.length > 0
    primes << worksheet.first
    worksheet.delete_if { |x| x % primes.last == 0 }
  end

	return primes
end

#transform(sentence) ⇒ Object

This is the bare bones version of Eliza.transform, the method that

applies transformation rule to make a response to a sentence.


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/demos.rb', line 69

def transform(sentence)
	queue = PriorityQueue.new
	Eliza.scan(sentence, queue)
	response = nil
	while queue.length > 0
		rule = queue.shift
		response = Eliza.apply(sentence, rule)
		break if response != nil
	end
	return response
end