-
Notifications
You must be signed in to change notification settings - Fork 15
/
ruby-exercise-3-answer.rb
53 lines (42 loc) · 1.27 KB
/
ruby-exercise-3-answer.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
#The exercise is to make the last exercise answer clearer, shorter and simpler using methods
def to_words s
s.split /[\W]+/
end
def starts_with_vowel s
#FIXME Errors on an empty string
#w{a e i o u} is a quick way of writing ['a', 'e', 'i', 'o', 'u']
%w{a e i o u}.include? s[0].downcase #Can you see why we use downcase here?
end
def has_odd_number_words s
to_words(s).count % 2 == 1
end
def last_word line
line.split(/[\W]+/).last
end
snowy = File.read 'the-man-from-snowy-river.txt'
snowy_lines = snowy.lines
snowy_words = to_words snowy
#Example
puts "Here are the lines of Snowy that start with a vowel"
snowy_lines.each do |line|
puts line if starts_with_vowel line
end
puts
puts "Here are the lines of Snowy that have an odd number of words"
#hint: a % b is the remainder after you divide a by b (called "mod" or "modulus")
# for example 17 % 2 == 1
snowy_lines.each do |line|
puts line if has_odd_number_words line
end
puts
puts "Here are all the rhyming pairs from the ends of the Snowy lines"
i = 0
while i < snowy_lines.count
[0, 1, 4, 5].each do |n|
first_word = last_word snowy_lines[i + n]
second_word = last_word snowy_lines[i + n + 2]
puts "#{first_word}, #{second_word}"
end
puts
i += 9 # This is a short way of writing i = i + 1
end