Re: Ruby Quiz - Challenge #10 - Breeding Kitties - Mix Genes Using the Sooper-Sekret Formula in the GeneSciene CryptoKitties Blockchain Contract
Gerald Bauer <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAAxEZd9isHxow7k15K6=rMHnawZLab6Z1=8Mvvex9DwMfiV3xw@mail.gmail.com> |
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.
[1] https://github.com/cryptocopycats/programming-cryptocollectibles/blob/master/mixgenes.rb
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>