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)
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
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...
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT