Class: MaxAmount::Seeker

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

Constant Summary collapse

MAX_SIZE_DIGITAL =

максимальный размер целого числа

1_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Seeker

Returns a new instance of Seeker.



10
11
12
13
14
# File 'lib/max_amount/seeker.rb', line 10

def initialize(params = {})
  @input_data = params[:text]

  @count_numbers = params[:nmax]
end

Instance Attribute Details

#count_numbersObject

Returns the value of attribute count_numbers.



8
9
10
# File 'lib/max_amount/seeker.rb', line 8

def count_numbers
  @count_numbers
end

#input_dataObject

Returns the value of attribute input_data.



8
9
10
# File 'lib/max_amount/seeker.rb', line 8

def input_data
  @input_data
end

Instance Method Details

#check_errorsObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/max_amount/seeker.rb', line 16

def check_errors
  raise InputDataCountNumbersMustExist if @input_data.nil? || @count_numbers.nil?

  raise InputDataEmpty if @input_data.empty?

  raise CountNumbersMustBePositive if @count_numbers.negative?

  raise CountNumbersNotEqualZero if @count_numbers.zero?

  self
end

#number_length(digit) ⇒ Object



46
47
48
49
50
# File 'lib/max_amount/seeker.rb', line 46

def number_length(digit)
  # Math.log10(digit).to_i + 1
  # или
  digit.to_s.chars.map(&:to_i).count
end

#searchObject

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/max_amount/seeker.rb', line 28

def search
  # берем из строки только целые неотрицательные числа
  numbers = input_data.scan(/\d+/).select do |num|
    num.to_i.is_a?(Integer) && num.to_i.positive?
  end.map(&:to_i).uniq

  # длина каждого числа не может превышать заданную величину "MAX_SIZE_DIGITAL"
  numbers.each { |num| raise InvalidDigitInput if number_length(num.to_i) > MAX_SIZE_DIGITAL }

  # числа должны быть
  raise NotContainZero if numbers.size.zero?

  numbers.sort!.reverse!

  # вывод наибольших целых чисел в количестве "count_numbers" штук
  numbers[0..count_numbers - 1]
end