arknmax 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 882bb6fc940fd0d5a0d6ad08cd652fbe32fd6c56b4687b2dcf822e0c6e4d5bd4
4
+ data.tar.gz: c1505954d259c8e4d7ea9d3b03f33ed37727362a62be17c98886ee8d5029849b
5
+ SHA512:
6
+ metadata.gz: 62f20424845de651e02acbd2a74ad35d449afbc833d0f430f6d57c103c899a47dc53388bc5f882b8a125a33da35cdecb8d29c5132d860002d0b440f0d128c8aa
7
+ data.tar.gz: 4e64c7f6aaed05e6b3799aeec138d2ffa0d55b8269ad9320b4543b925924355d714c18ebff415c9e486b1e0c21832a361465162e2b324a748e10dee244c5b737
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Change log
2
+
3
+ ## master
4
+
5
+ ## 0.0.1 (2018-10-03)
6
+
7
+ - Finished first implementation
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 A.A.Abroskin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ [![Build Status](https://api.travis-ci.org/Arkweid/arknmax.svg?branch=master)](https://travis-ci.org/palkan/wsdirector)
2
+ [![Coverage Status](https://coveralls.io/repos/github/Arkweid/arknmax/badge.svg?branch=master)](https://coveralls.io/github/Arkweid/arknmax?branch=master)
3
+
4
+ # Arknmax
5
+
6
+ This is a simple realisation of nmax function
7
+
8
+ ## Installation
9
+
10
+ $ gem install arknmax
11
+
12
+ ## Usage
13
+
14
+ $ cat bigfile.txt | arknmax 10
data/bin/arknmax ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+
5
+ $:.unshift(File.expand_path(__dir__ + '/../lib'))
6
+
7
+ require 'arknmax'
8
+
9
+ begin
10
+ size_limit = ARGV[0].to_i
11
+
12
+ if size_limit > 0
13
+ Arknmax::Reader.new(size_limit).perform.print_from_max
14
+ exit 0
15
+ else
16
+ STDERR.puts 'Type positive integer as first argument.'
17
+ exit 1
18
+ end
19
+
20
+ rescue => e
21
+ STDERR.puts e.message
22
+ STDERR.puts e.backtrace.join('\n')
23
+ exit 1
24
+ end
data/lib/arknmax.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'arknmax/version'
4
+ require 'arknmax/heap'
5
+ require 'arknmax/reader'
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'algorithms'
4
+
5
+ module Arknmax
6
+ # :nodoc:
7
+ class Heap
8
+ attr_reader :data_structure, :size_limit, :io
9
+
10
+ def initialize(size_limit, io: STDOUT)
11
+ @size_limit = size_limit
12
+ @io = io
13
+ @data_structure = Containers::MinHeap.new
14
+ end
15
+
16
+ def <<(num)
17
+ data_structure.push(num)
18
+
19
+ cut_extra_size! if overlimit?
20
+ end
21
+
22
+ def print_from_max
23
+ to_a.reverse_each { |x| io.puts x }
24
+ end
25
+
26
+ def print_from_min
27
+ data_structure.size.times { io.puts data_structure.next! }
28
+ end
29
+
30
+ def to_a
31
+ array = Array.new(data_structure.size)
32
+ data_structure.size.times { |i| array[i] = data_structure.next! }
33
+ array
34
+ end
35
+
36
+ private
37
+
38
+ def overlimit?
39
+ data_structure.length > size_limit
40
+ end
41
+
42
+ def cut_extra_size!
43
+ data_structure.pop
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arknmax
4
+ # :nodoc:
5
+ class Reader
6
+ DIGIT = /\d/
7
+ DIGIT_LINE = /\d+/
8
+
9
+ attr_reader :io, :size_limit, :max_digits_line
10
+ attr_accessor :container
11
+
12
+ def initialize(size_limit, io: STDIN, max_digits_line: 1000)
13
+ @size_limit = size_limit
14
+ @io = io
15
+ @max_digits_line = max_digits_line
16
+ @container = Arknmax::Heap.new(size_limit)
17
+ end
18
+
19
+ def perform
20
+ while (line = io.read(max_digits_line))
21
+ finish_digits_line!(line) if end_with_num_char?(line)
22
+
23
+ line.scan(DIGIT_LINE) { |n| container << n.to_i }
24
+ end
25
+
26
+ container
27
+ end
28
+
29
+ private
30
+
31
+ def end_with_num_char?(string)
32
+ !(string[-1] =~ DIGIT).nil?
33
+ end
34
+
35
+ def finish_digits_line!(string)
36
+ return if string.nil?
37
+
38
+ string << io.readbyte while end_with_num_char?(string) && !io.eof?
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arknmax
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arknmax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - A.A.Abroskin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: algorithms
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.59'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.59'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: nmax function
98
+ email:
99
+ - a.a.abroskin@yandex.ru
100
+ executables:
101
+ - arknmax
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - CHANGELOG.md
106
+ - LICENSE.txt
107
+ - README.md
108
+ - bin/arknmax
109
+ - lib/arknmax.rb
110
+ - lib/arknmax/heap.rb
111
+ - lib/arknmax/reader.rb
112
+ - lib/arknmax/version.rb
113
+ homepage: https://github.com/Arkweid/arknmax
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 2.3.1
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.7.6
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: nmax function
137
+ test_files: []