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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 520fb603e3b6ce1e2db7f002b3653a7556cdb2dc
4
- data.tar.gz: 0b5d6a577e4d288ff722966841a2736c57b9b58e
3
+ metadata.gz: ced1861af1688fc2dc47ada03ce710b3a32b8e38
4
+ data.tar.gz: 1fbde7a4e63adbf1879a13a979206c584b66ebae
5
5
  SHA512:
6
- metadata.gz: 5abd1796e60de18b77dd06a52637c15e019fcd075c7ebd54f43bf62a5b43f499a99d12216ffcbf54266e64ea1aead09f3a342edbed6e1643751a1491601a294b
7
- data.tar.gz: 637da4595aafd9734005fc5f78c7e8f04b763c36b29db82df69c479b0f94489c0d4ccd43cf0d51eb89b78f8057e5191963097da538c2b58f137d4b538c9eea9a
6
+ metadata.gz: e49c299ab22bc0479e1edb51c2714c61eb7a4448f224ed7fc25c82971c7a9bba6664f688899abb0d1c450cbec39b9fef731aac1c6a435cc00d47cf96da0fcdb8
7
+ data.tar.gz: d0e3524542be2bc818063204f2ebefada2b0b9fcc771d956691e5ebb8e9a34232c9762bc77708fc3ceed2c244b8bc8e234292b9f92059b05a332bb5f757f6bcf
data/bin/nummax CHANGED
@@ -1,5 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
-
3
2
  require 'nummax'
4
3
 
5
- NumMax.test
4
+ begin
5
+ puts NumMax.run(STDIN, ARGV[0])
6
+ rescue => ex
7
+ abort("Error: #{ex.message}")
8
+ end
@@ -1,7 +1,21 @@
1
- class NumMax
2
- public
3
- def self.test
4
- puts "Hello world!"
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
@@ -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
@@ -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.1
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-04 00:00:00.000000000 Z
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