max_amount 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f686f6a46fe5d18f487fea188762d9523b1f7b3808ae17ba7fa3a99939bf5a11
4
+ data.tar.gz: af10d44ec755e84738617a3519a9872095fd7deeec09cfd29dafdd8a23162a90
5
+ SHA512:
6
+ metadata.gz: 7b8275724afb4a1d50a20d209f02beb849e2e5aa6b6d89dcd6695e40237009b1c9624520d6b96a8236a08cdae04cceded24edfdb14d59675738300c7fc529a56
7
+ data.tar.gz: 7d8c4641398c4ee0581fd4880301438cabdaaf86d2dcfe4626b4b0e5ada41d7be11401488e3ec3ac6b1f11017f68b0ee4d19bdf47566a70850623449bd77e0df
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format doc
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+ Exclude:
16
+ - spec/**/*.rb
17
+
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - spec/**/*.rb
21
+
22
+ Metrics/AbcSize:
23
+ Enabled: false
24
+
25
+ Style/Documentation:
26
+ Enabled: false
27
+
28
+ Metrics/MethodLength:
29
+ Max: 50
30
+
31
+ RSpec/MultipleExpectations:
32
+ Max: 10
33
+
34
+ RSpec/ExampleLength:
35
+ Max: 20
36
+
37
+ RSpec/FilePath:
38
+ Enabled: false
39
+
40
+ RSpec/NoExpectationExample:
41
+ Enabled: false
42
+
43
+ Metrics/CyclomaticComplexity:
44
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-09-30
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in max_amount.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ group :test do
11
+ gem 'byebug', platforms: %i[mri mingw x64_mingw]
12
+ gem 'codecov', require: false
13
+ gem 'dotenv', '~> 2.7'
14
+ gem 'rspec', '~> 3.1'
15
+ gem 'rubocop', '~> 1.36'
16
+ gem 'rubocop-performance', '~> 1.15'
17
+ gem 'rubocop-rspec', '~> 2.13'
18
+ gem 'simplecov', require: false
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ max_amount (0.1.0)
5
+ optparse
6
+ securerandom
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ byebug (11.1.3)
13
+ codecov (0.6.0)
14
+ simplecov (>= 0.15, < 0.22)
15
+ diff-lcs (1.5.0)
16
+ docile (1.4.0)
17
+ dotenv (2.8.1)
18
+ json (2.6.2)
19
+ optparse (0.2.0)
20
+ parallel (1.22.1)
21
+ parser (3.1.2.1)
22
+ ast (~> 2.4.1)
23
+ rainbow (3.1.1)
24
+ rake (13.0.6)
25
+ regexp_parser (2.6.0)
26
+ rexml (3.2.5)
27
+ rspec (3.11.0)
28
+ rspec-core (~> 3.11.0)
29
+ rspec-expectations (~> 3.11.0)
30
+ rspec-mocks (~> 3.11.0)
31
+ rspec-core (3.11.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-expectations (3.11.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-mocks (3.11.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.11.0)
39
+ rspec-support (3.11.1)
40
+ rubocop (1.36.0)
41
+ json (~> 2.3)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.1.2.1)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml (>= 3.2.5, < 4.0)
47
+ rubocop-ast (>= 1.20.1, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 3.0)
50
+ rubocop-ast (1.21.0)
51
+ parser (>= 3.1.1.0)
52
+ rubocop-performance (1.15.0)
53
+ rubocop (>= 1.7.0, < 2.0)
54
+ rubocop-ast (>= 0.4.0)
55
+ rubocop-rspec (2.13.2)
56
+ rubocop (~> 1.33)
57
+ ruby-progressbar (1.11.0)
58
+ securerandom (0.2.0)
59
+ simplecov (0.21.2)
60
+ docile (~> 1.1)
61
+ simplecov-html (~> 0.11)
62
+ simplecov_json_formatter (~> 0.1)
63
+ simplecov-html (0.12.3)
64
+ simplecov_json_formatter (0.1.4)
65
+ unicode-display_width (2.3.0)
66
+
67
+ PLATFORMS
68
+ x86_64-linux
69
+
70
+ DEPENDENCIES
71
+ byebug
72
+ codecov
73
+ dotenv (~> 2.7)
74
+ max_amount!
75
+ rake (~> 13.0)
76
+ rspec (~> 3.1)
77
+ rubocop (~> 1.36)
78
+ rubocop-performance (~> 1.15)
79
+ rubocop-rspec (~> 2.13)
80
+ simplecov
81
+
82
+ BUNDLED WITH
83
+ 2.3.21
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Gleb V. Zhegilin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # MaxAmount
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/max_amount`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add max_amount
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install max_amount
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/max_amount.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+
5
+ begin
6
+ require 'bundler/setup'
7
+ Bundler::GemHelper.install_tasks
8
+ rescue LoadError
9
+ puts 'although not required, bundler is recommened for running the tests'
10
+ end
11
+
12
+ require 'bundler/gem_tasks'
13
+ require 'rspec/core/rake_task'
14
+
15
+ RSpec::Core::RakeTask.new(:spec)
16
+
17
+ require 'rubocop/rake_task'
18
+
19
+ RuboCop::RakeTask.new do |task|
20
+ task.requires << 'rubocop-performance'
21
+ task.requires << 'rubocop-rspec'
22
+ end
23
+
24
+ task default: %i[spec rubocop]
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/main'
4
+
5
+ # путь к файлу с пользовательской строкой
6
+ path = File.expand_path('../spec/support/fixtures/sample_data.txt', __dir__)
7
+
8
+ # если файла нет, то присваиваем nil
9
+ custom_string = File.exist?(path) ? File.read(path) : nil
10
+
11
+ MaxAmount::CLI.call(custom_string: custom_string)
data/lib/main.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'max_amount/version'
4
+
5
+ require 'byebug'
6
+ require 'optparse'
7
+ require 'securerandom'
8
+ require 'dotenv/load'
9
+
10
+ require_relative 'max_amount/options'
11
+ require_relative 'max_amount/cli'
12
+ require_relative 'max_amount/errors'
13
+ require_relative 'max_amount/extract_integer'
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MaxAmount
4
+ module CLI
5
+ extend MaxAmount::Options
6
+
7
+ def self.call(custom_string:)
8
+ options = MaxAmount::Options.ask
9
+
10
+ case options[:m]
11
+ when '1'
12
+ str = Dotenv.load('../.env')['TEST_STRING']
13
+ when '2'
14
+ str = SecureRandom.hex(options[:l].to_i)
15
+ when '3'
16
+ str = custom_string
17
+ end
18
+
19
+ params = {
20
+ text: str,
21
+ nmax: options[:n]&.to_i
22
+ }
23
+
24
+ params[:text] = params[:text].to_s.chomp unless params[:text].is_a?(String)
25
+
26
+ begin
27
+ output = MaxAmount::Seeker.new(params).check_errors.search.join("\n")
28
+ puts output
29
+ rescue MaxAmount::Error => e
30
+ message = e.message
31
+ puts message
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MaxAmount
4
+ class Error < StandardError
5
+ end
6
+
7
+ class InputDataEmpty < Error
8
+ def message
9
+ 'The string cannot be empty'
10
+ end
11
+ end
12
+
13
+ class CountNumbersMustBePositive < Error
14
+ def message
15
+ 'The entered number must be positive'
16
+ end
17
+ end
18
+
19
+ class CountNumbersNotEqualZero < Error
20
+ def message
21
+ 'The entered number cannot equal to zero'
22
+ end
23
+ end
24
+
25
+ class InputDataCountNumbersMustExist < Error
26
+ def message
27
+ 'The string and number must exist'
28
+ end
29
+ end
30
+
31
+ class NotContainZero < Error
32
+ def message
33
+ 'Must have at least one number'
34
+ end
35
+ end
36
+
37
+ class InvalidDigitInput < Error
38
+ def message
39
+ 'The length of the number cannot exceed the specified value'
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MaxAmount
4
+ class Seeker
5
+ # максимальный размер целого числа
6
+ MAX_SIZE_DIGITAL = 1_000
7
+
8
+ attr_accessor :input_data, :count_numbers
9
+
10
+ def initialize(params = {})
11
+ @input_data = params[:text]
12
+
13
+ @count_numbers = params[:nmax]
14
+ end
15
+
16
+ def check_errors
17
+ raise InputDataCountNumbersMustExist if @input_data.nil? || @count_numbers.nil?
18
+
19
+ raise InputDataEmpty if @input_data.empty?
20
+
21
+ raise CountNumbersMustBePositive if @count_numbers.negative?
22
+
23
+ raise CountNumbersNotEqualZero if @count_numbers.zero?
24
+
25
+ self
26
+ end
27
+
28
+ def search
29
+ # берем из строки только целые неотрицательные числа
30
+ numbers = input_data.scan(/\d+/).select do |num|
31
+ num.to_i.is_a?(Integer) && num.to_i.positive?
32
+ end.map(&:to_i).uniq
33
+
34
+ # длина каждого числа не может превышать заданную величину "MAX_SIZE_DIGITAL"
35
+ numbers.each { |num| raise InvalidDigitInput if number_length(num.to_i) > MAX_SIZE_DIGITAL }
36
+
37
+ # числа должны быть
38
+ raise NotContainZero if numbers.size.zero?
39
+
40
+ numbers.sort!.reverse!
41
+
42
+ # вывод наибольших целых чисел в количестве "count_numbers" штук
43
+ numbers[0..count_numbers - 1]
44
+ end
45
+
46
+ def number_length(digit)
47
+ # Math.log10(digit).to_i + 1
48
+ # или
49
+ digit.to_s.chars.map(&:to_i).count
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MaxAmount
4
+ module Options
5
+ def self.ask
6
+ # Все наши опции будут записаны сюда
7
+ options = {}
8
+
9
+ # Заведем опции
10
+ OptionParser.new do |opt|
11
+ # Этот текст будет выводиться, когда программа запущена с опцией -h
12
+ opt.banner = 'Чтение из входящего потока текстовых данных:'
13
+
14
+ # Вывод в случае, если запросили help
15
+ opt.on('--h', 'Справка') do
16
+ puts opt
17
+ exit
18
+ end
19
+
20
+ # Опция --n будет передавать n самых больших целых чисел, встречающихся
21
+ # в текстовых данных
22
+ opt.on('--n Nmax', 'n самых больших целых чисел ' \
23
+ '(n > 0)') { |o| options[:n] = o }
24
+
25
+ # Опция --l передает, какую мы хотим задать длину строки
26
+ opt.on('--l length', 'длина строки ') { |o| options[:l] = o }
27
+
28
+ # Опция --m передает то, хотим ли мы выбрать тестовую строку - [1]
29
+ # или случайно сгенерированную - [2]
30
+ lines = 'тестовая строка (m=1) / случайно сгенерированная строка (m=2) / пользовательская строка (m=3)'
31
+ opt.on('--m choice', lines) { |o| options[:m] = o }
32
+ end.parse!
33
+
34
+ options
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MaxAmount
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,4 @@
1
+ module MaxAmount
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: max_amount
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gleb V. Zhegilin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: optparse
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: securerandom
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Reading text data and outputting the n largest integers encountered in
42
+ the received text data.When input is complete, prints the n largest integers.
43
+ email:
44
+ - gleboceanborn@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files:
48
+ - README.md
49
+ files:
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - CHANGELOG.md
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - examples/max_amount.rb
59
+ - lib/main.rb
60
+ - lib/max_amount/cli.rb
61
+ - lib/max_amount/errors.rb
62
+ - lib/max_amount/extract_integer.rb
63
+ - lib/max_amount/options.rb
64
+ - lib/max_amount/version.rb
65
+ - sig/max_amount.rbs
66
+ homepage: https://github.com/ProfessorNemo/max_amount
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ rubygems_mfa_required: 'true'
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '3.0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.3.22
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Reading text data and outputting the n largest integers encountered in the
90
+ received text data.
91
+ test_files: []