Posts

Showing posts from 2025

Trie based Auto complete Program in Python

     A trie is basically a ‘try’. It is a tree type data structure. It has a root and nodes. This system provides you a brilliant way to find words depends on the user input. Generally, this input is a prefix text. How it works??? Here, a root node and some child nodes are there. Each node represents a chqracter. It is prefix searching, which finds the words according to the given prefix. Generally, it is suitable for auto complete data. Autocomplete: First, the traversal is happening to find the node with prefix value. Once found, it retrieves the data from top. Next, give the suggestion of words. Python implementation: class TrieNodeEg:     def __init__(T_self):         T_self.children = {}         T_self.is_end_of_word = False class Trie1:     def __init__(T_self):         T_self.root = TrieNodeEg() ...

Sudoku Solver Program in Python

       Sudoku is a number puzzle which makes the arrangement in particular order in a square. Logic: Let us consider 1 to 9 number. Plan a table for 9 rows and 9 columns. Each 3x3 row and column’s consider a part. So there are 9 parts. There are two things to be considered. First, each row and each column should have the number 1 to 9 at least once. But same number should not be repeated. Next, each part should have the number 1 to 9. Python implementation: def ch_valid(board1, s_row, s_col, n):     for i in range(9):         if board1[s_row][i] == n or board1[i][s_col] == n:             return False     start_row, start_col = 3 * (s_row // 3), 3 * (s_col // 3)     for i in range(3):         for j in range(3):           ...

To merge overlapping intervals in Python

       Merging means combining. Basically, it is a technique to merge overlapping intervals to non-overlapping intervals. What is an interval?               An interval is a range between two data. The data may be a time, memory and so on. Eg: [1,4] means in the range from 1 to 4. [3,5] overlaps with [1,4]. Here, 3 is in the range. What is overlapping?               When two ranges have same time. It may overlap. To avoid this, merging intervals are used. Logic: To merge the intervals, use this logic. First, sort the intervals by using their start time. For each interval, iterate it. Let us compare the current interval and last merged. If they overlap, combine them into a single unit. Else, just add the current interval as it is. Python implementation: The python program is given below. def merge_intervals...

Least Recently Used implementation using Python

       The Least Recently Used (LRU) Cache is a powerful strategy for managing limited memory by evicting the least recently accessed items. It’s widely used across systems where performance and resource optimization are critical. LRU -Least Recently Used. It is a cache which manages the less memory. Mainly, it handles the resource in optimize manner. Example: Let us consider a Web browser. It stores the recently visited pages, scripts and images. Python implementation:               It uses doubly Linked list for storing the data. The steps are given as follows… Create a c_node class and initialise three values. c_self,c_key and c_value. Make the previous and nest pointers as Null. Write the code for LRUcache. It holds the capacity value. let us create a Dummy head and tail nodes. Insert the values to the cache. Delete a value in the list. Function ‘get()’ is used to get the value. ‘put’ f...

How to find a square root of a number in python

              When a number is multiplied with same number, it is called square. Square root means the number is factorised with two same numbers. Eg: The square root of 25 is 5. (5x5 = 25). The square root is represented by √. Python implementation:               Python uses the following modules and methods to find the square root of a number. ‘exponentiation’ method    ‘math’ module     ‘cmath’ module    ‘numpy’ module. Let us briefly discuss each method. 1. ‘exponentiation’ method:       This method uses the concept ‘exponent’. >>> no = 81 >>> sqrt = no ** 0.5 >>> print("The Square root of the number is:", sqrt)       Output: The Square root of the number is: 9.0       Note : It returns float value. 2. ‘math’ module :   ...

Expression Evaluator in java

       It is a concept which deals with parsing , operator precedence, parenthesis handling.  Let us implement this in java using Stack concept. As everyone knows, Stack is a data structure with Last in First Out concept. Consider the following statement. 8 + 5 * (4 - 3) Where (4-3) -parenthesis +,*,-   deals with Operator precedence. Parsing is done using stacks for expression evaluation. Java Implementation: There are three methods(‘eval()’,’isOperator()’ and   ‘applyOp()’). The ‘eval()’ function separates value and operator separately. Based on the expression, operator precedence and parenthesis, it executes the expression. ‘isOperator()’ checks it is a operator or not. ‘applyOp()’ is for operator execution. ‘main()’ deals with creating objects for the class and call the function ‘eval()’ to get the output. Program: import java.util.*; import java.util.Stack; public class ExprEvaluator {     publi...

Custom class loader in java

       When you want to load class into the Java Virtual Machine(JVM), Custom Class Loader is used. Why Custom Class Loader? When your class has some specific needs as follows.. Isolate modules Plugins Control class Non- standard sources(Databases, Encrypted Files) Types of Class Loaders: There are 4 types of class loaders. Here is the list. Bootstrap : This Class Loader loads the core Java classes. It uses Internal JVM resources. Platform : This type uses the platform-specific classes and modules. It Loads Java module system. System : This class loader deals with application’s classes. It includes the class path. Custom : When you want to load user defined classes like plugin, this concept is used. Let us create an example…. First, the built – in package is included. Next, the function findClass() is overridden. public class MyClassLoaderEg extends ClassLoader {     @Override ...

Multithreaded File Downloader in java

               Multiple small chunks of a large file are downloaded here. this program explores the concurrency, file input output operations and how it transferred. Let us explore the steps. Steps: First, initialize the URL. The content length is retrieved. Split the download task. By the use of Fixed Thread Pool, the parallel downloads are done.   Finally, the chunks are written. Built-in Packages used for this process: ExecutorService ,HttpURLConnection and RandomAccessFile. Program implementation: import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MThreadedDownloader {     private static final int T_COUNT = 3;     private final String fURL;     private final String opFile;     public MThreadedDownloader(String fURL, String...

ORM(Object Relational Mapping) implementation in java

               A programming technique that interacts the relational database for developers. That is called ORM(Object Relational Mapping). Actually, it is like a bridge between front end and back end code. Here, front end is your code. Back end is your relational database.   Let us create a Customer database for this project. Java implementation: Part1: Customer Class creation public class Customer {     private int c_id;     private String c_name;     private String c_email;     // let us code the constructor     public Customer() {}     public Customer(int c_id, String c_name, String c_email) {         this.c_id = c_id;         this.c_name = c_name;         this.c_email = c_email;     }   ...

Java implementation of Producer - Consumer Problem using Blocking Queue

             Producer creates data and make it into a buffer. A consumer who always take the data from the buffer. Let us consider a textile manufacturer. He/she is the producer. Customer is the consumer. Problem: When the producer creates more products or no products. When a consumer doesn’t take the product. So the solution is implemented in java as Blocking Queue. This queue provides the solution for this problem. It is available in java.util.concurrent package. Let us create the code. Java implementation: // Import the built in package import java.util.concurrent.*; //class producer declaration and function ‘run()’ defintion class PCP_Producer implements Runnable {     private BlockingQueue<Integer> queue1;     public PCP_Producer(BlockingQueue<Integer> q) {         this.queue1 = q;     }     @Override ...

Data Structures -An Introduction with Basic Examples

  Let’s consider a social media application. You create an account with your basic details and receive many follow requests, which you accept. Your follower’s list is then organized like a data structure . Data structures are used to store and manage data in an organized and efficient way. Everyday Examples: Think of a bookshelf, a wardrobe, or a parking area in a mall—each one organizes different types of items systematically. Definition: A data structure is a way of organizing, processing, and retrieving data effectively. How are data structures classified? They are broadly categorized into two types: 1. Linear Data Structures Here, data is stored sequentially and accessed one by one. Array: A basic linear data structure where elements are stored in contiguous memory locations. Linked List: Composed of nodes that are connected using pointers or links. Stack: Follows the Last-In, First-Out (LIFO) principle. Queue: ...

Java implementation of MVC Pattern

A Design pattern which separates the data logic, UI (User Interface) and processing. Here, data logic deals with Model. UI is for View. The processing can be done by Controller. This design pattern is useful for Web Application Architecture. Model-View-Controller (MVC) separates data logic (Model), UI (View), and processing (Controller). The technical implementation is given below. Java implementation: This program implements four classes namely MVC_Model, MVC_View, MVC_Controller and MVCEg. ‘Class MVC_Model’ : It creates and holds the data. Member function :get_Data() Member variable : m_data. ‘Class M_View’: It displays the data to user. Member function: displayIt( String msg). where ‘msg’ is a member variable as String. ‘Class M_Controller’: This process the data. It initialises the values to above two classes. Member function: ‘updateItView()’. Here, this function gets the data from MVC_Model and use the MVC_View function displayIt(). ‘Class MVCE...