Question

In: Computer Science

Scala: Write and test a Program as elegantly as possible Given a piece of text, create...

Scala: Write and test a Program as elegantly as possible

Given a piece of text, create a histogram of letter pairs (order from high to low). For instance, for the text, “this is a good thing”,

the letter pairs are: th, hi, is, is, go, oo, od, th, hi, in, and ng. (ignore a) The histogram will be:
th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1

Sample Input/Output:

Enter text: this is a good thing
Histogram: th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1
Enter text: coooooool
Histogram: oo: 6, co: 1, ol: 1

Solutions

Expert Solution

Complete scala program with description in comments:

object Histogram {

def main(args: Array[String]) {
  
//take user input
println("Enter text:")
var input = scala.io.StdIn.readLine()
  
// length of the input string
var length = input.length()
var str = ""
  
// create an object of mutable LinkedHashMap
// key is string type, value is integer type for mainitaining count
val histogram = scala.collection.mutable.LinkedHashMap[String, Int]()
  
var index = 0
  
// traverse till second last character in the input string
// why second last: because we are creating histogram of pairs.
// last pair would be second last character and last character
for(index <- 0 to (length - 2))
{
// check if current character or next character not a white space
// this check ignores whitespaces and single letter
if( !(input(index) == ' ' || input(index+1) == ' '))
{
// prepare key by appending 2 characters
str = "" + input(index) + input(index+1)
  
// check if key already exists
// if exist just increment the value by 1
if(histogram.contains(str))
{
histogram(str) = histogram(str) + 1
}
// if key doesn't exist, insert it in map with count value one
else
{
histogram(str) = 1
}
}
}
  
print("Histogram:")
// print the key value pair using map iterator foreach
histogram.keys.foreach{ key => print( " " + key + ": " + histogram(key) + ",")}
}

}

Sample run 1:

Enter text:
this is a good thing
th: 2, hi: 2, is: 2, go: 1, oo: 1, od: 1, in: 1, ng: 1,

Sample run 2:

Enter text:
coooooool
co: 1, oo: 5, oo: 1, oo: 1, ol: 1,


Related Solutions

Scala: Write and test a program as elegantly as possible You are given a puzzle like...
Scala: Write and test a program as elegantly as possible You are given a puzzle like this: 7 __ 10 __ 2 Each blank may be filled with a ‘+’ (plus) or ‘-’ (minus), producing a value k. For all combinations of plus and minus, find the value of k that is closest to 0. In the above case, there are 4 combinations, each producing a different value: 7 + 10 + 2 = 19 7 + 10 - 2...
Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
Question: In a package named "oop" create a Scala class named "Team" and a Scala object...
Question: In a package named "oop" create a Scala class named "Team" and a Scala object named "Referee". Team will have: • State values of type Int representing the strength of the team's offense and defense with a constructor to set these values. The parameters for the constructor should be offense then defense • A third state variable named "score" of type Int that is not in the constructor, is declared as a var, and is initialized to 0 Referee...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
Java. If possible please put explanations for each line of code. Write a test program that...
Java. If possible please put explanations for each line of code. Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one...
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT