Add a bunch of new languages
This commit is contained in:
parent
1e05ecd7ee
commit
2a4d27cea0
158 changed files with 1469 additions and 0 deletions
5
Task/Entropy/R/entropy.r
Normal file
5
Task/Entropy/R/entropy.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
entropy = function(s)
|
||||
{freq = prop.table(table(strsplit(s, '')[1]))
|
||||
-sum(freq * log(freq, base = 2))}
|
||||
|
||||
print(entropy("1223334444")) # 1.846439
|
||||
17
Task/Entropy/Racket/entropy.rkt
Normal file
17
Task/Entropy/Racket/entropy.rkt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
|
||||
(define (log2 x)
|
||||
(/ (log x) (log 2)))
|
||||
|
||||
(define (digits x)
|
||||
(for/list ([c (number->string x)])
|
||||
(- (char->integer c) (char->integer #\0))))
|
||||
|
||||
(define (entropy x)
|
||||
(define ds (digits x))
|
||||
(define n (length ds))
|
||||
(- (for/sum ([(d c) (in-hash (samples->hash ds))])
|
||||
(* (/ d n) (log2 (/ d n))))))
|
||||
|
||||
(entropy 1223334444)
|
||||
Loading…
Add table
Add a link
Reference in a new issue