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 +4 -4
- data/.gitignore +4 -1
- data/.travis.yml +10 -0
- data/Gemfile +5 -0
- data/README.md +24 -1
- data/lib/nmax.rb +32 -19
- data/{nmax.gemspec → nmax_cli.gemspec} +1 -1
- data/test/test_nmax.rb +35 -7
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65ce5c5fda602899dff55ad51ddbfdad53d52ea39118ca285503f310383f46f0
|
4
|
+
data.tar.gz: 62c142ca5cd4be2aba6b7e61ab86d4bb5b04d89174aa08d88f24e687d49ec6eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86e86f33631b217c87747c7731b106e5b7cbdcede5580f897fab6d10cd181dc035bfd18097bf532e5a73ff0afce4fc83fce80523740103b39b5fc2290d8ea4e7
|
7
|
+
data.tar.gz: af6f994a68552c8c225ff3ca9d10d32004783220046de1d469f54656e1a83966cbfe8af9bc1524f1b623696f43107cd40ebb0a4cc50f8359b21b0fc694219ff3
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
ADDED
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
|
-
|
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
|
|
data/lib/nmax.rb
CHANGED
@@ -1,51 +1,64 @@
|
|
1
1
|
module Nmax
|
2
2
|
class << self
|
3
3
|
def run
|
4
|
-
|
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
|
-
|
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
|
-
|
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 ||
|
30
|
+
ARGV.size != 1 || argument_not_number?
|
24
31
|
end
|
25
32
|
|
26
|
-
def
|
33
|
+
def argument_not_number?
|
27
34
|
!ARGV[0].match?(/^\d+$/)
|
28
35
|
end
|
29
36
|
|
30
|
-
def
|
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
|
45
|
+
def add_nums(nums)
|
35
46
|
return if nums.empty?
|
36
47
|
|
37
|
-
nums.each { |n|
|
48
|
+
nums.each { |n| add_number(n) }
|
38
49
|
end
|
39
50
|
|
40
|
-
def
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
48
|
-
|
60
|
+
def fix_overflow
|
61
|
+
numbers.pop if numbers.size > size
|
49
62
|
end
|
50
63
|
end
|
51
64
|
end
|
data/test/test_nmax.rb
CHANGED
@@ -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
|
-
|
7
|
-
strings
|
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(
|
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 = [
|
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.
|
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
|
-
-
|
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.
|
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
|