# Re: Ruby Quiz - Challenge #10 - Breeding Kitties - Mix Genes Using the Sooper-Sekret Formula in the GeneSciene CryptoKitties Blockchain Contract

(Frank J. Cameron) #1

Gerald Bauer wrote:

Challenge #10 - Breeding Kitties - Mix Genes Using the Sooper-Sekret

Formula in the GeneSciene CryptoKitties Blockchain Contract

\$ ruby -v lib/010.rb
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
Run options: --seed 48193
# Running:
.
Finished in 0.004043s, 247.3455 runs/s, 742.0366 assertions/s.

\$ cat lib/010.rb
require_relative '../010/test.rb'

class RubyQuizTest
def mixgenes(mgenes, sgenes)
# can't resist new toys
(0..).step(4).take(12).each do |i|
3.downto(1) do |j|
mgenes[i+j],mgenes[i+j-1] = mgenes[i+j-1],mgenes[i+j] if rand<0.25
sgenes[i+j],sgenes[i+j-1] = sgenes[i+j-1],sgenes[i+j] if rand<0.25
end
end

48.times.map do |i|
mutation = if i%4 == 0
[mgenes[i], sgenes[i]].sort.then do |tg|
if tg[1] - tg[0] == 1 and tg[0].even?
tg[0] / 2 + 16 if rand < (tg[0] > 23 ? 0.125 : 0.25)
end
end
end

mutation or rand < 0.5 ? mgenes[i] : sgenes[i]
end
end
end

RubyQuizTest.new('fjc')

(Gerald Bauer) #2

Hello,

Happy new year. Prosit 2019! Wow. Thanks again for keeping the
ruby quiz going.
Great to see ruby-esque loops with step, downto and times.

For reference here's the test answer from the "Programming
CryptoCollectibles w/ Ruby" book [1]:

def mixgenes( mgenes, sgenes ) ## returns babygenes
babygenes = []

# PARENT GENE SWAPPING
for i in 0.step(11,1) do ## loop from 0 to 11 # for(i = 0; i < 12; i++)
index = 4 * i # index = 4 * i
for j in 3.step(1,-1) do ## loop from 3 to 1 # for (j = 3; j > 0; j--)
if rand() < 0.25 # if random() < 0.25:
mgenes[index+j-1], mgenes[index+j] = #
swap(mGenes, index+j, index+j-1)
mgenes[index+j], mgenes[index+j-1]
end
if rand() < 0.25 # if random() < 0.25:
sgenes[index+j-1], sgenes[index+j] = #
swap(sGenes, index+j, index+j-1)
sgenes[index+j], sgenes[index+j-1]
end
end
end

# BABY GENES
for i in 0.step(47,1) do ## loop from 0 to 47 # for (i = 0; i < 48; i++):
mutation = nil # mutation = 0
# CHECK MUTATION
if i % 4 == 0 # if i % 4 == 0:
gene1 = mgenes[i] # gene1 = mGenes[i]
gene2 = sgenes[i] # gene2 = sGenes[i]
if gene1 > gene2 # if gene1 > gene2:
gene1, gene2 = gene2, gene1 # gene1,
gene2 = gene2, gene1
end
if (gene2 - gene1) == 1 && gene1.even? # if (gene2 -
gene1) == 1 and iseven(gene1):
probability = 0.25 # probability = 0.25
if gene1 > 23 # if gene1 > 23:
probability /= 2 # probability /= 2
end
if rand() < probability # if
random() < probability:
mutation = (gene1 / 2) + 16 # mutation
= (gene1 / 2) + 16
end
end
end
# GIVE BABY GENES
if mutation # if mutation:
babygenes[i] = mutation # babyGenes[i]
= mutation
else # else:
if rand() < 0.5 # if random() < 0.5:
babygenes[i] = mgenes[i] #
babyGenes[i] = mGenes[i]
else # else:
babygenes[i] = sgenes[i] #
babyGenes[i] = sGenes[i]
end
end
end

babygenes # return bagygenes

end # mixgenes

Cheers. Prost.