Evolutionary algorithm: Difference between revisions

→‎{{header|jq}}: gojq and jaq
(→‎{{header|jq}}: start with a random string)
(→‎{{header|jq}}: gojq and jaq)
Line 4,524:
=={{header|jq}}==
{{Works with|jq}}
 
'''Works with gojq, the Go implementation of jq(*)'''
 
'''Works with jaq, the Rust implementation of jq(*)'''
 
In this entry, the "fitness" score is based on codepoint differences;
Line 4,536 ⟶ 4,540:
< /dev/random tr -cd '0-9' | fold -w 3 | $JQ -cnr -f evolutionary-algorithm.jq
</pre>
 
(*) For gojq and jaq, leading 0s must be stripped from the input, e.g. by
`sed -e '/^0./s/0//' -e '/^0./s/0//'`.
 
<syntaxhighlight lang="jq">
# Assumption: input consists of random three-digit numbers i.e. 000 to 999
Line 4,564 ⟶ 4,572:
# Output: a mutation of . such that each character is mutated with probability $r
def mutate($r):
(set|length) as $length
# Output: a pseudo-random character from set
# $n should be a random number drawn from range(0; N) inclusive where N > set|length
| def letter($n):
($n % $(set|length)) as $i
| set[$i:$i+1];
 
. as $p
2,458

edits