format_array 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +1 -0
- data/LICENSE +1 -0
- data/Manifest +6 -0
- data/README +18 -0
- data/Rakefile +2 -0
- data/format_array.gemspec +30 -0
- data/lib/format_array.rb +47 -0
- metadata +81 -0
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
v1.0. First cut
|
data/LICENSE
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
You can use this code for whatever you want.
|
data/Manifest
ADDED
data/README
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Format an array of strings with a prefix within a length bound
|
2
|
+
|
3
|
+
Given the strings
|
4
|
+
|
5
|
+
Accrington Stanley 0-2 Wycombe R
|
6
|
+
Barnet 0-1 Morecambe R
|
7
|
+
Bury 1-5 Milton Keynes Dons R
|
8
|
+
Chesterfield 1-1 Chester R
|
9
|
+
Dag & Red 0-1 Stockport R
|
10
|
+
Macclesfield 0-1 Bradford R
|
11
|
+
|
12
|
+
Combined with a prefix of "DIV2" and an output length of 76, you'd get
|
13
|
+
|
14
|
+
DIV2: Accrington Stanley 0-2 Wycombe R; Barnet 0-1 Morecambe R
|
15
|
+
DIV2: Bury 1-5 Milton Keynes Dons R; Chesterfield 1-1 Chester R
|
16
|
+
DIV2: Dag & Red 0-1 Stockport R; Macclesfield 0-1 Bradford R
|
17
|
+
|
18
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{format_array}
|
5
|
+
s.version = "1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = [""]
|
9
|
+
s.date = %q{2010-10-03}
|
10
|
+
s.description = %q{}
|
11
|
+
s.email = %q{}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/format_array.rb"]
|
13
|
+
s.files = ["CHANGELOG", "LICENSE", "README", "Rakefile", "lib/format_array.rb", "Manifest", "format_array.gemspec"]
|
14
|
+
s.homepage = %q{}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Format_array", "--main", "README"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{format_array}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/lib/format_array.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
CUTOFF=120
|
2
|
+
|
3
|
+
def format_array(a, p, j='; ')
|
4
|
+
raw = a.sort.join(j)
|
5
|
+
tmp = "#{p}: #{raw}"
|
6
|
+
|
7
|
+
# don't bother formatting if we're short
|
8
|
+
if raw.length + p.length < CUTOFF then
|
9
|
+
return [tmp]
|
10
|
+
end
|
11
|
+
|
12
|
+
# try a naive longest->shortest distribution
|
13
|
+
lines = raw.length / CUTOFF
|
14
|
+
sa = a.sort_by {|s| s.length}.reverse
|
15
|
+
out = Array.new(12) { Array.new }
|
16
|
+
while (sa.length > lines+1) do
|
17
|
+
0.upto(lines) { |i|
|
18
|
+
out[i].push sa.shift
|
19
|
+
}
|
20
|
+
end
|
21
|
+
0.upto(sa.length-1) { |i|
|
22
|
+
out[i].push sa.shift
|
23
|
+
}
|
24
|
+
strings = out.find_all {|i| i.length > 0}.map {|i| "#{p}: " << i.join(j)}
|
25
|
+
return strings
|
26
|
+
end
|
27
|
+
|
28
|
+
if nil then
|
29
|
+
test_data = <<HERE.split "\n"
|
30
|
+
Accrington Stanley 0-2 Wycombe R
|
31
|
+
Barnet 0-1 Morecambe R
|
32
|
+
Bury 1-5 Milton Keynes Dons R
|
33
|
+
Chesterfield 1-1 Chester R
|
34
|
+
Dag & Red 0-1 Stockport R
|
35
|
+
Macclesfield 0-1 Bradford R
|
36
|
+
Mansfield 2-3 Brentford R
|
37
|
+
Notts County 1-1 Grimsby R
|
38
|
+
Rochdale 0-2 Peterborough R
|
39
|
+
Rotherham 0-1 Hereford R
|
40
|
+
Shrewsbury 1-2 Lincoln City R
|
41
|
+
HERE
|
42
|
+
|
43
|
+
0.upto(5) { |i|
|
44
|
+
test = test_data[0..i]
|
45
|
+
puts format_array(test, 'DIV3').map{|k| "T#{i}: "<<k}.join("\n")
|
46
|
+
}
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: format_array
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: "1.0"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- ""
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-03 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: ""
|
22
|
+
email: ""
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- CHANGELOG
|
29
|
+
- LICENSE
|
30
|
+
- README
|
31
|
+
- lib/format_array.rb
|
32
|
+
files:
|
33
|
+
- CHANGELOG
|
34
|
+
- LICENSE
|
35
|
+
- README
|
36
|
+
- Rakefile
|
37
|
+
- lib/format_array.rb
|
38
|
+
- Manifest
|
39
|
+
- format_array.gemspec
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: ""
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --line-numbers
|
47
|
+
- --inline-source
|
48
|
+
- --title
|
49
|
+
- Format_array
|
50
|
+
- --main
|
51
|
+
- README
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 11
|
69
|
+
segments:
|
70
|
+
- 1
|
71
|
+
- 2
|
72
|
+
version: "1.2"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project: format_array
|
76
|
+
rubygems_version: 1.3.7
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: ""
|
80
|
+
test_files: []
|
81
|
+
|