-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwdgen.rb
executable file
·182 lines (168 loc) · 4.03 KB
/
wdgen.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Classes and functions
def permutation a, size: nil, filter: nil, multi_times: false, verbose: false, debug: false
template = <<-EOF
def _permutation a, filter
#Just as an example
#{if multi_times
'#it is multi_times'
else
'#it is not multi_times'
end}
total = 0
matched = 0
tmp = []
ret = []
dict = Hash.new(0)
a.each {|i| dict[i] += 1}
<% (1..n).each do |i| %>
dict.each do |k, v|
<% unless multi_times %>
next if dict[k] < 1
dict[k] -= 1
<% end %>
tmp << k
<% end %>
<% if n > 0 %>
<%= 'total += 1' if verbose %>
<%= 'if tmp.join =~ /\#{filter}/' if filter %>
if block_given?
yield tmp
else
ret << tmp.clone
end
<%= 'matched += 1' if verbose %>
<% if filter and verbose %>
else
STDERR.print "\#{tmp.join} \\r"
<% end %>
<%= 'end' if filter %>
<% end %>
<% (1..n).each do |i| %>
<% unless multi_times %>
dict[k] += 1
<% end %>
tmp.delete_at(-1)
end
<% end %>
<%= 'STDERR.puts "matched: \#{matched}"' if verbose %>
<%= 'STDERR.puts "total: \#{total}"' if verbose %>
return ret unless block_given?
end
EOF
require 'erb'
n = (size.nil?) ? a.length : size
b = binding
b.local_variable_set(:n, n)
b.local_variable_set(:filter, filter)
b.local_variable_set(:multi_times, multi_times)
b.local_variable_set(:verbose, verbose)
dyn_code = ERB.new(template).result(b)
puts dyn_code.gsub(/\n\s*\n/m, "\n") if debug
eval(dyn_code)
if block_given?
_permutation(a, filter) {|i| yield i}
else
return _permutation(a, filter)
end
end
def usage
puts "Usage #{$0} [-b] [-f regexp] [-n size] [-m] [-d] [-v] [str]"
puts """
-b - Benchmark
-f - Regexp filter
-n - Word size limit ( <= size )
-v - Verbose
-d - Debug
-m - Any symbol can be used any times
-az - Symbols 'abcdefghijklmnopqrstuvwxyz'
-AZ - Symbols 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
-09 - Digits '0123456789'
Example:
#{$0} abc
#{$0} 01 -n 8 -m -f '^1{7,7}' -v
#{$0} 01 -n 20 -m -f '1{18,19}$' -v -d
#{$0} -az -m -n 4 -f 'well|done|ruby' -v
for d in $(#{$0} -09 -m -n 2 -f '(0[1-9]|[1-2][0-9]|3[0-1])'); do
for m in $(#{$0} -09 -m -n 2 -f '(0[1-9]|1[0-2])'); do
for g in $(#{$0} -09 -m -n 4 -f '19([5-9][0-9])'); do
echo $d$m$g; done; done; done
"""
exit 0
end
# Options
filter = nil
size = nil
benchmark = false
verbose = false
debug = false
multi_times = false
dict = ""
# Main
trap("SIGINT") { exit! }
if __FILE__ == $0
if ARGV.empty? or ARGV.include?("-h")
ARGV.delete("-h")
usage
end
if ARGV.include?("-v")
ARGV.delete("-v")
verbose = true
end
if ARGV.include?("-d")
ARGV.delete("-d")
debug = true
end
if ARGV.include?("-m")
ARGV.delete("-m")
multi_times = true
end
if ARGV.include?("-az")
ARGV.delete("-az")
dict << 'abcdefghijklmnopqrstuvwxyz'
end
if ARGV.include?("-AZ")
ARGV.delete("-AZ")
dict << 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
end
if ARGV.include?("-09")
ARGV.delete("-09")
dict << '0123456789'
end
if ARGV.include?("-f")
filter = ARGV[ARGV.index("-f")+1]
ARGV.delete_at(ARGV.index("-f")+1)
ARGV.delete("-f")
end
if ARGV.include?("-n")
size = (ARGV[ARGV.index("-n")+1]).to_i
ARGV.delete_at(ARGV.index("-n")+1)
ARGV.delete("-n")
end
if ARGV.include?("-b")
ARGV.delete("-b")
benchmark = true
end
if ARGV.length > 1
usage
end
if benchmark
require 'benchmark'
a = (1..10).map {|i| i}
c = 1000*1000
i = 0
Benchmark.bm do |bm|
bm.report("1kk per ") { permutation(a, filter: filter ? '' : nil) { i += 1; break if i >= c }}
end
end
dict << ARGV[0] if ARGV[0]
a = dict.split("")
permutation(a, size: size, filter: filter, multi_times: multi_times, verbose: verbose, debug: debug) { |i|
i.each { |i| print i }
puts
}
#puts "Without yield it is long feed back ..."
#arr = permutation(a)
#arr.each {|i| print i; puts}
end