Skip to main content

Posts

Word Frequency Counter

Word Frequency Counter in Java Task Write a Java program to count the frequency of each word in a given sentence. The program should: 1. Take a sentence as input. 2. Split the sentence into individual words. 3. Count how many times each word appears. 4. Print each word along with its frequency. Example Input: the quick brown fox jumps over the lazy dog Output: the : 2 quick : 1 brown : 1 fox : 1 jumps : 1 over : 1 lazy : 1 dog : 1 --- Code import java.util.HashMap; import java.util.Map; public class WordFrequencyCounter {     public static void main(String[] args) {         // Input sentence         String sentence = "the quick brown fox jumps over the lazy dog";         // Split the sentence into words (using space as delimiter)         String[] words = sentence.split(" ");         // Create a HashMap to store words and their counts         Map<String, In...

BASICS OF ALGORITHMS

Definition of an Algorithm: An algorithm is a step-by-step procedure or a set of rules designed to solve a specific problem or accomplish a defined task in a finite amount of time. It is a well-defined computational process that takes input and produces output. *** Characteristics of an Algorithm *** An algorithm must satisfy the following characteristics: Input: The algorithm should have well-defined inputs. Inputs can be zero or more quantities. Output: The algorithm must produce at least one output. The output should be the solution to the problem. Finiteness: The algorithm must terminate after a finite number of steps. Infinite loops or unending calculations violate this characteristic. Definiteness: Each step of the algorithm must be precisely defined. Ambiguity in any step should be avoided. Effectiveness: Each operation should be basic enough to be carried out by a computing agent (such as a person or a computer) in a finite amount of time. Performan...

Operations on list in Scala, Part - 1

In Scala, a List is a fundamental data structure that represents an ordered collection of elements. Lists are immutable, which means that once a list is created, its elements cannot be changed. Here are some common operations you can perform on Scala lists: 1. Creating Lists: You can create a list in Scala using the List companion object: scala Copy code val myList: List[Int] = List(1, 2, 3, 4, 5) 2. Accessing Elements: You can access elements in a list using their indices: scala Copy code val firstElement = myList(0) // Returns 1 val secondElement = myList(1) // Returns 2 3. Concatenation: You can concatenate two lists using the ++ operator: scala Copy code val anotherList: List[Int] = List(6, 7, 8) val combinedList = myList ++ anotherList // Returns List(1, 2, 3, 4, 5, 6, 7, 8) 4. Adding Elements: You can add elements to the beginning of a list using :: (cons) operator: scala Copy code val newList = 0 :: myList // Returns List(0, 1, 2, 3, 4, 5) 5. List Operations: head : Returns ...