Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem description Assume that you are working for Google and you want to write

ID: 638285 • Letter: P

Question

Problem description Assume that you are working for Google and you want to write a program to analyze the trends of users' queries, e.g. what are the most frequent search terms. This program accepts input as a text file named input.txt containing search terms, each term is on a single line. Its output is a text file named output.txt which lists all distinct terms in the input file with their corresponding frequencies sorted decreasingly. Each term and its frequency are written on one line. To make the processing more efficient, in the input and output files, a search term is encoded as an unique, integral ID rather than its character sequence.

Example:

input.txt

Explanation / Answer

public class Mode {
public static int mode(final int[] n) {
int maxKey = 0;
int maxCounts = 0;

int[] counts = new int[n.length];

for (int i=0; i < n.length; i++) {
counts[n[i]]++;
if (maxCounts < counts[n[i]]) {
maxCounts = counts[n[i]];
maxKey = n[i];
}
}
return maxKey;
}

public static void main(String[] args) {
public static Integer[] readFileReturnIntegers(
String filename) {
Integer[] temp = new Integer[1000];
int i = 0;
// connect to the file
File file = new File(filename);
Scanner inputFile = null;
try {
inputFile = new Scanner(file);
}
// If file not found-error message
catch (FileNotFoundException Exception) {
System.out.println("File not found!");
}
// if connected, read file
if (inputFile != null) {
// loop through file for integers and store in array
try {
while (inputFile.hasNext()) {
try {
temp[i] = inputFile.nextInt();
i++;
} catch (InputMismatchException e) {
inputFile.next();
}
}
} finally {
inputFile.close();
}
Integer[] array = new Integer[i];
System.arraycopy(temp, 0, array, 0, i);
return array;
}
return new Integer[] {};
}
System.out.println(mode(temp));
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote