Sarika Singh

Sarika Singh

142 Articles Published

Articles by Sarika Singh

Page 2 of 15

Python Program for Cutting a Rod

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 1K+ Views

The Rod Cutting problem is a classic dynamic programming challenge where we cut a rod into pieces to maximize total value. Given a rod of length n and prices for different lengths, we find the optimal way to cut the rod for maximum profit. Problem Statement You are given a rod of length n and an array price[] where price[i] represents the price of a rod of length i+1. Your task is to determine the maximum value obtainable by cutting up the rod and selling the pieces. Using Recursive Approach The recursive approach tries all possible ...

Read More

Python Program for Cocktail Sort

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 521 Views

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 to their respective ends. This approach helps in reducing the number of iterations compared to Bubble Sort, especially when smaller elements are at the end of the list. How Cocktail Sort Works Cocktail Sort improves upon Bubble Sort by sorting in both directions during each pass − ...

Read More

Python Program for BogoSort or Permutation Sort

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 425 Views

BogoSort, also known as Permutation Sort or Stupid Sort, is a highly inefficient sorting algorithm that works by generating random permutations of a list until it becomes sorted. Despite being impractical for real-world use, it serves as an interesting example of how not to design algorithms. BogoSort continues to shuffle the array randomly until the list becomes sorted. The expected time complexity is unbounded, making its average performance extremely poor compared to efficient algorithms like QuickSort or MergeSort. Algorithm Overview The BogoSort algorithm follows these simple steps ? Check if the list ...

Read More

Python Program for Binary Insertion Sort

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 1K+ Views

Binary insertion sort is an improved version of the regular insertion sort algorithm. In normal insertion sort, each element is compared linearly with the sorted portion to find its correct position, taking O(n) comparisons per element. Binary insertion sort uses binary search to find the correct insertion position, reducing comparisons from O(n) to O(log n) per element. However, shifting elements still requires O(n) time in the worst case. How Binary Insertion Sort Works The algorithm combines binary search with insertion sort ? Start from the second element (index 1) ...

Read More

Python Program for Activity Selection Problem

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 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 finish time. The goal is to choose the maximum number of activities that a single person can perform without any overlap between the selected activities. Variable Notations Below are the variables used in the problem definition − N: Total number ...

Read More

Python Program for 0-1 Knapsack Problem

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 6K+ Views

The 0-1 Knapsack Problem is a classic optimization problem where you have a knapsack with limited capacity and items with specific weights and values. The goal is to select items that maximize value without exceeding the weight limit, where each item can only be taken once (0 or 1). Problem Types There are three main variants of the knapsack problem − 0-1 Knapsack: Each item can either be selected or not (0 or 1) Fractional Knapsack: Items can be broken into fractions Unbounded Knapsack: ...

Read More

Python program to find all duplicate characters in a string

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 41K+ Views

This article teaches you how to write a Python program to find all duplicate characters in a string using different approaches. Characters that repeat themselves within a string are referred to as duplicate characters. When we refer to finding duplicate characters in a string, we mean identifying every character that appears more than once in the string. Input-Output Scenario Following is the input-output scenario to find all the duplicate characters in a string ? Input: TutorialsPoint Output: t, o, i As we can see, the duplicate characters in the given string "TutorialsPoint" are ...

Read More

__name__ (A Special variable) in Python

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 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 imported as a module into another script). In this article, we will learn about the __name__ variable in Python and how to use it effectively. Understanding the __name__ Variable The __name__ variable is a built-in variable that holds the name of the current module. When you run a script directly, Python sets __name__ to __main__. If the same script is ...

Read More

Python program to check for URL in a string

Sarika Singh
Sarika Singh
Updated on 25-Mar-2026 3K+ Views

This article will teach you how to determine whether a string contains a URL or not. In Python, strings are collections of bytes that represent Unicode characters. When given a string, we will first determine whether it contains a URL and then extract it using regular expressions. Using findall() Method We will use Python's regular expression concept to solve this problem. Regular expressions are supported by the Python re module. The findall() method returns a list of all matches found in the string, scanning from left to right. Syntax re.findall(pattern, string) Example ...

Read More

Python Program to print unique values from a list

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 3K+ Views

List is a built-in data structure in python which functions like a dynamically sized array. Lists are created by putting elements in square brackets [element1, element2, ....]. Elements present in list are indexed and the indexing starts from [0]. List has the following properties − List is mutable meaning that elements can be added, removed or changed from a list after its creation. List is ordered, so adding new elements to the list ...

Read More
Showing 11–20 of 142 articles
« Prev 1 2 3 4 5 15 Next »
Advertisements