-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex26_1.rb
executable file
·77 lines (61 loc) · 1.69 KB
/
ex26_1.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
#!/usr/bin/env ruby
# This function will break up words for us.
def break_words(stuff)
words = stuff.split(' ')
end
# Sorts the words.
def sort_words(words)
words.sort()
end
# Prints the first word after popping it off.
def puts_first_word(words)
word = words.shift()
puts word
end
# Prints the last word after popping it off.
def puts_last_word(words)
word = words.pop
puts word
end
# Takes in a full sentence and returns the sorted words.
def sort_sentence(sentence)
words = break_words(sentence)
sort_words(words)
end
# Puts the first and last words of the sentence.
def puts_first_and_last(sentence)
words = break_words(sentence)
puts_first_word(words)
puts_last_word(words)
end
# Sorts the words then prints the first and last one.
def puts_first_and_last_sorted(sentence)
words = sort_sentence(sentence)
puts_first_word(words)
puts_last_word(words)
end
puts "-"*50
puts "Let's play with sentences and words:"
# sentence = "All good things come to those who wait."
# User input
puts "Type a sentence to break up into separate words and play with:"
sentence = gets.chomp()
# Break the words up at spaces and put them into an array
words = break_words(sentence)
# Sort words
sorted_words = sort_words(words)
puts "First word (words):"
puts_first_word(words)
puts "Last word (words):"
puts_last_word(words)
puts "First word (sorted_words):"
puts_first_word(sorted_words)
puts "Last word (sorted_words):"
puts_last_word(sorted_words)
sorted_words = sort_sentence(sentence)
puts "Sorted_words:"
puts sorted_words
puts "First and last (sentence):"
puts_first_and_last(sentence)
puts "First and last sorted (sentence):"
puts_first_and_last_sorted(sentence)