Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
18
Task/Entropy/EchoLisp/entropy-1.echolisp
Normal file
18
Task/Entropy/EchoLisp/entropy-1.echolisp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(lib 'hash)
|
||||
;; counter: hash-table[key]++
|
||||
(define (count++ ht k )
|
||||
(hash-set ht k (1+ (hash-ref! ht k 0))))
|
||||
|
||||
(define (hi count n )
|
||||
(define pi (// count n))
|
||||
(- (* pi (log2 pi))))
|
||||
|
||||
;; (H [string|list]) → entropy (bits)
|
||||
(define (H info)
|
||||
(define S (if(string? info) (string->list info) info))
|
||||
(define ht (make-hash))
|
||||
(define n (length S))
|
||||
|
||||
(for ((s S)) (count++ ht s))
|
||||
(for/sum ((s (make-set S))) (hi (hash-ref ht s) n)))
|
||||
|
||||
12
Task/Entropy/EchoLisp/entropy-2.echolisp
Normal file
12
Task/Entropy/EchoLisp/entropy-2.echolisp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
;; by increasing entropy
|
||||
|
||||
(H "🔴") → 0
|
||||
(H "🔵🔴") → 1
|
||||
(H "1223334444") → 1.8464393446710154
|
||||
(H "♖♘♗♕♔♗♘♖♙♙♙♙♙♙♙♙♙") → 2.05632607578088
|
||||
(H "EchoLisp") → 3
|
||||
(H "Longtemps je me suis couché de bonne heure") → 3.860828877124944
|
||||
(H "azertyuiopmlkjhgfdsqwxcvbn") → 4.700439718141092
|
||||
(H (for/list ((i 1000)) (random 1000))) → 9.13772704467521
|
||||
(H (for/list ((i 100_000)) (random 100_000))) → 15.777516877140766
|
||||
(H (for/list ((i 1000_000)) (random 1000_000))) → 19.104028424596976
|
||||
34
Task/Entropy/FreeBASIC/entropy.freebasic
Normal file
34
Task/Entropy/FreeBASIC/entropy.freebasic
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
' version 25-06-2015
|
||||
' compile with: fbc -s console
|
||||
|
||||
Sub calc_entropy(source As String, base_ As Integer)
|
||||
|
||||
Dim As Integer i, sourcelen = Len(source), totalchar(255)
|
||||
Dim As Double prop, entropy
|
||||
|
||||
For i = 0 To sourcelen -1
|
||||
totalchar(source[i]) += 1
|
||||
Next
|
||||
|
||||
Print "Char count"
|
||||
For i = 0 To 255
|
||||
If totalchar(i) = 0 Then Continue For
|
||||
Print " "; Chr(i); Using " ######"; totalchar(i)
|
||||
prop = totalchar(i) / sourcelen
|
||||
entropy = entropy - (prop * Log (prop) / Log(base_))
|
||||
Next
|
||||
|
||||
Print : Print "The Entropy of "; Chr(34); source; Chr(34); " is"; entropy
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
calc_entropy("1223334444", 2)
|
||||
Print
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
8
Task/Entropy/Nim/entropy.nim
Normal file
8
Task/Entropy/Nim/entropy.nim
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import tables, math
|
||||
|
||||
proc entropy(s): float =
|
||||
var t = initCountTable[char]()
|
||||
for c in s: t.inc(c)
|
||||
for x in t.values: result -= x/s.len * log2(x/s.len)
|
||||
|
||||
echo entropy("1223334444")
|
||||
8
Task/Entropy/Oforth/entropy.oforth
Normal file
8
Task/Entropy/Oforth/entropy.oforth
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: entropy(s) -- f
|
||||
| freq sz |
|
||||
s size dup ifZero: [ return ] asFloat ->sz
|
||||
ListBuffer initValue(255, 0) ->freq
|
||||
s apply( #[ dup freq at 1+ freq put ] )
|
||||
0.0 freq applyIf( #[ 0 <> ], #[ sz / dup ln * - ] ) Ln2 / ;
|
||||
|
||||
entropy("1223334444") .
|
||||
10
Task/Entropy/Sidef/entropy.sidef
Normal file
10
Task/Entropy/Sidef/entropy.sidef
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
func entropy(s) {
|
||||
var counts = Hash.new;
|
||||
s.each { |c| counts{c} := 0 ++ };
|
||||
var len = s.len;
|
||||
[0, counts.values.map {|count|
|
||||
var freq = count/len; freq * freq.log2 }...
|
||||
]«-»;
|
||||
}
|
||||
|
||||
say entropy("1223334444");
|
||||
10
Task/Entropy/jq/entropy-1.jq
Normal file
10
Task/Entropy/jq/entropy-1.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Input: an array of strings.
|
||||
# Output: an object with the strings as keys, the values of which are the corresponding frequencies.
|
||||
def counter:
|
||||
reduce .[] as $item ( {}; .[$item] += 1 ) ;
|
||||
|
||||
# entropy in bits of the input string
|
||||
def entropy:
|
||||
(explode | map( [.] | implode ) | counter
|
||||
| [ .[] | . * log ] | add) as $sum
|
||||
| ((length|log) - ($sum / length)) / (2|log) ;
|
||||
1
Task/Entropy/jq/entropy-2.jq
Normal file
1
Task/Entropy/jq/entropy-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"1223334444" | entropy # => 1.8464393446710154
|
||||
Loading…
Add table
Add a link
Reference in a new issue