-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1.rb
More file actions
executable file
·320 lines (259 loc) · 7.36 KB
/
Lab1.rb
File metadata and controls
executable file
·320 lines (259 loc) · 7.36 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Assignment 1
# Part1: Hello World
class HelloWorldClass
def initialize(name)
@name = name.capitalize
end
def sayHi
puts "Hello #{@name}!"
end
end
hello = HelloWorldClass.new("jan")
#hello.sayHi
# Part2: Palindromes
def palindrome?(string)
#Convert to lowercase (downcase) and remove nonword characters (gsub)
forwardString=string.downcase.gsub(/\W/,"")
#Reverse the order of the characters
revString=forwardString.reverse
#if forwardString is the same as revString then it is a palindrome
if revString==forwardString then
return true
else
return false
end
end
#puts palindrome?("Rats live on no evil star?")
#Part3: Word Count
def count_words(string)
#Split on word boundaries and delete elements that are nonword characters
words=string.split(/\b/).delete_if{|word| word=~/\W/ }
#Need to add link to stack overflow site
count=Hash.new(0)
words.each{|word| count[word]+=1}
return count
end
#puts count_words("doo! bee-- doo, bee doo")
#Part4: Rock Paper Scissors
#4a
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_game_winner(game)
#Check to ensure only 2 players per game
raise WrongNumberOfPlayersError unless game.length == 2
#Assign first and second player/move to player1 and player2 respectively
player1=game[0]
player2=game[1]
#If both players play the same move then the first player is the winner
if player1[1].downcase==player2[1].downcase then
return player1
#Player1 plays "Rock"
elsif player1[1].downcase== "r" then
if player2[1].downcase=="s" then
return player1
elsif player2[1].downcase=="p" then
return player2
end
#Player1 plays "Scissors"
elsif player1[1].downcase=="s" then
if player2[1].downcase=="p" then
return player1
elsif player2[1].downcase=="r" then
return player2
end
#Player1 plays "Paper"
elsif player1[1].downcase=="p" then
if player2[1].downcase=="r" then
return player1
elsif player2[1].downcase=="s" then
return player2
end
end
#If the program reaches this point without returning a winner, then
#raise NoSuchStrategyError because one of the strategies is invalid
raise NoSuchStrategyError
end
#print rps_game_winner([["Armando","p"],["Dave","R"]]) , "\n"
#4b
def rps_tournament_winner(tournament)
#Determines the number of players in the tournament
numPlayers=(tournament.flatten.length)/2
#Uses the number of players to determine the number of rounds
#needed to find a single winner
rounds=Math.log2(numPlayers).to_i
#Flattens tournament array into list of player/strategy lists of
#length 2 by using a recursion level that is one less than the
#number of rounds
currentRound=tournament.flatten(rounds-1)
#Each iteration corresponds to a round of the tournament where
#the players that lose are eliminated and the winners move on
#to the next round. It loops until there is a single winner
until currentRound.length<2 do
#nextRound is empty before any games are played in current round
nextRound=[]
numGames=currentRound.length/2
#Determines the winner of each game for a given round. The
#winner's player/strategy is added to nextRound
for i in 0..numGames-1
currentGame=currentRound.pop(2)
winner=rps_game_winner(currentGame)
nextRound+=[winner]
end
#Upon completion of a round, currentRound is set equal to
#nextRound and the next round begins
currentRound=nextRound
end
#Retruns the player/strategy of the winner as a 1-dimensional array
#of length2
return currentRound.flatten
end
tournament=[
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ],
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
]
]
print rps_tournament_winner(tournament), "\n"
#5 Anagrams
def combine_anagrams(words)
anagrams=words.group_by{|word| word.downcase.chars.sort}.values
end
print combine_anagrams(['Cars', 'for', 'potatoes', 'racs', 'four', 'scar', 'creams', 'scream'])
# 6 Glob Match
def glob_match(filenames, pattern)
# http://ruby-doc.org/core-2.1.0/File.html#M000001
return filenames.delete_if{|filename| !File.fnmatch(pattern,filename,File::FNM_DOTMATCH)}
end
print glob_match(['part1.rb', 'part2.rb', 'part2.rb~', '.part3.rb.un~'], '*part*rb?*'), "\n"
#7a Dessert Class
#https://gist.github.com/feiskyer/1964748
class Dessert
attr_accessor :name
attr_accessor :calories
def initialize(name, calories)
@name=name
@calories=calories
end
def healthy?
@calories<200
end
def delicious?
true
end
end
#7b JellyBean
class JellyBean < Dessert
attr_accessor :flavor
def initialize(name, calories, flavor)
@name=name
@calories=calories
@flavor=flavor
end
def delicious?
if @flavor== "black licorice"
false
else
true
end
end
end
a=JellyBean.new("a",232,"black licorice")
p a.delicious?
p a.healthy?
b=Dessert.new("black licorice",190)
p b.delicious?
p b.healthy?
#8 Foo Class
#https://gist.github.com/feiskyer/1964749
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
# getter
attr_reader attr_name
attr_reader attr_name+"_history"
class_eval %Q{
def #{attr_name}=(val)
@#{attr_name} = val
@#{attr_name}_history = [nil] if @#{attr_name}_history.nil?
@#{attr_name}_history.push(val)
end
}
end
end
class Foo
attr_accessor_with_history :bar
end
#f = Foo.new
#f.bar = 1
#f.bar = 2
#p f.bar
#p f.bar_history # => if your code works, should be [nil,1,2]
#9 Currency Conversion
#https://gist.github.com/feiskyer/1964749
class Numeric
@@currencies = {'dollar' => 1.0, 'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019}
def method_missing(method_id)
singular_currency = to_singular(method_id)
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency] #converts to dollars
else
super
end
end
def in(currency)
singular_currency = to_singular(currency)
if @@currencies.has_key?(singular_currency)
self / @@currencies[singular_currency] #converts from dollars to argument passed to in()
else
super
end
end
private
def to_singular(word)
word.to_s.gsub( /s$/, '')
end
end
# puts 5.dollars.in(:euros) , 10.euros.in(:rupees)
# 10a
#https://gist.github.com/feiskyer/1964749
class String
def palindrome?
# Calls palindrome? defined in question 2
Object.send(:palindrome?, self)
end
end
puts "***** 10a *****", "foo".palindrome?, "foof".palindrome?
#10b
#https://gist.github.com/feiskyer/1964749
module Enumerable
def palindrome?
self.collect{|x| x} == self.collect{|x| x}.reverse
end
end
puts "***** 10b *****", [1,2,3,2,1].palindrome?, ["this", "does", "work", "does", "this"].palindrome?
#11
#https://gist.github.com/feiskyer/1964749
class CartesianProduct
include Enumerable
attr_reader :l_enum, :r_enum
def initialize(l_enum, r_enum)
@l_enum = l_enum
@r_enum = r_enum
end
def each
self.l_enum.each {
|l| self.r_enum.each {
|r| yield [l, r]
}
}
end
end
puts "***** 11 *****"
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }