Sarika Singh has Published 137 Articles

__exit__ in Python

Sarika Singh

Sarika Singh

Updated on 13-Jul-2025 00:09:12

4K+ Views

In Python, some special methods have names that start and end with double underscores. These are called dunder methods. One such method is __exit__, which is used in context managers for cleanup tasks. Context Manager in Python Context Mangers helps to manage resources such as files, database connections, or network ... Read More

__name__ (A Special variable) in Python

Sarika Singh

Sarika Singh

Updated on 13-Jul-2025 00:07:41

3K+ Views

Python does not require a main function to start execution like many other programming languages. Instead, it uses a special built-in variable called __name__ to determine how a Python script is being executed (directly or, is it imported as a module into another script). In this article, we will learn ... Read More

__future__ Module in Python

Sarika Singh

Sarika Singh

Updated on 13-Jul-2025 00:03:01

556 Views

What is the __future__ Module? The __future__ is a built-in Python module that is used to import features from newer versions of Python into older versions. This helps you to write code that will work the same way in both old and new versions of Python. It is mainly used ... Read More

Python Program for Detect Cycle in a Directed Graph

Sarika Singh

Sarika Singh

Updated on 13-Jun-2025 18:45:15

1K+ Views

A cycle occurs when a path starts and ends at the same vertex, following the direction of the edges. In directed graphs, cycles can cause problems like infinite loops or dependency errors, so detecting them is important in areas like task scheduling and deadlock detection. We can use Depth-First Search ... Read More

Python Program for Cutting a Rod

Sarika Singh

Sarika Singh

Updated on 13-Jun-2025 18:44:18

982 Views

Rod Cutting ProblemThe Rod Cutting problem is a classic example of dynamic programming. The goal is to cut a rod into pieces to maximize the total value. Each piece has a specific length and value, and we must choose the cuts in such a way that the total value is ... Read More

Python Program for Cocktail Sort

Sarika Singh

Sarika Singh

Updated on 13-Jun-2025 18:41:55

488 Views

What is Cocktail Sort? Cocktail sort is a variation of bubble sort that sorts the array in both directions on each pass. It is also known as Bidirectional Bubble Sort or Shaker Sort. The algorithm works by traversing the list forward and backward, alternatively, pushing the largest and smallest elements ... Read More

Python Program for BogoSort or Permutation Sort

Sarika Singh

Sarika Singh

Updated on 13-Jun-2025 18:19:26

369 Views

BogoSort, also known as Permutation Sort or Stupid Sort, is a sorting algorithm based on generating random permutations of the list until it gets sorted. BogoSort continues to shuffle the array randomly until the list becomes sorted. The expected time complexity is unbounded, and its average performance is extremely poor. ... Read More

Python Program for Binary Insertion Sort

Sarika Singh

Sarika Singh

Updated on 13-Jun-2025 18:17:34

1K+ Views

Binary Insertion SortBinary insertion sort is an improved version of the regular Insertion Sort algorithm. In a normal insertion sort, each element is compared linearly with the sorted portion of the list to find its correct position. This takes O(n) comparisons for each element. In Binary Insertion Sort, we use ... Read More

Python Program for Activity Selection Problem

Sarika Singh

Sarika Singh

Updated on 13-Jun-2025 18:11:19

1K+ Views

The activity selection problem selects the maximum number of non-overlapping activities from a given set. Each activity has a start and finish time, and a single person can perform only one activity at a time. Problem Statement You are given n activities, each defined by a start time and a ... Read More

How to remove all leading whitespace in string in Python?

Sarika Singh

Sarika Singh

Updated on 11-Jun-2025 17:16:16

18K+ Views

Leading whitespace refers to any spaces, tabs, or other blank characters that appear at the start of a string. These characters come before the first visible text and can affect how the string is displayed. We can remove all leading whitespace from a string in Python using the lstrip() function. ... Read More

Advertisements