brute forceな文字列の生成
[asin:B00EJ27Q0M:image:large:w200]
ブルートフォース的な解法を選択する場合、こういうコードで良いのだろうか?
Rubyで書きました。nは0以上の整数。
剰余は62で求めて、除算は63で求めるのがポイント。
Aa0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("") def brute(n) text = "" while n > 61 r = n%62 text = Aa0[r] + text n = (n-r)/63 end text = Aa0[n] + text end
小文字の英字のみでよいならこうなる。
a = "abcdefghijklmnopqrstuvwxyz".split("") def brutelower(n) text = "" while n > 25 r = n%26 text = a[r] + text n = (n-r)/27 end text = a[n] + text end