Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Entropy/C/entropy-1.c
Normal file
46
Task/Entropy/C/entropy-1.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#define MAXLEN 100 //maximum string length
|
||||
|
||||
int makehist(unsigned char *S,int *hist,int len){
|
||||
int wherechar[256];
|
||||
int i,histlen;
|
||||
histlen=0;
|
||||
for(i=0;i<256;i++)wherechar[i]=-1;
|
||||
for(i=0;i<len;i++){
|
||||
if(wherechar[(int)S[i]]==-1){
|
||||
wherechar[(int)S[i]]=histlen;
|
||||
histlen++;
|
||||
}
|
||||
hist[wherechar[(int)S[i]]]++;
|
||||
}
|
||||
return histlen;
|
||||
}
|
||||
|
||||
double entropy(int *hist,int histlen,int len){
|
||||
int i;
|
||||
double H;
|
||||
H=0;
|
||||
for(i=0;i<histlen;i++){
|
||||
H-=(double)hist[i]/len*log2((double)hist[i]/len);
|
||||
}
|
||||
return H;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
unsigned char S[MAXLEN];
|
||||
int len,*hist,histlen;
|
||||
double H;
|
||||
scanf("%[^\n]",S);
|
||||
len=strlen(S);
|
||||
hist=(int*)calloc(len,sizeof(int));
|
||||
histlen=makehist(S,hist,len);
|
||||
//hist now has no order (known to the program) but that doesn't matter
|
||||
H=entropy(hist,histlen,len);
|
||||
printf("%lf\n",H);
|
||||
return 0;
|
||||
}
|
||||
6
Task/Entropy/C/entropy-2.c
Normal file
6
Task/Entropy/C/entropy-2.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ ./entropy
|
||||
1223334444
|
||||
1.846439
|
||||
$ ./entropy
|
||||
Rosetta Code is the best site in the world!
|
||||
3.646513
|
||||
Loading…
Add table
Add a link
Reference in a new issue