Sorted Doubly Linked List A sorted doubly linked list is a type of doubly linked list in which elements are arranged in a specific order, typically ascending or descending based on the data values. Where, insertion operation makes sure that the new node is placed in its correct sorted position. A doubly linked list is a two-way linked list in which a node is connected with two pointers, i.e., next and previous pointers, which are references to the next node and previous node, respectively. Characteristics of Sorted Doubly Linked List The following are the characteristics of the doubly linked ... Read More
In C++, both virtual functions and runtime polymorphism are key features that enable dynamic behavior and code flexibility. Virtual Functions A virtual function is the member function that is declared in the base class using the keyword virtual and is overridden in the derived class. The virtual function enables runtime polymorphism means the function that will be executed is determined at runtime rather than at compile time. Characteristics of Virtual Function Following are the characteristics of the virtual function: Virtual function make sure the correct function is called for an object, regardless of the type ... Read More
A singly linked list is a type of linear data structure where each node contains two items: The data and a link to the next node in the list. Where, first node is called as head node and the last node is called the tail. So, you can traverse the linked list from the head node and continue until the link becomes null. Sorted Singly Linked List A singly linked list is supposed to be sorted when its elements are arranged in specific order (i.e., ascending or descending order), and each node contains data and a pointer (or reference) to ... Read More
Here, we will see how to use Wagner and Fisher algorithm for string matching in C++. Using this algorithm, we can find how many minimum changes are required to match those strings. Wagner Fisher Algorithm Wagner Fisher is a dynamic programming algorithm that is used to find the minimum edit distance or levenshtein distance between two input strings. Levenshtein distance between two strings means the number of changes (ie, insertion, deletion or updation) required to convert one string into another. In simpler words, this algorithm will calculate a minimum number of changes required to convert ... Read More
A Binary Search Tree (BST) is a sorted binary tree in which each node follows two key properties: The right subtree of a node contains only keys greater than the node's key. The left subtree of a node contains only keys less than the node's key. Additionally, each node has at most two children. Tree Rotation Tree rotation is an operation that changes the structure without interfering with the order of the elements on a binary tree. It moves one node up in the tree and one node down. ... Read More
In Python, an IndexError occurs when you try to access a position (index) in a list, tuple, or similar collection that isn't there (does not exist). It means your program is trying to access the elements that are not available in the sequence (object). Using try-except to Catch IndexError You can use a try-except block to catch (handle) an IndexError and stop your program from crashing if you try to access an index that doesn't exist (or invalid). Example In this example, we try to access the 5th index of a list that only has 3 elements, which causes an ... Read More
In Python, an IOError (or OSError in latest versions) occurs when an input/output operation fails. For example, when we are trying to read a file that doesn’t exist, writing to a file that is read-only, or accessing a corrupted device. You can catch IOError using a try-except block to handle file input/output errors in Python. For compatibility with Python 3, we need to use OSError or catch both using a tuple. Using try-except Block You can catch IOError using a try-except block. This helps you handle file-related errors without crashing the program. Example In this example, we try to open ... Read More
In Python, you can check whether a substring exists within another string using Python in operator, or string methods like find(), index(), and __contains__(). A string in Python is a sequence of characters that is enclosed in quotes. You can use either single quotes '...' or double quotes "..." to write a string, like this - "Hello" //double quotes 'Python' //single quote A substring in Python simply means a part of a string. For example, text = "Python" part = "tho" Here, "tho" is a substring of the string "Python". Using the in operator We can check ... Read More
In Python, exception names usually end with "Error" (like ZeroDivisionError, NameError, and TypeError). This clearly shows that they are related to problems that happen while the program is running. Using this naming style makes error messages easier to read, helps with debugging. Why exceptions end with "Error" An exception is a kind of (run-time) error. Having the word "Error" in the name of the exception may help us realize that there is an issue when we encounter an exception. It follows a logical naming convention similar to other programming languages like Java and C++, that also use names ending in ... Read More
To pass a variable to an exception in Python, provide the variable as an argument when raising the exception. For custom exceptions, store the variable in an attribute. You can pass variables like strings or numbers directly into built-in exceptions to include dynamic data in the error message. Example: Passing a variable to a ValueError In this example, we are passing a variable containing an invalid input message to a ValueError - value = "abc123" try: raise ValueError(f"Invalid input: {value}") except ValueError as e: print("Caught exception:", e) We get the following output - ... Read More