Entropy/Narcissist: Difference between revisions

(Added XPL0 example.)
(→‎{{header|jq}}: def sum():)
 
(3 intermediate revisions by 3 users not shown)
Line 183:
{{Output}}
<pre>bytes 158 entropy 5.2802</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> DIM Freq%(255)
FOR I%=PAGE TO LOMEM Freq%(?I%)+=1 NEXT
Size=LOMEM - PAGE
FOR I%=0 TO 255
IF Freq%(I%) Entropy+=Freq%(I%) / Size * LN(Freq%(I%) / Size) / LN(2)
NEXT
PRINT "My size is ";Size " bytes and my entropy is ";-Entropy "!"
END</syntaxhighlight>
{{out}}
<pre>My size is 224 bytes and my entropy is 5.11257089!</pre>
 
=={{header|C}}==
Line 596 ⟶ 609:
{{out}}
<pre>Entropy of file "src/EntropyNarcissist.java" = 4.691381977073.</pre>
 
=={{header|jq}}==
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
 
The program assumes it will be presented to itself using
an invocation of jq with the -sR options, along the lines of:
<pre>
jq -sR -f entropy-narcissist.jq < entropy-narcissist.jq
</pre>
 
If your jq supports `keys_unsorted`, feel free to use it instead of `keys`.
<syntaxhighlight lang="jq">
def chars: explode[] | [.] | implode;
 
def bow(stream):
reduce stream as $word ({}; .[($word|tostring)] += 1);
 
def sum(s): reduce s as $x (0; .+$x);
 
length as $l
| bow(chars)
| sum(keys[] as $k | .[$k] as $c | $c * ($c|log2) )
| ($l|log2) - ./$l
</syntaxhighlight>
{{output}}
<pre>
{{output}}
<pre>
4.796499915496963
</pre>
 
=={{header|Julia}}==
Line 1,034 ⟶ 1,081:
=={{header|Wren}}==
Basically an amalgam of the code in the [[Print_itself#Wren]] and [[Entropy#Wren]] tasks.
<syntaxhighlight lang="ecmascriptwren">import "os" for Process
import "io" for File
 
2,442

edits