-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise4.rb
224 lines (175 loc) · 4.69 KB
/
Exercise4.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
221
222
223
####################
# exercise1:
####################
# scope -- first prints as a result of the method method call, then prints value of a
####################
# exercise2:
####################
# to_s will convert symbols to strings, but to_str will not
# if you want something to look like a string, use to_s. If you want something to act like a string, use to_str.
# http://groups.google.com/group/ruby-talk-google/browse_thread/thread/360cd68a6120a826
puts :oliver.to_s.class
puts 'oliver'.to_str.class
####################
puts 'exercise3:'
####################
# File.readlines('plainTxt.txt', 'r+') do |f1|
# while line = f1.gets
# a = line.split
# a.each do |item|
# item == 'word'? item.replace('inserted word') : item
# puts 'item:' + item
# end
# puts line
# #??????????????????????????????????????????????
# end
# end
# a = File.readlines('plainTxt.txt')
# b = File.open('plainTxtReplaced.txt', 'w')
# a.each do |line|
# line.sub('word', 'FART')
# b.write(line)
# end
# b.close
input = File.read('plainTxt.txt').sub('word', 'inserted word')
output = File.open('plainTxtReplaced.txt', 'w')
output.write(input)
output.close
####################
puts 'exercise4:'
####################
puts Dir.pwd
puts Dir.mkdir('tmp')
puts Dir.chdir('tmp')
puts Dir.pwd
puts Dir.chdir('..')
puts Dir.pwd
puts Dir.rmdir('tmp')
####################
# exercise5:
####################
# what will not be displayed:
# a, e, f
####################
puts 'exercise6:'
####################
# see p021rangesex.rb
s = 'key=value'
s1, s2 = s.split('=')
puts "s1 = #{s1} s2 = #{s2}"
####################
# exercise7:
####################
# see p026zdeafgm1.rb
####################
# exercise8:
####################
# see analyzer.rb
####################
puts 'exercise9:'
####################
# Write a Ruby program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Discuss this in the FizzBuzz Forum.
def fizz_buzz
(1..100).each do |num|
fizz = num % 3
buzz = num % 5
if (fizz == 0) && (buzz == 0)
puts 'FizzBuzz'
elsif (fizz == 0)
puts 'Fizz'
elsif (buzz == 0)
puts 'Buzz'
else puts num
end
end
end
fizz_buzz
# flexibile using collections:
# def check_number(i)
# s = ''
# s = 'Fizz' if i % 3 == 0
# s << 'Buzz' if i % 5 == 0
# s == '' ? i : s
# end
#
# def fizzbuzz(range)
# a = range.to_a
# a.collect {|i| check_number(i)}
# end
#
# puts fizzbuzz(1..100)
####################
# exercise10:
####################
s = 'the sentence i want to reverse'
puts s.split.reverse.join(' ')
####################
# exercise11:
####################
# see p020arraysum.rb
####################
# exercise12:
####################
# see p021oddeven.rb
####################
puts 'exercise13:'
####################
quiz = [0,0,0,1,0,0,1,1,0,1]
def took_quiz?(array)
didnot, total = 0, 0
array.each do |num|
num == 0 ? didnot += 1 : didnot
total += 1
end
puts "Number of participants who did not attempt Quiz 1 is #{didnot} out of #{total} total participants"
end
puts took_quiz?(quiz)
# also
completed = 0
quiz.each do |attempt|
completed += 1 unless attempt.zero?
end
puts "Quiz 1 is #{quiz.size - completed} out of #{quiz.size}"
# also:
s = quiz.inject(0) { |sum, elem| sum += 1-elem }
puts "The number of participants who did not attempt Quiz 1 is #{s} out of #{quiz.length} total participants."
# also
QUIZ_TAKEN_ARR = [1]
puts "The number of participants who did not attempt Quiz 1 is #{(quiz - QUIZ_TAKEN_ARR).size} out of #{quiz.size} total participants."
####################
# exercise14:
####################
#record = {:name, 'Satish', :email, '[email protected]', :phone, '919371006659'}
record = {:name => 'Satish', :email => '[email protected]', :phone => '919371006659'}
puts record[:name]
####################
# exercise15:
####################
# scope changed to always local for blocks in 1.9
####################
# exercise16:
####################
h = { "Ruby" => "Matz", "Perl" => "Larry", "Python" => "Guido" }
puts h.member?("Matz") #f
puts h.member?("Python") #t
puts h.include?("Guido") #f
puts h.include?("Ruby") #t
puts h.has_value?("Larry")#t
#puts h.exists?("Perl") #undefined
####################
# exercise17:
####################
for i, j in [[1, 2], [3, 4], [5, 6]]
p [i, j]
end
# answer 3:
# => [1,2]
# => [3,4]
# => [5,6]
####################
# exercise18:
####################
# ascending sort by string length: answer=> 4, 6
a = ["Magazine", "Sunday", "Jump"]
puts a.sort { |l, r| l.length <=> r.length }
puts a.sort_by { |s| s.length }