forked from SF-WDI-LABS/ruby-methods
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolutions.rb
220 lines (178 loc) · 3.08 KB
/
solutions.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
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
# Basic Solution to Exercise 1
def p_times(statement,num)
i = 0
while i < num do
puts statement
i += 1
end
end
# A more ruby-esque way to do it
def p_times2(statement,num)
num.times do
puts statement
end
end
# p_times2('hello',5)
# Exercise 2
def letter_count(str)
ans = {}
i = 0
while i < str.length do
if ans[str[i]] == nil
ans[str[i]] = 1
else
ans[str[i]] += 1
end
i += 1
end
ans
end
def letter_count2(str)
ans = {}
str.each_char do |c|
if ans.has_key? c
ans[c] += 1
else
ans[c] = 1
end
end
ans
end
def letter_count3(str)
str.each_char.inject (Hash.new) do |memo, ch|
memo[ch] ? memo[ch] += 1 : memo[ch] = 1
memo
end
end
# puts letter_count 'hello'
# Exercise 3
def mock_me
while true do
mock = gets.chomp
if mock == 'quit' || mock == 'q'
break
else
puts mock
end
end
end
# mock_me
# Exercise 4
def print_contacts(contacts)
contacts.each do |name, phone|
puts name, phone
end
end
contacts_hash = {'juliana' => '555-555-5555', 'anne' => '666-666-6666', 'jenny' => "867-5309"}
# print_contacts contacts_hash
# Exercise 5
def get_contact(contacts)
puts contacts
puts 'Name: '
name = gets.chomp
puts 'Phone: '
phone = gets.chomp
contacts[name] = phone
contacts
end
# puts get_contact contacts_hash
def get_contacts(existing_contacts)
puts "add contact? (y or n)"
add_contact = gets.chomp.downcase == "y"
while add_contact do
get_contact(existing_contacts)
puts "add another contact? (y or n)"
add_contact = gets.chomp.downcase == "y"
end
existing_contacts
end
# puts get_contacts contacts_hash
## Enumerable Inject/Reduce Exercises
# Enumerable Inject 1
def get_sum(arr)
arr.inject do |memo, x|
memo + x
end
end
# puts get_sum [1,2,3]
# Enumerable Inject 2
def get_max(arr)
arr.inject do |memo, x|
if memo < x
x
else
memo
end
end
end
# puts get_max [1,2,5,4,3]
# Enumerable Inject 3
def get_min(arr)
arr.inject do |memo,x|
if memo < x
memo
else
x
end
end
end
# puts get_min [1,2,-2,5,4,3]
# Enumerable Inject 4
def reverse_str(str)
str.chars.inject do |memo, x|
x + memo
end
end
# puts reverse_str "hello world"
# Enumerable Inject Challenge
def partial_sums(arr)
sums = [0]
arr.inject(0) do |memo,x|
sums.push(memo+x)
memo+x
end
sums
end
# p partial_sums [1,2,3]
# Array Map 1
def multiply_by(num,arr)
arr.map do |x|
x * num
end
end
# p multiply_by 3, [2, 1]
# Array Map 2
def reverse_each(arr)
arr.map do |x|
reverse_str x
end
end
# p reverse_each ['hello','world','juliana']
# Array Map 3
def get_responses(arr)
arr.map do |x|
puts x
gets.chomp
end
end
# p get_responses ['what?','why?','how?']
## Method Exercises
def factorial(n)
if n < 2
1
else
n * factorial(n-1)
end
end
# puts factorial 4
# Method 2
def is_palindrome?(num)
num_string = num.to_s
num_string == num_string.reverse
end
# puts is_palindrome? 2442
# Method 3
def reverse_str(str)
str.reverse
end
# puts reverse_str 'hello'