Word Frequency Chart Problems - java

The goal of this code was to create a program using main method java to analysis a piece text which has been entered from a user.
They do this by entering the text into a scanner which is then analysed by the program. The analysis is to produce word frequency, mean length and also print out the results in a form of a asterisks chart, were a single "*" represents 1 words.
For example " Birds can maybe fly" produces this results:
Enter text:
Birds can maybe fly
Birds can maybe fly
1 letter words: 0
2 letter words: 0
3 letter words: 2
4 letter words: 0
5 letter words: 2
mean lenght: 4.0
1 letter words
2 letter words
3 letter words **
4 letter words
5 letter words **
So far I've completed the word frequency and the word mean, but the part I'm stuck on is creating the asterisks chart. This is an area in which I've never touch upon and was wondering how I would go about it, would I use a histogram or just use my int and then print out a "*" instead of a number?. I'm not expecting anyone to just hand me code, but if someone could give me a hint in what I should do or just point me the right direction or maybe just give me an explanation in what I should do, it would be greatly appreciated.
Code:
import java.util.Scanner;
public class Freq
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
while (true)
{
System.out.println("Enter text: ");
String s;
s = scan.nextLine();
String input = s;
String strippedInput = input.replaceAll("\\W", " ");
System.out.println("" + strippedInput);
String[] strings = strippedInput.split(" ");
int[] counts = new int[6];
int total = 0;
for (String str : strings)
if (str.length() < counts.length)
counts[str.length()] += 1;
for (String s1 : strings)
total += s1.length();
for (int i = 1; i < counts.length; i++)
System.out.println(i + " letter words: " + counts[i]);
System.out.println(("mean lenght: ") + ((double) total / strings.length));
}
}
}

Commons Lang StringUtils.repeat()
Usage:
String str = "*";
int n = 2;
String repeated = StringUtils.repeat(str, n);
repeated will be: **

You could just loop over counts and for each cell print a number of asterisks equal to the number stored in it:
for (int i = 0; i < counts.length; ++i) {
StringBuilder sb = new StringBuilder(i).append(" letter words ");
for (int j = 0; j <= counts[i]; ++j) {
sb.append('*');
}
System.out.println(sb);
}

Related

Displaying a pyramid

I have this task to display a pyramid as follows:
I wrote the code but unfortunately, the digits are displayed with spaces and order.
public class DisplayPyramid {
//main method declaration. Program entry point which begins its execution
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
//Prompt the user to enter an integer (number of lines)
System.out.print("Enter the number of lines: ");
int numbers = input.nextInt(); //Read value from keyboard and assign it to numberOfLines
String padding = " ";
//Display pyramid
//for loop to produce each row
for (int rows = 0; rows < numbers ; rows++) {
for (int k = numbers - rows; k >= 1; k--){
System.out.print(k + " ");
}
for (int l = 2; l <= numbers - rows; l++){
System.out.print(" " + l);
}
//Advance to the next line at the end of each rows
System.out.print("\n");
}
} }
And this is my output:
Can you help me figure out what is wrong with code ?
Anyone's help will be much appreciated.
Consider the 1st pass of the outer loop which produces
If we color hint your code, which the first inner loop in red, the second inner loop in green
This will be their corresponding output for each pass
The last pass of the red loop print "1 " and the first pass of green loop print " 2". They combine and become "1 2", which has 2 spaces in between.
The solution as Osama A.R point out, just reverse the printing order of number and space for the green loop and make if follow the red loop pattern. That will make the sequence neat.
Your second for loop prints the spaces first and then the number, however, the spaces have already been added by the first for loop, so just update the second for loop to print the spaces after printing the number.
E.g. your second loop should be like this:
for (int l = 2; l <= numbers - rows; l++){
System.out.print(l + " ");
}

Array rotation TLE (Time Limit Exceeded)

I am really confused why my java code is not working it is giving TLE on Code Monks on Hacker Earth.
Here is the link to the 1
Link to Question
the first question MONK AND ROTATION
import java.util.Scanner;
class TestClass {
static int[] ar=new int[100001];
public static void main(String args[] ){
Scanner in=new Scanner(System.in);
byte t=in.nextByte();
while(t-->0){
int n=in.nextInt();
int k=in.nextInt()%n;
for(int i=0;i<n-k;i++)
ar[i]=in.nextInt();
for(int i=0;i<k;i++)
System.out.print(in.nextInt()+" ");
for(int i=0;i<n-k;i++)
System.out.print(ar[i]+" ");
System.out.println();
}
}
}
I don't know why is it giving TLE I think there is some infinite loop going.
the question at the site is-
Monk and Rotation
Monk loves to perform different operations on arrays, and so being the principal of HackerEarth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task.
Input:
The first line will consists of one integer T denoting the number of test cases.
For each test case:
The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.
The next line consists of N space separated integers , denoting the elements of the array A.
Output:
Print the required array.
Constraints:
1<=T<=20
1<=N<=10^5
0<=K<=10^6
0<=A[i]<=10^6
Sample Input
1
5 2
1 2 3 4 5
Sample Output
4 5 1 2 3
Explanation
Here T is 1, which means one test case.
denoting the number of elements in the array and , denoting the number of steps of rotations.
The initial array is:
In first rotation, 5 will come in the first position and all other elements will move to one position ahead from their current position. Now, the resultant array will be
In second rotation, 4 will come in the first position and all other elements will move to one position ahead from their current position. Now, the resultant array will be
Time Limit: 1.0 sec(s) for each input file
Memory Limit: 256 MB
Source Limit: 1024 KB
I'm not sure about the correctness of your solution, but try to use StreamTokenizer or BufferedReader instead of Scanner. Scanner is too slow and may result in TLE when you need to read a lot of data.
Reduce the number of reads and writes from/to System.in and System.out.
Look at the following solution
Scanner scanner = new Scanner(System.in);
int noOfTestCases = scanner.nextInt();
for (int i = 0; i < noOfTestCases; i++) {
int arraySize = scanner.nextInt();
int noOfRotations = scanner.nextInt();
noOfRotations = noOfRotations % arraySize;
scanner.nextLine();
String inputString = scanner.nextLine();
String[] inputStringArray = inputString.split(" ");
StringBuffer sb = new StringBuffer();
for (int j = 0; j < arraySize; j++) {
sb.append(inputStringArray[(arraySize + j - noOfRotations) % arraySize] + " ");
}
System.out.print(sb);
System.out.println("");
}
import java.util.*;
public class temp {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
int p = 0;
int ar[] = new int[n];
for(int i=0;i<n;i++){
ar[i] = sc.nextInt();
}
k %= n;
for(int i=0;i<n;i++){
p = ar[(i+(n-k))%n];
System.out.print(p+" ");
}
System.out.println();
}
}
}
Though I have not used a big sized array in the starting, this code is working fine for all test cases.
Try this one.
Think from a different perspective. Instead of splitting the string and converting it into an array and applying the iterative logic, we can apply a different logic.
The trick is you just need to find the position of the input string where we have to split only once.
By that I mean,
input=>
6 2       //4 is the length of numbers and 2 is the index of rotation
1 2 3 4 5 6     //array (take input as a string using buffered reader)
Here, we just need to split the array string at the 2nd last space i.e. 4th space. So the output can be achieved by just splitting the string once-
5 6 1 2 3 4
first split- 5 6 + space + second split- 1 2 3 4
This logic worked for me and all the test cases passed.
Also don't forget to cover the corner case scenario when array input string is just one number.
Code Snippet-
int count=0;
for(int k=0; k<arr.length(); k++) {
if(arr.charAt(k)==' ')
count++;
if(count==size-rot) {
System.out.println(arr.substring(k+1,arr.length())
+ " " + arr.substring(0,k));
break;
}
}
Problem is in the System.out.print() call that is inside the for-loop. Thats a fairly heavy call and if called too many times and creates an overhead. This solution works:
//import for Scanner and other utility classes
import java.util.*;
class TestClass {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
int noOfTests = Integer.parseInt(input);
for (int t = 0; t < noOfTests; t++) {
input = s.nextLine();
String[] str = input.split(" ");
int sizeOfArray = Integer.parseInt(str[0]);
int noOfRotations = Integer.parseInt(str[1]);
String strIntegerArray = s.nextLine();
String[] array = strIntegerArray.split(" ");
printRightRotatedArray(array, noOfRotations, strIntegerArray.length());
}
}
static void printRightRotatedArray(String[] array, int noOfRotations, int lengthOfStr) {
int len = array.length;
int noOfAcutalRotations = noOfRotations % len;
int startingIndex = len - noOfAcutalRotations;
StringBuilder sb = new StringBuilder(lengthOfStr+1);
for (int i = 0; i < len; i++) {
sb.append(array[(startingIndex + i) % len]);
sb.append(" ");
}
System.out.println(sb);
}
}
putting all into string buffer and print at the end worked for me.
class TestClass {
public static void main(String args[] ) throws Exception {
//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();// output
for(int i=0;i<t;i++){
String [] nk=br.readLine().split(" ");
int n= Integer.parseInt(nk[0]);
int k=Integer.parseInt(nk[1]);
String [] a=br.readLine().split(" ");
int split=n-(k%n);
for (int j = split; j < a.length; j++) {
sb.append(a[j]);
sb.append(' ');
}
for (int j = 0 ; j < split; j++) {
sb.append(a[j]);
sb.append(' ');
}
sb.append("\n");
}
System.out.println(sb);
}
}

Updating a sum (Ex. spaces = spaces + 2) with for loop

Okay, I'm making a program that'll make vertical lines, horizontal lines, diagonals lines too! I'm kinda confused on one of my outputs that doesn't make any sense.
So my psudocode was like this:
//enter a char
//enter a number that will determine how long the line is
//define with easyreader what type of line it will be (hori, vert, diag)
//the idea of making the diag lines was this...
#
(two spaces) #
(four spaces) #
(six spaces) #
//we could use the sum spaces = spaces + 2; to keep on calculating what
//the previous spaces was
Code was:
class starter {
public static void main(String args[])
{
System.out.print("What char would you like? ");
EasyReader sym = new EasyReader();
String chars = sym.readWord();
System.out.print("How long would you like it to be? ");
int nums = sym.readInt();
System.out.print("Diag, Vert, or Hori? ");
//you want to read the __ varible, not the sym.readX()
String line = sym.readWord();
System.out.println("");
System.out.println("");
if(line.equals("Hori")){
for(int x = 0; x < nums; x++){
System.out.print(chars + " ");
}
}
else if(line.equals("Vert")){
for(int y = 0; y < nums; y++){
System.out.println(chars + " ");
}
}
else{
for(int xy = 0; xy < nums; xy++){
for(int spaces = 0; spaces < nums; spaces++){
spaces = spaces + 2;
System.out.print(spaces + " ");
System.out.println(chars);
}
}
}
}
}
At the bottom you will see a for loop called xy that will read how long the lines would be. Under that for loop would control the spaces. However, for some reason, the sum isn't updating correctly. The output is always:
2 (char)
5 (char)
8 (char)
2 (char)
5 (char)
8 (char)
...
The output should be:
2 (char)
4 (char)
8 (char)
...
EDIT **********
Since I need help now changing incriements here is an example (So I don't have to explain it a lot in comments)
Example: If user puts he wants the line as much as 5 units. With two for loops, one controlling how many spaces he wants, one controlling how many chars will print out, the output would be then 2, 4, 6, 8, 10.
In for loop statement, you say 'increase spaces by one after each iteration' (spaces++):
for(int spaces = 0; spaces < nums; spaces++){
In the body of your loop, you additionally ask to increase it by 2:
spaces = spaces + 2;
So each iteration it gets increased by 3.
By the way, there seems to be something wrong with your nested loops (if I understand the intention correctly). If the outer loop (looping over xy) draws a line on each iteration, then the inner loop, which is supposed to output an indent for the current line, must be bounded by xy (multiplied by 2) rather than nums. I'd write it like this:
for (int xy = 0; xy < nums; xy++) {
for (int spaces = 0; spaces < xy*2; spaces += 2) {
System.out.print(" ");
}
System.out.println(chars);
}
Because you are adding 3 to the spaces each time
for(int spaces = 0; spaces < nums; spaces++){
spaces = spaces + 2;
spaces++ spaces += 1
spaces = spaces + 2; spaces += 2
actually the problem is in this part :
for(int spaces = 0; spaces < nums; spaces++){
spaces = spaces + 2;
System.out.print(spaces + " ");
System.out.println(chars);
}
when the program starts we have spaces = 0
then this part is going to run spaces =spaces + 2
now spaces is equal to 2, so we have spaces = 2
the program prints 2, after that spaces increments by 1 using spaces++ part
now spaces is equal to 3, which means spaces=3
after that this line is going to be run spaces = spaces + 2
so the value of spaces becomes 5
and if we do this for ever and ever we will have this sequence of numbers :
2 5 8 11 14 ....
in fact this is because we are incrementing spaces by 3 in each iteration
if you modify your code in this form, the problem is going to be solved :
for (int xy = 0; xy < nums; xy++) {
for(int spaces = 0; spaces < xy*2; spaces += 2)
System.out.print(spaces + " ");
System.out.println(chars);
}

Counting lower case letters in a string and printing out their occurrence in the form of a histogram?

I am currently trying to write a program where the user will input a string and then the program will output the occurrence of lowercase letters as such:
"Hello world! The quick brown fox jumps over the fence."
a:
b:*
c:**
d:*
e:*****
f:**
g:
h:***
... so on until z.
I just have no idea how to go about writing this. I've looked around but no one uses arrays. I was thinking you have an array for the alphabet and then have a loop that takes each element of the string and corresponds it with a letter of the alphabet, which then adds one to the counter which wil ultimately display the histogram.
Just not sure how to go about it.
Thanks.
EDIT: Here's what I have so far. It's not much and I still don't really understand what to do. But it's something.
import java.util.Scanner;
public class CountingChars {
public static void main(String[] args) {
System.out.println("Enter the text you would like to count the characters of. Please end with a blank line.");
Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();
String alphabet = "abcdefghijklmnopqrstuvwxyz";
int[] amount = new int[alphabet.length()];
//for (int i = 0; i < userInput.length();i++){
//}
char occurrence;
int count = 0;
while(userInput.length()>0){
occurrence = userInput.charAt(0);
int i = 0;
while(i < userInput.length() && userInput.charAt(i) == occurrence){
count++;
}
}
}
}
Two basic ways of doing this which come to mind.
First is using an array of fixed length with stored ints (lower alph chars), where 'a' is on index 0. And then iterate through the given chararray updating the specific index (you can get the index by something like 'selectedChar' - 'a', which will give you the index position). Then you simply iterate through the list a print number of asterisks accordingly.
Second way is using a HashMap, where you store per each character the value, count the chars, update the value in the map accordingly and then simply go through the map and print those out (now that I am thinking about it, SortedMap will be better).
public static void printAlphabetHistogram(String input) {
int amount[] = new int[25];
for(char c : input.toCharArray()) {
if(Character.isAlphabetic(c)) {
c = Character.toLowerCase(c);
amount[c - 'a'] += 1;
}
}
for(int i = 0; i < amount.length; i++) {
System.out.printf("%s:", (char)('a' + i));
for(int j = 0; j < amount[i]; j++) {
System.out.print("*");
}
System.out.println();
}
}

Calculating the average word length

I am a java beginner with very weak understanding of java. To my already working code which already calculates the number of words in a sentence, total number of characters of a sentence and the total number of characters for each word, I would like to add another function.
I would like to add a piece of code which calculates the average of word length, for example if I type in 'hey hi cat dog i', the output would be 2.4. (Because the total number of characters for this sentence is 12, divided by the number of words (5 words) gives the average of 2.4).
Underneath there is my piece of code I'm working on and this is what I created based on many tutorials, but they all teach the average for numbers, not word lengths. My thinking is that my code should firstly count the sum of characters for each words (word.length) and then divide it by the sum of words (sentence.length). But it seems not to work. Can you please help me to correct this piece of code?
{
//prints out average word length
int length = wordcount / word.length ;
sum = sum + word.length / sentence length; //this counts the sum of characters of words and divides them by the number of words to calculate the average
System.out.println("The average word length is " + sum);} //outputs the sum calculated above
{
Underneath there is my full code to help you understand better what I mean. Thank you for all your help!
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in); //This adds a scaner/text window to the program.
while(true)
{ // have created infinite loop.
System.out.print("Enter your text or enter 'quit' to finish the program: ");
String sentence = in.nextLine();
if(sentence.equals("quit"))
{ // if enterd value is 'quit' than it comes out of loop
break;
}
else
{ //else if 'quit' wasn't typed it, the program displays whats underneath.
System.out.println("You have entered: "
+ sentence); // to print what the user has entered in the text window/scanner.
System.out.println("The total number of characters is " + sentence.length()
+ "."); // to print the total number of characters
System.out.println("This piece of text has " + countWords(sentence)
+ " words."); //this method counts the number of words in the entered sentence.
String[] words =
sentence.split(" "); // to get the individual words, splits them when the free space (" ") is found.
int maxWordLength = 0;
int wordLength = 0;
for(int i = 0; i < words.length; i++)
{
wordLength = words[i].length();
if(wordLength > maxWordLength)
{ //This piece of code is an array which counts the number of words with the same number of characters.
maxWordLength = wordLength;
}
}
int[] intArray = new int[maxWordLength + 1];
for(int i = 0; i < words.length; i++)
{
intArray[words[i].length()]++;
}
for(int i = 1; i < intArray.length; i++)
{
System.out.printf("There are " + "%d word(s) of length %d<\n>", intArray[i], i);
}
System.out.println("The numbers of characters for each word:"); //word.length method counts the number of characters for each word.
for(int i = 0; i < words.length; i++)
{
System.out.println(words[i] + " = " + words[i].length() + " characters");
}
}
}
}
{
//prints out average word length
int length = wordcount / world.length;
sum = sum + word.length / sentence
length; //this counts the sum of characters of words and divides them by the number of words to calculate the average
System.out.println("The average word length is " + sum);
} //outputs the sum calculated above
{
in.close();
}
private static int countWords(String str)
{ //this piece of code splits the words when the space (" ") is found and prints out the length of words.
String words[] = str.split(" ");
int count = words.length;
return count;
}
}
Use the split method.
Here is an example:
//returns the average word length of input string s
//the method is of double type since it will likely not return an integer
double avgWordLength(String s){
String delims=",;. ";//this contains all the characters that will be used to split the string (notice there is a blank space)
//now we split the string into several substrings called "tokens"
String[] tokens = s.split(delims);
int total=0;//stores the total number of characters in words;
for(int i=0; i<tokens.length(); i++){
total += tokens[i].length(); //adds the length of the word to the total
}
double avg = total/tokens.length();
return avg;
}
And there you have it.
you can try like this
String input = "hello Alpha this is bravo";
String[] strArray = input.split(" ");
float totalChars = 0;
for(String s : strArray){
totalChars += s.length();
}
float words = strArray.length;
float averageWordLength = (float)(totalChars/words);
System.out.println(averageWordLength);
You simply need to call something like:
public static double getAverageCharLength(String str) {
String words[] = str.split(" ");
int numWords = words.length;
int totalCharacters = 0;
for(int i = 0; i < numWords; i++)
totalCharacters = totalCharacters + words[i].length();
return totalCharacters/numWords;
}
I can't really tell you where you're going wrong because I can't understand the jumble that is your code. But this is the logic you should follow.
Note: This will not calculate correctly the average word length if your words contain special characters like apostrophes. I'm not sure if you need to look out for that in your case but if you do, look into regular expressions to specify what characters to ignore and using String methods like contains().
Also note that you have no method signatures for the following two methods you're trying to define:
{
//prints out average word length
int length = wordcount / world.length;
sum = sum + word.length / sentence
length; //this counts the sum of characters of words and divides them by the number of words to calculate the average
System.out.println("The average word length is " + sum);
} //outputs the sum calculated above
{
in.close();
}
Maybe try going over the Java syntax in the oracle docs if you're unsure on how to go about writing these methods correctly.

Categories

Resources