nummax 0.0.1 → 1.0.0
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.
- checksums.yaml +4 -4
- data/bin/nummax +5 -2
- data/lib/nummax.rb +19 -5
- data/lib/nummax/io.rb +22 -0
- data/test/test_nummax.rb +64 -0
- metadata +8 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ced1861af1688fc2dc47ada03ce710b3a32b8e38
|
|
4
|
+
data.tar.gz: 1fbde7a4e63adbf1879a13a979206c584b66ebae
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e49c299ab22bc0479e1edb51c2714c61eb7a4448f224ed7fc25c82971c7a9bba6664f688899abb0d1c450cbec39b9fef731aac1c6a435cc00d47cf96da0fcdb8
|
|
7
|
+
data.tar.gz: d0e3524542be2bc818063204f2ebefada2b0b9fcc771d956691e5ebb8e9a34232c9762bc77708fc3ceed2c244b8bc8e234292b9f92059b05a332bb5f757f6bcf
|
data/bin/nummax
CHANGED
data/lib/nummax.rb
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
end
|
|
1
|
+
require 'nummax/io'
|
|
2
|
+
require 'set'
|
|
3
|
+
|
|
4
|
+
module NumMax
|
|
6
5
|
|
|
6
|
+
def self.run(input, count)
|
|
7
|
+
if input.tty? then raise("Input stream should not be associated with a terminal device") end
|
|
8
|
+
|
|
9
|
+
count = Integer(count) rescue raise("As a parameter required positive integer")
|
|
10
|
+
unless count > 0 then raise("As a parameter required positive integer") end
|
|
11
|
+
|
|
12
|
+
set = SortedSet.new
|
|
13
|
+
input.each_int do |i|
|
|
14
|
+
set << i
|
|
15
|
+
set.delete(set.first) if (set.length > count)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
set.to_a
|
|
19
|
+
end
|
|
20
|
+
|
|
7
21
|
end
|
data/lib/nummax/io.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class IO
|
|
2
|
+
|
|
3
|
+
def geti
|
|
4
|
+
value = ""
|
|
5
|
+
while char = getc() do
|
|
6
|
+
if char =~ /\d/
|
|
7
|
+
value << char
|
|
8
|
+
elsif !value.empty?
|
|
9
|
+
ungetc(char)
|
|
10
|
+
break
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
value.empty? ? nil : value.to_i
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def each_int
|
|
17
|
+
while integer = geti() do
|
|
18
|
+
yield integer
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
data/test/test_nummax.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require 'nummax'
|
|
3
|
+
|
|
4
|
+
class NumMaxTest < Minitest::Test
|
|
5
|
+
|
|
6
|
+
def test_geti
|
|
7
|
+
file = File.new("#{Dir.pwd}/test/test_files/text")
|
|
8
|
+
|
|
9
|
+
assert_equal(42, file.geti)
|
|
10
|
+
assert_equal("V", file.getc)
|
|
11
|
+
assert_equal(1742, file.geti)
|
|
12
|
+
assert_equal(4, file.geti)
|
|
13
|
+
assert_equal(8, file.geti)
|
|
14
|
+
assert_equal(1936, file.geti)
|
|
15
|
+
file.getc
|
|
16
|
+
file.getc
|
|
17
|
+
assert_equal(937, file.geti)
|
|
18
|
+
|
|
19
|
+
file.close
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_each_int
|
|
23
|
+
file = File.new("#{Dir.pwd}/test/test_files/text")
|
|
24
|
+
result = [42, 1742, 4, 8, 1936, 1937, 1958, 98, 20, 1954, 1955, 1958, 76]
|
|
25
|
+
numbers = []
|
|
26
|
+
file.each_int { |n| numbers << n }
|
|
27
|
+
|
|
28
|
+
assert_equal(result, numbers)
|
|
29
|
+
assert_equal(nil, file.geti)
|
|
30
|
+
|
|
31
|
+
file.close
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def test_nummax_text
|
|
35
|
+
file = File.new("#{Dir.pwd}/test/test_files/text")
|
|
36
|
+
result = [98, 1742, 1936, 1937, 1954, 1955, 1958]
|
|
37
|
+
|
|
38
|
+
assert_equal(result, NumMax.run(file, "7"))
|
|
39
|
+
assert_raises { NumMax.run(STDIN, 12) }
|
|
40
|
+
assert_raises { NumMax.run(file, "12num") }
|
|
41
|
+
assert_raises { NumMax.run(file, "0") }
|
|
42
|
+
|
|
43
|
+
file.close
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_nummax_number
|
|
47
|
+
file = File.new("#{Dir.pwd}/test/test_files/number")
|
|
48
|
+
result = [999991, 999992, 999993, 999994]
|
|
49
|
+
|
|
50
|
+
assert_equal(result, NumMax.run(file, 4))
|
|
51
|
+
|
|
52
|
+
file.pos = 0
|
|
53
|
+
assert_equal(56, NumMax.run(file, 100000).size)
|
|
54
|
+
|
|
55
|
+
file.close
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_terminal
|
|
59
|
+
command = "cat #{Dir.pwd}/test/test_files/number | #{Dir.pwd}/bin/nummax 3"
|
|
60
|
+
result = "999992\n999993\n999994\n"
|
|
61
|
+
assert_equal(`#{command}`, result)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
metadata
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: nummax
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Lunacorp
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-10-
|
|
11
|
+
date: 2016-10-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
|
-
description:
|
|
13
|
+
description: 'Outputs n the largest integer numbers of input stream.Example run: cat
|
|
14
|
+
file.txt | nummax 500'
|
|
14
15
|
email: Lunacorp@mail.ru
|
|
15
16
|
executables:
|
|
16
17
|
- nummax
|
|
@@ -19,6 +20,8 @@ extra_rdoc_files: []
|
|
|
19
20
|
files:
|
|
20
21
|
- bin/nummax
|
|
21
22
|
- lib/nummax.rb
|
|
23
|
+
- lib/nummax/io.rb
|
|
24
|
+
- test/test_nummax.rb
|
|
22
25
|
homepage: http://rubygems.org/gems/nummax
|
|
23
26
|
licenses:
|
|
24
27
|
- MIT
|
|
@@ -43,4 +46,5 @@ rubygems_version: 2.5.1
|
|
|
43
46
|
signing_key:
|
|
44
47
|
specification_version: 4
|
|
45
48
|
summary: NumMax
|
|
46
|
-
test_files:
|
|
49
|
+
test_files:
|
|
50
|
+
- test/test_nummax.rb
|