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
Code in Scala:
import scala.collection.immutable.ListMap //immutable listmap cannot be updated import scala.collection.mutable.Map //mutable map can be updated object Histogram { def main(args: Array[String]): Unit = { println("Enter text: ") //Print message val string = scala.io.StdIn.readLine().toLowerCase //Take input from user and process it to lower case var word_list: List[String] = string.split("\\s+").toList //Split given string by white spaces e.g. To do => ['To', 'do'] word_list = word_list.filter(_.length > 1) //Eliminate words with character length = 1 val map:Map[String, Int] = Map() //Create a map with key as String and value as Integer for(word <- word_list) //Iterate over list of words { for(character_index <- 1 until word.length) //Iterate over each word { val key = word.charAt(character_index - 1).toString + word.charAt(character_index) //Take two characters from string and concatenate them map(key) = if(map.contains(key)) map(key) + 1 else 1 //If key exists, increment current count otherwise create entry } } val sorted_map = ListMap(map.toSeq.sortWith(_._2 > _._2):_*) //Sort map according to values in descending order print("Histogram:\t") //Display message for((key, value) <- sorted_map) //Iterate over sorted map { print(key+":"+value+"\t") //Print key:value pair } } } Code Description: Input: String Output: List of substrings with frequency count
Main Logic:
Input: This is a good thing
Execution:
Scala: Write and test a Program as elegantly as possible Given a piece of text, create...
Scala: Write 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...
Create a program in C++ that has only an integer type input called in. This program interchanges two characters’ positions based on user’s input value. The specifications are: If user enters 0, first and second characters are swapped. If user enters 1, first and last characters are swapped. If users enter 2, second and last characters are swapped. For example: The original text is “ABC”. First letter is A, second letter is B and last letter is C. If 0...
6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...
Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...
Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...
14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...
Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...
Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...
Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...
I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...