Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Entropy-Narcissist/Java/entropy-narcissist.java
Normal file
40
Task/Entropy-Narcissist/Java/entropy-narcissist.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class EntropyNarcissist {
|
||||
|
||||
private static final String FILE_NAME = "src/EntropyNarcissist.java";
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.printf("Entropy of file \"%s\" = %.12f.%n", FILE_NAME, getEntropy(FILE_NAME));
|
||||
}
|
||||
|
||||
private static double getEntropy(String fileName) {
|
||||
Map<Character,Integer> characterCount = new HashMap<>();
|
||||
int length = 0;
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));) {
|
||||
int c = 0;
|
||||
while ( (c = reader.read()) != -1 ) {
|
||||
characterCount.merge((char) c, 1, (v1, v2) -> v1 + v2);
|
||||
length++;
|
||||
}
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
double entropy = 0;
|
||||
for ( char key : characterCount.keySet() ) {
|
||||
double fraction = (double) characterCount.get(key) / length;
|
||||
entropy -= fraction * Math.log(fraction);
|
||||
}
|
||||
|
||||
return entropy / Math.log(2);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue