kaki-utils 0.0.10 → 0.1.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.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +5 -0
  3. data/lib/kaki/utils.rb +71 -0
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8b536f8c238b581ab38931bb6f5df5337490fcf0
4
- data.tar.gz: fb9c9e60229153b9d66011ba305e3f6bdf57d161
2
+ SHA256:
3
+ metadata.gz: 72bacd04bc1372b116ebe8ace9f7f0a32fdcf935c689cf29ed853f0c0e5561f0
4
+ data.tar.gz: 34cb58e25174a33c63831bf2757eb16db2633fde79d0b123ae063ebbae0598eb
5
5
  SHA512:
6
- metadata.gz: 76e5f21f17b8a75971def4a5b6b3816acb81d6eae09c4ddb4507ee3cc2b180fbb5cbb0d105d56f362736393d540d2130757471c103d4cbb6227519356b1fb81e
7
- data.tar.gz: 743da047ea8f47facabec45edbb3e664ff7cdfb3074dde7728fc1b31d5e60dd65a5e53f48ea837857195013db341ba587626f82dc35043d60f83e3ef49532d3b
6
+ metadata.gz: 7bc4cbb3ad13c05971dd09aa0d10bb2a5f538c8128bc016ee4b6f0736f926cc8da4b6ee8c56f0e7ebeed8b78634a6b982f0be792d26e7c264214d51fe8c01056
7
+ data.tar.gz: e2fedbbb7b6078f96e912a26a9dae50f36c22b756940c530cf6398de2b10e3fa516051638dd29660338f7b2f6a21200524f75d103d27882f73c8a8802b9b5bdc
data/README.md CHANGED
@@ -14,6 +14,11 @@ methods
14
14
  * Utils.delete\_empty\_folder(folder_name = './')
15
15
  * Utils.bell    (only Ubuntu)
16
16
  * Utils.factorial(n)
17
+ * Utils.permutation(a, b)
18
+ * Utils.combination(a, b)
19
+ * Utils.repeated_permutation(a, b)
20
+ * Utils.repeated_combination(a, b)
21
+ * Utils.generate_random_strings(num, string_length = nil)
17
22
 
18
23
  ## Recurring decimal
19
24
 
data/lib/kaki/utils.rb CHANGED
@@ -3,6 +3,7 @@ require 'fileutils'
3
3
  require 'fastimage'
4
4
  require "io/console"
5
5
  require 'gtk2'
6
+ require 'set'
6
7
 
7
8
  module Utils
8
9
  #画像か?
@@ -103,4 +104,74 @@ module Utils
103
104
  result
104
105
  end
105
106
  module_function :factorial
107
+
108
+
109
+ #a個の中からb個選ぶ順列の数
110
+ # @return [Integer]
111
+ def permutation(a, b)
112
+ [*1..a].last(b).inject(1, &:*)
113
+ end
114
+
115
+ #aの中からb個選ぶ組み合わせの数
116
+ # @return [Integer]
117
+ def combination(a, b)
118
+ Utils.permutation(a, b) / Utils.factorial(b)
119
+ end
120
+
121
+ #a個の中から重複を許してb個選ぶ順列の数
122
+ # @return [Integer]
123
+ def repeated_permutation(a, b)
124
+ a ** b
125
+ end
126
+
127
+ #a個の中から重複を許してb個選ぶ組み合わせの数
128
+ # @return [Integer]
129
+ def repeated_combination(a, b)
130
+ Utils.combination(a + b - 1, b)
131
+ end
132
+ module_function :permutation, :combination, :repeated_permutation, :repeated_combination
133
+
134
+
135
+ #アルファベット小文字を使った長さ string_length の文字列を、重複しないようにランダムに num 個生成する。string_length が指定されないときは最短の文字列を使う
136
+ # @return [Array]
137
+ def generate_random_strings(num, string_length = nil)
138
+ table = [*"a".."z"]
139
+ limit = [0, 26, 702, 18278, 475254, 12356630]
140
+ result = []
141
+ generate_string1 = ->(n, l) {
142
+ st = ""
143
+ l.times do
144
+ a, n = n % 26, n / 26
145
+ st = table[a] + st
146
+ end
147
+ st
148
+ }
149
+ generate_string2 = ->(n) {
150
+ idx = limit.find_index {|i| i > n}
151
+ generate_string1.(n - limit[idx - 1], idx)
152
+ }
153
+
154
+ if string_length and 26 < string_length
155
+ raise "Given length of strings too big."
156
+ end
157
+
158
+ num_table = Set.new
159
+ if string_length
160
+ n = Utils.repeated_permutation(26, string_length)
161
+ raise "Given length of strings too small." if n < num
162
+ while num_table.size < num
163
+ num_table << rand(n)
164
+ end
165
+ num_table.each {|i| result << generate_string1.(i, string_length)}
166
+ else
167
+ idx = limit.find_index {|i| i >= num}
168
+ raise "Result Array too big." unless idx
169
+ while num_table.size < num
170
+ num_table << rand(limit[idx])
171
+ end
172
+ num_table.each {|i| result << generate_string2.(i)}
173
+ end
174
+ result
175
+ end
176
+ module_function :generate_random_strings
106
177
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaki-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - obelisk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-14 00:00:00.000000000 Z
11
+ date: 2018-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gtk2
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 2.5.2
75
+ rubygems_version: 2.7.6
76
76
  signing_key:
77
77
  specification_version: 4
78
78
  summary: My trivial utilities.