nmax 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 38dc5db15e4077da7039677839c6575d62e12163
4
+ data.tar.gz: 6c6625a1c3e74cce2373701965d6dda37df87561
5
+ SHA512:
6
+ metadata.gz: ca644b7bbf2d41acc05e6e8666172d6bbfb46b2b3e8d10e27dcbd1c0ddf14698ff4ace6b48f5c68f538cc218cedf78040247d47378b02a665bf6c7eb7fa9625b
7
+ data.tar.gz: 5d76346b5403c6263ea57a21bcaa6b43f61ffc8b6324e734e359314674afb7a2e9035b46b98e0ef4844896314f8cc91fcf72f3b1f42e0dd69eeefcabeeb81e0e
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
@@ -0,0 +1,16 @@
1
+ # inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.3
5
+
6
+ Documentation:
7
+ Enabled: false
8
+
9
+ Metrics/LineLength:
10
+ Max: 120
11
+
12
+ Style/FrozenStringLiteralComment:
13
+ Enabled: false
14
+
15
+ Style/SymbolArray:
16
+ Enabled: true
@@ -0,0 +1,11 @@
1
+ cache: bundler
2
+ language: ruby
3
+ install:
4
+ - bundle install --retry=3
5
+ script:
6
+ - bundle exec rubocop
7
+ - bundle exec rake
8
+ rvm:
9
+ - 2.1
10
+ - 2.2
11
+ - 2.3.0
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'minitest'
7
+ gem 'rubocop'
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+ =====================
3
+
4
+ Copyright (c) 2014 Vladislav Petrov <electronnicchest@gmail.com>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ this software and associated documentation files (the "Software"), to deal in
8
+ the Software without restriction, including without limitation the rights to
9
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
+ of the Software, and to permit persons to whom the Software is furnished to do
11
+ so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,51 @@
1
+ [![Build Status](https://secure.travis-ci.org/plus3x/nmax.png?branch=master)](http://travis-ci.org/plus3x/nmax)
2
+
3
+ # NMax #
4
+
5
+ ## Installation ##
6
+
7
+ ```
8
+ ~ $ gem install nmax
9
+ ```
10
+
11
+ ## Usage ##
12
+
13
+ ```
14
+ ~ $ nmax --help
15
+ Usage:
16
+ nmax [N] [OPTIONS]
17
+
18
+ Options
19
+ -n, --n [n] Count of biggest numbers
20
+ -f, --file [file] Input file
21
+ -v, --version Show version
22
+ -h, --help Show this help
23
+ ```
24
+
25
+ ```
26
+ ~ $ cat sample_data_40GB.txt | nmax 10000
27
+ 1242315355154252146241642
28
+ 152451425254542541542512
29
+ 1545421542542151425214
30
+ 4324324235545245242
31
+ ...
32
+ ```
33
+
34
+ ## Техническое задание ##
35
+
36
+ ### Напишите скрипт nmax, который делает следующее: ###
37
+ + читает из входящего потока текстовые данные;
38
+ + по завершении ввода выводит n самых больших целых чисел, встретившихся в полученных текстовых данных.
39
+
40
+ ### Дополнительные указания: ###
41
+ + числом считается любая непрерывная последовательность цифр в тексте;
42
+ + известно, что чисел длиннее 1000 цифр во входных данных нет;
43
+ + число n должно быть единственным параметром скрипта;
44
+ + код должен быть покрыт тестами;
45
+ + код должен быть оформлен в виде гема (содержащего исполняемый файл, код модулей и т.д.);
46
+ + плюсом является размещение на Github и интеграция с Travis CI.
47
+
48
+ ### Пример запуска: ###
49
+ ```
50
+ $ cat sample_data_40GB.txt | nmax 10000
51
+ ```
@@ -0,0 +1,5 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new :default do |test|
4
+ test.pattern = 'test/**/*_test.rb'
5
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH << './lib'
3
+
4
+ require 'nmax'
5
+
6
+ puts NMax.get(stdin: STDIN, count: ARGV[0].to_i)
@@ -0,0 +1,36 @@
1
+ module NMax
2
+ autoload :VERSION, 'nmax/version'
3
+
4
+ def self.get(stdin:, count:)
5
+ chars = stdin.each_char
6
+ grouped_numbers = chars.chunk { |c| c =~ /\d/ }
7
+
8
+ numbers = grouped_numbers.inject([]) do |memo, (_, digits)|
9
+ memo << digits.join.to_i
10
+ memo = memo.sort.last(count) if memo.size > count
11
+ memo
12
+ end
13
+
14
+ numbers.sort.last(count)
15
+ end
16
+
17
+ # Not beautiful, but faster
18
+ # def self.get(stdin:, count:)
19
+ # number_str = ''
20
+ # numbers = []
21
+
22
+ # stdin.each_char do |c|
23
+ # if c =~ /\d/
24
+ # number_str << c
25
+ # elsif !number_str.empty?
26
+ # numbers << number_str.to_i
27
+ # numbers = numbers.sort.last(count) if numbers.size > count
28
+ # number_str.clear
29
+ # end
30
+ # end
31
+
32
+ # numbers << number_str.to_i unless number_str.empty?
33
+
34
+ # numbers.sort.last(count)
35
+ # end
36
+ end
@@ -0,0 +1,3 @@
1
+ module NMax
2
+ VERSION = '0.1'.freeze
3
+ end
@@ -0,0 +1,20 @@
1
+ require './lib/nmax/version'
2
+
3
+ Gem::Specification.new 'nmax', NMax::VERSION do |s|
4
+ s.homepage = 'http://rubygems.org/gems/nmax'
5
+ s.date = '2016-03-28'
6
+ s.summary = 'NMax'
7
+ s.description = 'NMax print max numbers from huge enterence input'
8
+ s.author = 'Vladislav Petrov'
9
+ s.email = 'electronicchest@gmail.com'
10
+ s.license = 'MIT'
11
+ s.platform = Gem::Platform::RUBY
12
+ s.post_install_message = 'Thanks for installing!'
13
+
14
+ s.required_ruby_version = '>= 2.1.2'
15
+
16
+ s.files = `git ls-files`.split
17
+ s.test_files = `git ls-files -- {test,spec,features}`.split
18
+ s.executables = `git ls-files -- bin`.split.map { |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
+ end
@@ -0,0 +1,3 @@
1
+ some text with number 12 and greater number then 22
2
+ 11 addition number
3
+ 30
@@ -0,0 +1,24 @@
1
+ require_relative 'test_helper'
2
+ require 'tempfile'
3
+
4
+ describe NMax do
5
+ it 'should get 2 max numbers from readed file' do
6
+ assert_equal `cat #{Bundler.root}/test/fixtures/text_with_numbers.txt | #{Bundler.root}/bin/nmax 2}`, "22\n30\n"
7
+ end
8
+
9
+ def memstats
10
+ `ps -o rss= #{Process.pid}`.strip.to_i
11
+ end
12
+
13
+ it 'should not load file to process memory' do
14
+ file = Tempfile.new('tmp')
15
+ file.write(('0'..'z').to_a.*(50_000).sample(1_000_000).join)
16
+ file.rewind
17
+
18
+ starting_memory = memstats
19
+
20
+ NMax.get(stdin: file, count: 5)
21
+
22
+ assert memstats < starting_memory + 6000, 'Calculation has consumed more than 6MB'
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'minitest/autorun'
4
+ require 'bundler'
5
+ require 'nmax'
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nmax
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Vladislav Petrov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: NMax print max numbers from huge enterence input
14
+ email: electronicchest@gmail.com
15
+ executables:
16
+ - nmax
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rubocop.yml"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - MIT-LICENSE.md
25
+ - README.md
26
+ - Rakefile
27
+ - bin/nmax
28
+ - lib/nmax.rb
29
+ - lib/nmax/version.rb
30
+ - nmax.gemspec
31
+ - test/fixtures/text_with_numbers.txt
32
+ - test/nmax_test.rb
33
+ - test/test_helper.rb
34
+ homepage: http://rubygems.org/gems/nmax
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message: Thanks for installing!
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.1.2
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.5.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: NMax
58
+ test_files:
59
+ - test/fixtures/text_with_numbers.txt
60
+ - test/nmax_test.rb
61
+ - test/test_helper.rb