Re: Ruby Quiz - Challenge #8 - Base32 Alphabet - Convert the Super "Sekretoooo" 240-Bit CryptoKitties Genome to Kai Notation - Annipurrsary!

"Frank J. Cameron" <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
Gerald Bauer wrote:
> Challenge #8 - Base32 Alphabet - Convert the Super "Sekretoooo"
> 240-Bit CryptoKitties Genome to Kai Notation - Annipurrsary!

$ ruby -v lib/008.rb
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Run options: --seed 55765
# Running:
..
Finished in 0.001806s, 1107.3523 runs/s, 1107.3523 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

$ cat lib/008.rb
require_relative '../008/test.rb'

class RubyQuizTest
  Encode = [(1..9),('a'..'k'),('l'..'x')].map(&:to_a).inject(&:+)

  # benchmark
  # 5_000.times: 0.462325   0.001169   0.463494 (  0.504119)
  # 50000.times: 4.584615   0.003136   4.587751 (  4.840367)
  # def kai_encode(num)
  #   ('%0240b' % num)
  #   .chars
  #   .each_slice(5)
  #   .map(&:join)
  #   .map{|n| n.to_i(2)}
  #   .map{|n| Encode[n]}
  #   .join
  # end

  # benchmark
  # 5_000.times: 0.148790   0.000000   0.148790 (  0.151831)
  # 50000.times: 1.490005   0.000987   1.490992 (  1.581884)
  def kai_encode(num)
    num
    .to_s(2)
    .rjust(240, '0')
    .yield_self{|bs| (0..235).step(5).map{|i| bs[i,5]}}
    .map{|n| Encode[n.to_i(2)]}
    .join
  end

  def kai_fmt(kai)
    kai
    .chars
    .each_slice(4)
    .map(&:join)
    .join(' ')
  end
end

RubyQuizTest.new('fjc')


Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.