class Tasks::Task

50 tasks

Public Class Methods

all_tasks() click to toggle source

return array with numbers of all tasks

# 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:) click to toggle source

array of prime numbers

# 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:) click to toggle source

return task condition by number

# 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:) click to toggle source

hint when comparing numbers

# 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:) click to toggle source

place of the horse, task 988

# 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) click to toggle source

array with random digits in diapason lower_bound..y

# 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:) click to toggle source

matrix n x m

# 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:) click to toggle source

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

# File lib/tasks.rb, line 12
def number_array(digit:)
  digit.to_s.split('').map(&:to_i)
end
task_1(a:, b:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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_328() click to toggle source
# File lib/tasks.rb, line 276
def task_328
  array_separators(array: (1..100))
end
task_33(x:, y:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# File lib/tasks.rb, line 134
def task_39(digit1:, digit2:)
  digit1 > digit2 ? digit1 : [digit1, digit2]
end
task_41(x:, y:, z:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# File lib/tasks.rb, line 148
def task_62(digit:)
  digit.to_i
  (digit % 2).zero? ? 'yes' : 'no'
end
task_64(digit:) click to toggle source
# File lib/tasks.rb, line 153
def task_64(digit:)
  (digit / 100).to_i
end
task_65(digit:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# File lib/tasks.rb, line 363
def task_822(year:)
  (year.to_i % 4).zero? ? '366 days' : '365 days'
end
task_823(n:, m:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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