nmax_cli 0.0.0 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e6accd6271507af5f799533ff9534c8347aa63b5c152cb053bafa96ea2f3a5a
4
- data.tar.gz: f4a36cdb53679dd8aa7abba00021d7638ed6482625994d4f52cd8a83b37d774c
3
+ metadata.gz: 65ce5c5fda602899dff55ad51ddbfdad53d52ea39118ca285503f310383f46f0
4
+ data.tar.gz: 62c142ca5cd4be2aba6b7e61ab86d4bb5b04d89174aa08d88f24e687d49ec6eb
5
5
  SHA512:
6
- metadata.gz: 7802233ca87d09266126a69043b6954e6d38037d64fac10f2838a4f10c0d617357fe020e7caf5251bae6b69f43a2d943fb28cbaa1d92f3b179b8674f74488770
7
- data.tar.gz: 182b2bb39ebe771bee286905268f20a5f1b00e4b97d6c4a6d21d1c3dd4060eb93ebd4f368304485513164f80b6f9f60e825d24649648d48ad144e0ada13891fd
6
+ metadata.gz: 86e86f33631b217c87747c7731b106e5b7cbdcede5580f897fab6d10cd181dc035bfd18097bf532e5a73ff0afce4fc83fce80523740103b39b5fc2290d8ea4e7
7
+ data.tar.gz: af6f994a68552c8c225ff3ca9d10d32004783220046de1d469f54656e1a83966cbfe8af9bc1524f1b623696f43107cd40ebb0a4cc50f8359b21b0fc694219ff3
data/.gitignore CHANGED
@@ -1,2 +1,5 @@
1
1
  /test/sample_files
2
- *.gem
2
+ *.gem
3
+ /coverage
4
+ Gemfile.lock
5
+ .bundle/
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ dist: trusty
3
+
4
+ language: ruby
5
+ deploy:
6
+ provider: rubygems
7
+ api_key: $RUBYGEMS_API_KEY
8
+ gem: nmax_cli
9
+ on: deploy-gem-release
10
+ skip_cleanup: true
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'coveralls', require: false
4
+ gem 'minitest'
5
+ gem 'rake'
data/README.md CHANGED
@@ -1,7 +1,30 @@
1
+ [![Build Status](https://travis-ci.org/neverovda/nmax.svg?branch=master)](https://travis-ci.org/neverovda/nmax)
2
+ [![Coverage Status](https://coveralls.io/repos/github/neverovda/nmax/badge.svg?branch=master)](https://coveralls.io/github/neverovda/nmax?branch=master)
3
+
1
4
  # nmax
2
5
  Output N maximum numbers from input stream
3
6
 
4
- скрипт nmax, делает следующее:
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem ' nmax_cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install nmax_cli
22
+
23
+ ## Example
24
+
25
+ cat sample_data_40GB.txt | nmax 10000
26
+
27
+ ## скрипт nmax, делает следующее:
5
28
  * читает из входящего потока текстовые данные
6
29
  * по завершении ввода выводит n самых больших целых чисел, встретившихся в полученных текстовых данных
7
30
 
@@ -1,51 +1,64 @@
1
1
  module Nmax
2
2
  class << self
3
3
  def run
4
- parameter_check
4
+ warning
5
5
  return if wrong_args?
6
+ return if not_presence_stdin?
7
+
6
8
  @size = ARGV[0].to_i
7
9
  @numbers = []
10
+ strings_processing
8
11
 
9
- $stdin.each do |line|
10
- add_nums_to_result nums(line)
11
- end
12
- puts '' if @numbers.empty?
13
- puts @numbers
12
+ puts numbers
14
13
  end
15
14
 
16
15
  private
17
16
 
18
- def parameter_check
17
+ attr_reader :size
18
+ attr_accessor :numbers
19
+
20
+ def warning
21
+ warn 'Not reading from stdin' if not_presence_stdin?
19
22
  warn 'Nmax must receive one argument (number). Example: nmax 100' if wrong_args?
20
23
  end
21
24
 
25
+ def not_presence_stdin?
26
+ $stdin.tty?
27
+ end
28
+
22
29
  def wrong_args?
23
- ARGV.size != 1 || first_arg_not_number?
30
+ ARGV.size != 1 || argument_not_number?
24
31
  end
25
32
 
26
- def first_arg_not_number?
33
+ def argument_not_number?
27
34
  !ARGV[0].match?(/^\d+$/)
28
35
  end
29
36
 
30
- def nums(line)
37
+ def strings_processing
38
+ $stdin.each { |line| add_nums search_numbers(line) }
39
+ end
40
+
41
+ def search_numbers(line)
31
42
  line.scan(/\d+/).map(&:to_i)
32
43
  end
33
44
 
34
- def add_nums_to_result(nums)
45
+ def add_nums(nums)
35
46
  return if nums.empty?
36
47
 
37
- nums.each { |n| add(n) }
48
+ nums.each { |n| add_number(n) }
38
49
  end
39
50
 
40
- def add(num)
41
- insert_at = @numbers.bsearch_index { |n| num >= n }
42
- @numbers.insert(insert_at, num) if insert_at
43
- @numbers << num unless insert_at
44
- shorten
51
+ def add_number(num)
52
+ return if numbers.include?(num)
53
+
54
+ insert_at = numbers.bsearch_index { |n| num > n }
55
+ numbers.insert(insert_at, num) if insert_at
56
+ numbers << num unless insert_at
57
+ fix_overflow
45
58
  end
46
59
 
47
- def shorten
48
- @numbers.pop if @numbers.size > @size
60
+ def fix_overflow
61
+ numbers.pop if numbers.size > size
49
62
  end
50
63
  end
51
64
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'nmax_cli'
3
- s.version = '0.0.0'
3
+ s.version = '0.0.2'
4
4
  s.executables << 'nmax'
5
5
  s.date = '2020-09-22'
6
6
  s.summary = 'Nmax - Simple gem for search bigest integers from input stream'
@@ -1,22 +1,36 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
1
4
  require 'minitest/autorun'
2
5
  require_relative '../lib/nmax.rb'
3
6
 
4
7
  module TestNmaxHelper
8
+ attr_reader :out, :err
5
9
  def start(strings, argv)
6
- io = StringIO.new
7
- strings.each { |str| io.puts str}
8
- io.rewind
9
- $stdin = io
10
+ io_associate_with_terminal_device if strings.empty?
11
+ io_like_input_strings(strings) unless strings.empty?
10
12
  ARGV.replace argv
11
13
  @out, @err = capture_io do
12
14
  Nmax.run
13
15
  end
14
16
  end
17
+
18
+ private
19
+
20
+ def io_associate_with_terminal_device
21
+ $stdin = File.new('/dev/tty')
22
+ end
23
+
24
+ def io_like_input_strings(strings)
25
+ io = StringIO.new
26
+ strings.each { |str| io.puts str }
27
+ io.rewind
28
+ $stdin = io
29
+ end
15
30
  end
16
31
 
17
32
  class TestNmax < Minitest::Test
18
33
  include TestNmaxHelper
19
- attr_reader :out, :err
20
34
  def setup
21
35
  @strings = ['Lorem ipsum dolor sit amet, consectetur adipiscing elit...']
22
36
  @argv = ['1']
@@ -25,7 +39,7 @@ class TestNmax < Minitest::Test
25
39
 
26
40
  def test_without_numbers
27
41
  start(@strings, @argv)
28
- assert_equal("\n", out)
42
+ assert_equal('', out)
29
43
  end
30
44
 
31
45
  def test_with_one_number
@@ -43,6 +57,15 @@ class TestNmax < Minitest::Test
43
57
  assert_equal("3000\n2000\n1000\n", out)
44
58
  end
45
59
 
60
+ def test_the_same_big_numbers
61
+ strings = ['L 3000 ipsum 4 consectetur..1000',
62
+ 'Lorem 2 3000dolor am 3',
63
+ 'tetur2000 5 2000 elit']
64
+ argv = ['3']
65
+ start(strings, argv)
66
+ assert_equal("3000\n2000\n1000\n", out)
67
+ end
68
+
46
69
  def test_arguments_empty
47
70
  argv = []
48
71
  start(@strings, argv)
@@ -56,8 +79,13 @@ class TestNmax < Minitest::Test
56
79
  end
57
80
 
58
81
  def test_many_arguments
59
- argv = ['100', 'arg']
82
+ argv = %w[100 arg]
60
83
  start(@strings, argv)
61
84
  assert_equal(@error_out, err)
62
85
  end
86
+
87
+ def test_stdin
88
+ start([], @argv)
89
+ assert_equal("Not reading from stdin\n", err)
90
+ end
63
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nmax_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitrii Neverov
@@ -20,11 +20,13 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - ".gitignore"
23
+ - ".travis.yml"
24
+ - Gemfile
23
25
  - README.md
24
26
  - Rakefile
25
27
  - bin/nmax
26
28
  - lib/nmax.rb
27
- - nmax.gemspec
29
+ - nmax_cli.gemspec
28
30
  - test/test_nmax.rb
29
31
  homepage: https://github.com/neverovda/nmax
30
32
  licenses:
@@ -45,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
47
  - !ruby/object:Gem::Version
46
48
  version: '0'
47
49
  requirements: []
48
- rubygems_version: 3.1.2
50
+ rubygems_version: 3.1.4
49
51
  signing_key:
50
52
  specification_version: 4
51
53
  summary: Nmax - Simple gem for search bigest integers from input stream