-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter10.rb
More file actions
206 lines (176 loc) · 5 KB
/
chapter10.rb
File metadata and controls
206 lines (176 loc) · 5 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# def ask_recusively question
# puts question
# answer = gets.chomp.downcase
# if answer == 'yes'
# true
# elsif answer == 'no'
# false
# else
# puts 'Please answer yes or no'
# ask_recusively question
# end
# end
# ask_recusively 'Do you wet the bed?'
# Enter a number between 1 and 10
# def number_check question
# puts question
# answer = gets.chomp.to_i
# if answer > 0 and answer < 11
# answer
# else
# puts question
# number_check question
# end
# end
# save = number_check 'Enter a number between 1 and 10 to add to 9'
# puts '9 plus ' + save.to_s + ' equals ' + (save + 9).to_s
# 99 bottles of beer with recursion
# def bottles_of_beer number
# if number > 0
# puts number.to_s + ' bottles of beer on the wall, ' + number.to_s + ' bottles of beer.'
# puts 'Take one down, pass it around, ' + (number - 1).to_s + ' bottles of beer on the wall.'
# bottles_of_beer (number - 1)
# end
# end
# puts 'How many bottles of beer are on the wall?'
# bottles = gets.chomp.to_i
# bottles_of_beer bottles
# puts 'Civilization Map Program v1.0'
# M = 'land'
# o = 'water'
# world = [
# [M,M,o,o,o,o,o,o,o,o,o],
# [M,M,M,o,o,o,o,o,o,o,o],
# [o,o,M,o,o,o,o,o,o,o,o],
# [o,o,o,M,o,o,o,o,o,o,o],
# [o,o,o,M,M,M,o,o,o,o,o],
# [o,o,o,M,M,M,M,M,M,M,M],
# [o,o,o,o,M,o,o,o,o,o,o],
# [o,o,o,o,o,o,o,o,o,o,o],
# [o,o,o,o,o,o,o,o,o,o,o],
# [o,o,o,o,o,o,o,o,o,o,o],
# [o,o,o,o,o,o,o,o,o,o,o]
# ]
# def continent_size world, x, y
# if (x < 0) or (x > 11)
# return 0
# elsif (y < 0) or (y > 11)
# return 0
# end
# if world[y][x] != 'land'
# # Either it's water or we already counted it
# # but either way, we don't want to count it now.
# return 0
# end
# # So first we count this tile...
# size = 1
# world[y][x] = 'counted land'
# # ... then we count all of the neighboring eight tiles
# # (and, of course, their neighbors by way of the recursion).
# size = size + continent_size(world, x-1, y+1)
# size = size + continent_size(world, x , y+1)
# size = size + continent_size(world, x+1, y+1)
# size = size + continent_size(world, x+1, y )
# size = size + continent_size(world, x+1, y-1)
# size = size + continent_size(world, x , y-1)
# size = size + continent_size(world, x-1, y-1)
# size = size + continent_size(world, x-1, y )
# size
# end
# puts continent_size(world, 1, 1)
# puts 'Program to check if a person is an employee v 1.0'
# employees = [
# ['Rick','Peyton'],
# ['Pam','Brunson'],
# ['Vanessa','Kern'],
# ['Susan','Russell']
# ['Tracy','Garner']
# ]
# def employee_check employees, first, last
# end
# puts 'What is the first name of the person you are looking for?'
# fname = gets.chomp
# puts 'We have a ' + employee_check(fname,'') + ' here. Is that who you are looking for?'
# puts 'Sorting Program with recursion v1.0'
# # Keep two more lists around
# # One for already-sorted words
# # One for still - unsorted words
# # Find the smallest word in the unsorted list
# # push it into the end of the sorted_array
# def sort some_array
# recursive_sort some_array, []
# end
# def recursive_sort unsorted_array, sorted_array
# smallest = unsorted_array[0]
# array_length = unsorted_array.length
# position = 0
# x = 0
# if array_length > 1
# unsorted_array.each do |uns|
# if uns.downcase < smallest.downcase
# smallest = uns
# position = x
# end
# x = x + 1
# end
# sorted_array.push(smallest)
# unsorted_array.delete_at(position)
# recursive_sort unsorted_array, sorted_array
# else
# sorted_array.push(unsorted_array.pop)
# sorted_array.each do |sor|
# puts sor
# end
# end
# end
# # Get a list of unsorted words into an array
# orig_array = []
# word = 'placeholder'
# puts 'Enter a list of words to be sorted. Press enter when done.'
# while word != ''
# word = gets.chomp
# orig_array.push word
# end
# orig_array.pop
# puts 'This is the output of the built in sort method.'
# orig_array.sort.each do |un|
# puts un
# end
# puts 'This is the output of Rick\'s sort method.'
# sort orig_array
puts 'Shuffling Program with recursion v1.0'
# Keep two more lists around
# One for already-sorted words
# One for still - unsorted words
# Find the smallest word in the unsorted list
# push it into the end of the sorted_array
def shuffle some_array
recursive_shuffle some_array, []
end
def recursive_shuffle unshuffled_array, shuffled_array
array_length = unshuffled_array.length
if array_length > 0
pick_one = rand(array_length)
shuffled_array.push(unshuffled_array[pick_one])
unshuffled_array.delete_at(pick_one)
recursive_shuffle unshuffled_array, shuffled_array
else
shuffled_array.each do |sh|
puts sh
end
end
end
# Get a list of unsorted words into an array
orig_array = []
word = 'placeholder'
puts 'Enter a list of words to be shuffled. Press enter when done.'
while word != ''
word = gets.chomp
orig_array.push word
end
orig_array.pop
puts 'This is the output of the built in shuffle method.'
orig_array_shuffled = orig_array
puts orig_array_shuffled.shuffle
puts 'This is the output of Rick\'s sort method.'
shuffle orig_array