Class: Tasks::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/tasks.rb

Overview

50 tasks

Class Method Summary collapse

Class Method Details

.all_tasksObject

return array with numbers of all tasks



421
422
423
424
425
426
427
# File 'lib/tasks.rb', line 421

def all_tasks
  method_name = 'task_'
  methods = Task.methods(false)
  tasks = methods.select { |elem| elem.to_s.include? method_name }
  tasks.map! { |elem| elem.to_s.delete method_name }
  tasks.map!(&:to_i).sort!
end

.array_separators(array:) ⇒ Object

array of prime numbers



17
18
19
# File 'lib/tasks.rb', line 17

def array_separators(array:)
  array.select { |x| (1..x).select { |y| (x % y).zero? }.length <= 2 }
end

.conditions(num:) ⇒ Object

return task condition by number



430
431
432
433
434
435
436
437
# File 'lib/tasks.rb', line 430

def conditions(num:)
  num = num.to_i
  if all_tasks.include? num
    CONDITIONS[num]
  else
    'Task not found.'
  end
end

.hint(digit1:, digit2:) ⇒ Object

hint when comparing numbers



39
40
41
42
43
44
45
# File 'lib/tasks.rb', line 39

def hint(digit1:, digit2:)
  if digit1 > digit2
    'more than needed'
  elsif digit1 < digit2
    'less than needed'
  end
end

.horse_place(run:, horse_num:) ⇒ Object

place of the horse, task 988



396
397
398
399
400
401
402
403
404
405
# File 'lib/tasks.rb', line 396

def horse_place(run:, horse_num:)
  horse_num -= 1
  if run[horse_num] == run.max
    'Victory!'
  elsif run[horse_num] == run.min
    'Last.'
  else
    'Second.'
  end
end

.new_array(quantity:, lower_bound: -100,, top_bound: 100) ⇒ Object

array with random digits in diapason lower_bound..y



32
33
34
35
36
# File 'lib/tasks.rb', line 32

def new_array(quantity:, lower_bound: -100, top_bound: 100)
  array = []
  quantity.times { array.push(rand(lower_bound..top_bound)) }
  array
end

.new_matrix(n_lines:, m_lines:) ⇒ Object

matrix n x m



22
23
24
25
26
27
28
29
# File 'lib/tasks.rb', line 22

def new_matrix(n_lines:, m_lines:)
  array = []
  (0..(n_lines - 1)).each do |i|
    array[i] = []
    (0..(m_lines - 1)).each { array[i].push(rand(1..10)) }
  end
  array
end

.number_array(digit:) ⇒ Object

number => array, example 234 => [2, 3, 4]



12
13
14
# File 'lib/tasks.rb', line 12

def number_array(digit:)
  digit.to_s.split('').map(&:to_i)
end

.task_1(a:, b:) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/tasks.rb', line 47

def task_1(a:, b:)
  sum = a + b
  diff = a - b
  mult = a * b
  { sum: sum.round(2),
    difference: diff.round(2),
    multiplication: mult.round(2) }
end

.task_10(height:) ⇒ Object



83
84
85
86
# File 'lib/tasks.rb', line 83

def task_10(height:)
  result = Math.sqrt((2 * height) / 9.81)
  { time: result.round(2) }
end

.task_12(side_of_triangle:) ⇒ Object



88
89
90
91
# File 'lib/tasks.rb', line 88

def task_12(side_of_triangle:)
  result = Math.sqrt(3) * side_of_triangle / 4 * side_of_triangle**2
  { Square: result.round(2) }
end

.task_13(pendulum_length:) ⇒ Object



93
94
95
96
# File 'lib/tasks.rb', line 93

def task_13(pendulum_length:)
  result = (2 * 3.14 * Math.sqrt(pendulum_length / 9.81)).round(2)
  { period: result }
end

.task_15(cathetus_a:, hypotenuse:) ⇒ Object



98
99
100
101
102
# File 'lib/tasks.rb', line 98

def task_15(cathetus_a:, hypotenuse:)
  cathetus_b = Math.sqrt(hypotenuse**2 - cathetus_a**2)
  square = ((cathetus_a + cathetus_b - hypotenuse) / 2).round(2)
  { cathetus_b: cathetus_b.round(2), Square: square }
end

.task_16(circumference:) ⇒ Object



104
105
106
107
# File 'lib/tasks.rb', line 104

def task_16(circumference:)
  result = Math::PI * Math.sqrt(circumference / (2 * Math::PI))
  { Square: result.round(2) }
end

.task_182(n:) ⇒ Object



173
174
175
176
177
178
# File 'lib/tasks.rb', line 173

def task_182(n:)
  array = new_array(quantity: n.to_i)
  array2 = array.select { |elem| (elem % 5).zero? && elem % 7 != 0 }
  sum = array2.reduce(:+)
  { arr_new: array2, sum: sum, quantity: array2.length }
end

.task_185(n:) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/tasks.rb', line 180

def task_185(n:)
  array = new_array(quantity: n.to_i)
  array2 = array.select { |elem| elem > 0 }
  sum = if array2 != []
          array2.reduce(:+)**2
        else
          0
        end
  { array: array2, sum: sum }
end

.task_191(n:) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/tasks.rb', line 191

def task_191(n:)
  quantity = 0
  array = new_array(quantity: n.to_i)
  array.map! do |elem|
    quantity += 1 if elem > 7
    elem > 7 ? 7 : elem
  end
  { array: array, quantity: quantity }
end

.task_2(x:, y:) ⇒ Object



56
57
58
59
60
# File 'lib/tasks.rb', line 56

def task_2(x:, y:)
  difference = x.abs - y.abs
  multiplication = (x * y).abs
  (difference / (1 + multiplication)).round(4)
end

.task_205(n:) ⇒ Object



201
202
203
204
205
206
# File 'lib/tasks.rb', line 201

def task_205(n:)
  array = new_array(quantity: n.to_i)
  max = array.map(&:abs).max
  sum = Math.sqrt(array.map { |x| x * x }.reduce(:+))
  { array: array, max: max, sum: sum }
end

.task_207(n:) ⇒ Object



208
209
210
211
212
213
# File 'lib/tasks.rb', line 208

def task_207(n:)
  arr = number_array(digit: n.to_i).delete_if do |x|
    x.zero? || x == 5
  end
  arr.join.to_i
end

.task_224(n:) ⇒ Object



215
216
217
218
# File 'lib/tasks.rb', line 215

def task_224(n:)
  n = n.to_i
  (1..n).select { |x| (n % x).zero? }
end

.task_225(n:) ⇒ Object



220
221
222
223
# File 'lib/tasks.rb', line 220

def task_225(n:)
  n = n.to_i
  (1..n).select {|x| (n % x**2).zero? && n % x**3 != 0 }
end

.task_230(n:) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/tasks.rb', line 225

def task_230(n:)
  min = 1000
  num_array = new_array(quantity: n.to_i)
  num_array.inject do |x, y|
    min = (x - y).abs if (x - y).abs < min
    y
  end
  { array: num_array, min: min }
end

.task_24(x1:, x2:, y1:, y2:) ⇒ Object



109
110
111
112
113
114
# File 'lib/tasks.rb', line 109

def task_24(x1:, x2:, y1:, y2:)
  diff_x = x1 - x2
  diff_y = y1 - y2
  result = Math.sqrt(diff_x**2 + diff_y**2)
  result.round(2)
end

.task_272(n:) ⇒ Object



235
236
237
238
239
240
241
242
243
# File 'lib/tasks.rb', line 235

def task_272(n:)
  precipitation = new_array(quantity: n.to_i,
                            lower_bound: 1,
                            top_bound: 100)
  average = precipitation.reduce(:+) / precipitation.size
  deviation = []
  precipitation.each { |x| deviation.push(x - average) }
  { precipitation: precipitation, average: average, deviation: deviation }
end

.task_279(n:) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/tasks.rb', line 245

def task_279(n:)
  n = n.to_i
  a = new_array(quantity: n)
  b = new_array(quantity: n).reverse!
  ab = []
  (0..n - 1).each { |i| ab.push(a[i] + b[i]) }
  { array_a: a, array_b: b, array_ab: ab }
end

.task_3(edge_length:) ⇒ Object



62
63
64
65
66
# File 'lib/tasks.rb', line 62

def task_3(edge_length:)
  volume = edge_length**3
  square = edge_length**2 * 6
  { Volume: volume.round(2), Square: square.round(2) }
end

.task_30(x:) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/tasks.rb', line 116

def task_30(x:)
  degree3 = x**3
  middle = 2 * x + 3 * x**2
  res1 = 1 - middle - 4 * degree3
  res2 = 1 + middle + 4 * degree3
  { result1: res1.round(2), result2: res2.round(2) }
end

.task_302(n:) ⇒ Object



254
255
256
257
258
# File 'lib/tasks.rb', line 254

def task_302(n:)
  number = number_array(digit: n.to_i)
  number.uniq!
  number.length
end

.task_317(n:) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/tasks.rb', line 260

def task_317(n:)
  n = n.to_i
  array = new_array(quantity: n)
  sum_array = 0
  (0..n - 1).each { |i| sum_array += array[i]**(i + 1) }
  { array: array, sum: sum_array }
end

.task_325(n:) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/tasks.rb', line 268

def task_325(n:)
  n = n.to_i
  separators = (1..n).select do |x|
    (n % x).zero?
  end
  array_separators(array: separators)
end

.task_328Object



276
277
278
# File 'lib/tasks.rb', line 276

def task_328
  array_separators(array: (1..100))
end

.task_33(x:, y:) ⇒ Object



124
125
126
127
# File 'lib/tasks.rb', line 124

def task_33(x:, y:)
  array = [x, y]
  { min: array.min, max: array.max }
end

.task_34(x:, y:, z:) ⇒ Object



129
130
131
132
# File 'lib/tasks.rb', line 129

def task_34(x:, y:, z:)
  array = [x, y, z]
  { min: array.min, max: array.max }
end

.task_39(digit1:, digit2:) ⇒ Object



134
135
136
# File 'lib/tasks.rb', line 134

def task_39(digit1:, digit2:)
  digit1 > digit2 ? digit1 : [digit1, digit2]
end

.task_41(x:, y:, z:) ⇒ Object



138
139
140
141
# File 'lib/tasks.rb', line 138

def task_41(x:, y:, z:)
  array = [x, y, z]
  array.select { |x| x <= 3 && x >= 1 }
end

.task_43(x:, y:, z:) ⇒ Object



143
144
145
146
# File 'lib/tasks.rb', line 143

def task_43(x:, y:, z:)
  array = [x, y, z]
  array.select { |x| x > 0 }.map { |x| x**2 }
end

.task_536(n:) ⇒ Object



280
281
282
283
284
# File 'lib/tasks.rb', line 280

def task_536(n:)
  array = new_array(quantity: n.to_i)
  result = array != array.uniq ? 'yes' : 'no'
  { array: array, identical_elements: result }
end

.task_555(n:) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/tasks.rb', line 286

def task_555(n:)
  n.to_i
  a = []
  (0..n - 1).each do |i|
    a[i] = []
    a[i].push(1)
    if i > 0
      (0..a[i - 1].size - 2).each do |j|
        a[i].unshift(a[i - 1][j] + a[i - 1][j + 1])
      end
      a[i].unshift(1)
    end
  end
  a
end

.task_561(n:) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/tasks.rb', line 302

def task_561(n:)
  n = n.to_i
  new_array = []
  (1..n).each do |s|
    array = number_array(digit: s * s)
    if array.last(number_array(digit: s).size).join.to_i == s
      new_array.push(s)
    end
  end
  new_array
end

.task_6(cathetus_a:, cathetus_b:) ⇒ Object



68
69
70
71
72
# File 'lib/tasks.rb', line 68

def task_6(cathetus_a:, cathetus_b:)
  hypotenuse = Math.sqrt(cathetus_a**2 + cathetus_b**2).round(2)
  square = (cathetus_a * cathetus_b / 2).round(2)
  { hypotenuse: hypotenuse, Square: square }
end

.task_606(a:, b:, c:, d:) ⇒ Object



314
315
316
317
318
319
320
321
322
# File 'lib/tasks.rb', line 314

def task_606(a:, b:, c:, d:)
  rectangle = [a, b, c, d]
  sum = rectangle.reduce(:+)
  rectangle_flag = true
  (0..3).each do |i|
    rectangle_flag = false if (sum - rectangle[i]) < rectangle[i]
  end
  rectangle_flag ? 'yes' : 'no'
end

.task_62(digit:) ⇒ Object



148
149
150
151
# File 'lib/tasks.rb', line 148

def task_62(digit:)
  digit.to_i
  (digit % 2).zero? ? 'yes' : 'no'
end

.task_64(digit:) ⇒ Object



153
154
155
# File 'lib/tasks.rb', line 153

def task_64(digit:)
  (digit / 100).to_i
end

.task_65(digit:) ⇒ Object



157
158
159
160
# File 'lib/tasks.rb', line 157

def task_65(digit:)
  sum_array = number_array(digit: digit.to_i).reduce(:+)
  sum_array**3 == digit**2 ? 'yes' : 'no'
end

.task_67(digit:) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/tasks.rb', line 162

def task_67(digit:)
  digit = digit.to_i
  array = number_array(digit: digit)
  penultimate_number = array[array.length - 2] if digit >= 10
  { digits_number: array.length,
    sum: array.reduce(:+),
    last_digit: array[array.length - 1],
    first_digit: array[0],
    penultimate_number: penultimate_number }
end

.task_697(k:, m:, l:) ⇒ Object



324
325
326
327
328
329
330
331
332
# File 'lib/tasks.rb', line 324

def task_697(k:, m:, l:)
  k = k.to_i
  m = m.to_i
  l = l.to_i
  matrix_a = Matrix.rows(new_matrix(n_lines: k, m_lines: m))
  matrix_b = Matrix.rows(new_matrix(n_lines: m, m_lines: l))
  mult = matrix_a * matrix_b
  { matrix_a: matrix_a, matrix_b: matrix_b, result: mult }
end

.task_698(n:) ⇒ Object



334
335
336
337
338
# File 'lib/tasks.rb', line 334

def task_698(n:)
  n = n.to_i
  matrix = Matrix.rows(new_matrix(n_lines: n, m_lines: n))
  { matrix: matrix, result: matrix**2 }
end

.task_699(n:) ⇒ Object



340
341
342
343
344
345
346
# File 'lib/tasks.rb', line 340

def task_699(n:)
  n = n.to_i
  matrix_a = Matrix.rows(new_matrix(n_lines: n, m_lines: n))
  matrix_b = Matrix.rows(new_matrix(n_lines: n, m_lines: n))
  result = matrix_a * matrix_b - matrix_b * matrix_a
  { matrix_a: matrix_a, matrix_b: matrix_b, result: result }
end

.task_704(n:) ⇒ Object



348
349
350
351
352
353
354
355
356
# File 'lib/tasks.rb', line 348

def task_704(n:)
  n = n.to_i
  matrix_a = Matrix.rows(new_matrix(n_lines: n, m_lines: n))
  matrix_b = Matrix.rows(new_matrix(n_lines: n, m_lines: n))
  matrix_c = Matrix.rows(new_matrix(n_lines: n, m_lines: n))
  result = (matrix_a + matrix_b) * matrix_c
  { matrix_a: matrix_a, matrix_b: matrix_b,
    matrix_c: matrix_c, result: result }
end

.task_710(m:, n:) ⇒ Object



358
359
360
361
# File 'lib/tasks.rb', line 358

def task_710(m:, n:)
  matrix = Matrix.rows(new_matrix(n_lines: m.to_i, m_lines: n.to_i))
  matrix.transpose
end

.task_8(n_corners:, radius:) ⇒ Object



74
75
76
77
# File 'lib/tasks.rb', line 74

def task_8(n_corners:, radius:)
  result = 2 * radius * Math.tan(3.14 / n_corners) * n_corners
  { Perimeter: result.round(4) }
end

.task_822(year:) ⇒ Object



363
364
365
# File 'lib/tasks.rb', line 363

def task_822(year:)
  (year.to_i % 4).zero? ? '366 days' : '365 days'
end

.task_823(n:, m:) ⇒ Object



367
368
369
370
371
372
373
# File 'lib/tasks.rb', line 367

def task_823(n:, m:)
  n = n.to_i
  m = m.to_i
  leap_years = 0
  (n..m).each { |y| leap_years += 1 if (y % 4).zero? }
  leap_years
end

.task_831(n:) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/tasks.rb', line 375

def task_831(n:)
  n = n.to_i
  october = Time.mktime(n, 10, 1)
  day1 = october.wday
  if day1 < 7
    day = 7 - day1
    Time.mktime(n, 10, day + 1)
  else
    Time.mktime(n, 10, day1)
  end
end

.task_9(resistance1:, resistance2:, resistance3:) ⇒ Object



79
80
81
# File 'lib/tasks.rb', line 79

def task_9(resistance1:, resistance2:, resistance3:)
  (1 / (1 / resistance1 + 1 / resistance2 + 1 / resistance3)).round(4)
end

.task_986(n:) ⇒ Object



387
388
389
390
391
392
393
# File 'lib/tasks.rb', line 387

def task_986(n:)
  digit = rand(0..9)
  digit_user = n.to_i
  return 'incorrect value' unless (0..9).cover? digit_user

  digit_user != digit ? hint(digit1: digit_user, digit2: digit) : 'Right you are!'
end

.task_988(horse_num:) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/tasks.rb', line 407

def task_988(horse_num:)
  horse_num = horse_num.to_i
  return 'incorrect value' unless (1..3).cover? horse_num

  finish = 500
  horses_run = []
  3.times { horses_run.push(rand(1..100)) }
  while horses_run.max < finish
    horses_run.map! { |x| x + rand(1..100) }
  end
  horse_place(run: horses_run, horse_num: horse_num)
end