Posts

Header files in C

     Header files are built in toolkit which has many functions with its declaration, macro definitions that can be reused in all C programs. Features: It has many features as given below. It is a modular programming. It is easily readable. It has good maintainability. Reusability of code. Syntax: 1.       Built in header file        #include <headerfile.h>       Where ‘.h’ is header file extension.       Eg: #include <stdio.h> 2.       User Defined header file. #include “headerfile.h” List of built in header files:     <stdio.h> - It is for input and output functions.     <stdlib.h> - It is for Utility functions.     <string.h> - It is related to String manipulations.     <math.h> - It has mathematical operations....

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...