-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab2.rb
76 lines (60 loc) · 1.69 KB
/
Lab2.rb
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
# Part1: Hello World
class HelloWorldClass
def initialize(name)
@name = name.capitalize
end
def sayHi
puts "Hello #{@name}!"
end
end
#hello = HelloWorldClass.new("James Ontiveros")
#hello.sayHi
#Part2
def isPalindrome(string)
string = string.downcase
string = string.gsub(/[^a-z]/, '')
if string == string.reverse
return "This is a Palindrome"
else "This is not a Palindrome"
end
end
def count_words(string)
string.downcase!
astring = string.split
counthash = Hash.new(0)
astring.each { |word| counthash[word] += 1 }
return counthash
end
#part3
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
raise NoSuchStrategyError unless game[0][1].match(/[RPS]/) && game[1][1].match(/[RPS]/)
wc = ["RS","SP","PR","RR","PP","SS"]
return wc.index(game[0][1] + game[1][1]) ? game[0] : game[1]
end
def rps_tournament_winner(game)
if !game[0][0].is_a?(String)
return rps_tournament_winner([rps_tournament_winner(game[0]), rps_tournament_winner(game[1])])
else
return rps_game_winner(game)
end
end
#Part4
def combine_anagrams(words)
words.group_by{ |word| word.chars.sort }.values
end
#p isPalindrome("A man, a plan, a canal -- Panama")
#p count_words("A man, a plan, a canal -- Panama")
p rps_tournament_winner([
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ],
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
]
])
p combine_anagrams(['cars', 'for', 'potatoes', 'racs', 'four', 'scar', 'creams', 'scream'])