Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Letter-frequency/Java/letter-frequency-1.java
Normal file
5
Task/Letter-frequency/Java/letter-frequency-1.java
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
36
Task/Letter-frequency/Java/letter-frequency-2.java
Normal file
36
Task/Letter-frequency/Java/letter-frequency-2.java
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
public static void main(String[] args) throws IOException {
|
||||
Map<Integer, Integer> frequencies = frequencies("src/LetterFrequency.java");
|
||||
System.out.println(print(frequencies));
|
||||
}
|
||||
|
||||
static String print(Map<Integer, Integer> frequencies) {
|
||||
StringBuilder string = new StringBuilder();
|
||||
int key;
|
||||
for (Map.Entry<Integer, Integer> entry : frequencies.entrySet()) {
|
||||
key = entry.getKey();
|
||||
string.append("%,-8d".formatted(entry.getValue()));
|
||||
/* display the hexadecimal value for non-printable characters */
|
||||
if ((key >= 0 && key < 32) || key == 127) {
|
||||
string.append("%02x%n".formatted(key));
|
||||
} else {
|
||||
string.append("%s%n".formatted((char) key));
|
||||
}
|
||||
}
|
||||
return string.toString();
|
||||
}
|
||||
|
||||
static Map<Integer, Integer> frequencies(String path) throws IOException {
|
||||
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(path))) {
|
||||
/* key = character, and value = occurrences */
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
int value;
|
||||
while ((value = reader.read()) != -1) {
|
||||
if (map.containsKey(value)) {
|
||||
map.put(value, map.get(value) + 1);
|
||||
} else {
|
||||
map.put(value, 1);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
26
Task/Letter-frequency/Java/letter-frequency-3.java
Normal file
26
Task/Letter-frequency/Java/letter-frequency-3.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class LetterFreq {
|
||||
public static int[] countLetters(String filename) throws IOException{
|
||||
int[] freqs = new int[26];
|
||||
BufferedReader in = new BufferedReader(new FileReader(filename));
|
||||
String line;
|
||||
while((line = in.readLine()) != null){
|
||||
line = line.toUpperCase();
|
||||
for(char ch:line.toCharArray()){
|
||||
if(Character.isLetter(ch)){
|
||||
freqs[ch - 'A']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
in.close();
|
||||
return freqs;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException{
|
||||
System.out.println(Arrays.toString(countLetters("filename.txt")));
|
||||
}
|
||||
}
|
||||
15
Task/Letter-frequency/Java/letter-frequency-4.java
Normal file
15
Task/Letter-frequency/Java/letter-frequency-4.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
public static int[] countLetters(String filename) throws IOException{
|
||||
int[] freqs = new int[26];
|
||||
try(BufferedReader in = new BufferedReader(new FileReader(filename))){
|
||||
String line;
|
||||
while((line = in.readLine()) != null){
|
||||
line = line.toUpperCase();
|
||||
for(char ch:line.toCharArray()){
|
||||
if(Character.isLetter(ch)){
|
||||
freqs[ch - 'A']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return freqs;
|
||||
}
|
||||
7
Task/Letter-frequency/Java/letter-frequency-5.java
Normal file
7
Task/Letter-frequency/Java/letter-frequency-5.java
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
public static Map<Integer, Long> countLetters(String filename) throws IOException {
|
||||
return Files.lines(Paths.get(filename))
|
||||
.flatMapToInt(String::chars)
|
||||
.filter(Character::isLetter)
|
||||
.boxed()
|
||||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue