Python Program for Detect Cycle in a Directed Graph

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 (DFS) with a recursion stack to detect whether a cycle exists in a directed graph or not. Problem Statement You are given a directed graph represented using an adjacency list. Your task is to write a Python program to check whether the graph contains a cycle or not. DFS-Based Cycle ... Read More

Python Program for Cutting a Rod

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

898 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 as high as possible. This problem is similar to the 0-1 Knapsack problem and can be solved using recursion and dynamic programming techniques. It is useful in resource allocation and pricing strategies. Rod Cutting Problem Statement You are given a rod of length n and an array price[] where price[i] ... Read More

Python Program for Cocktail Sort

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

389 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 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. Problem Statement You are given an unsorted list of elements. The goal is to sort the list using Cocktail Sort, which moves ... Read More

Python Program for BogoSort or Permutation Sort

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

278 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. Problem Statement You are given a list of unsorted elements. Your task is to sort the list using BogoSort by repeatedly generating random permutations until the list is sorted. Random Permutation-based Sorting This sorting method checks if the list is sorted. If not, it randomly shuffles the elements and checks ... Read More

Python Program for Binary Insertion Sort

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 Binary Search to find the correct position of the current element in the sorted part of the list. This reduces the number of comparisons from O(n) to O(log n) per insertion. However, shifting elements still takes O(n) time in the worst case. Problem Statement You are given a list of ... Read More

Python Program for Basic Euclidean algorithms

Sarika Singh
Updated on 13-Jun-2025 18:14:27

602 Views

Euclidean AlgorithmThe Euclidean Algorithm is used to find the Greatest Common Divisor (GCD) of two numbers. The GCD of two integers is the largest number that divides both of them without leaving a remainder. This algorithm is based on the principle that the GCD of two numbers also divides their difference. The method repeatedly replaces the larger number by the remainder of dividing the larger number by the smaller one, until one of the numbers becomes zero. Problem Statement You are given two positive integers a and b. You need to find the greatest common divisor (GCD) of these two ... Read More

Python Program for Activity Selection Problem

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 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 of activities S: ... Read More

How to detect vowels vs consonants in Python?

Yaswanth Varma
Updated on 13-Jun-2025 17:39:56

6K+ Views

In the English alphabet, letters are categorized into vowels and consonants. The vowels are (a, e, i, o, u), and the remaining alphabetic characters are considered as consonants. In this article, we are going to detect the vowels vs the consonants in Python. Python provides a simple way to solve this problem by using the built-in string methods and logical conditions. Using Python in Operator The in operator is used to check whether the provided assignment character exists in the string. It returns true if it exists; otherwise, it returns false. To detect whether a letter is a ... Read More

When to use %r instead of %s in Python?

Yaswanth Varma
Updated on 13-Jun-2025 16:24:05

4K+ Views

Using %r instead of %s in PythonIn Python, string formatting is done by using the %(formatting operator). The %s uses the str() to convert the value, while the %r uses the repr() to convert values. Both operators look similar, but the difference is that they serve different purposes: %s: It converts the value to a human-readable string. %r: It converts the value that contains the quotes, escape characters, etc. Let's dive into the article to learn when to use %r instead of %s, which helps in debugging and logging. ... Read More

Self Destructing Code in C

Tapas Kumar Ghosh
Updated on 13-Jun-2025 14:27:16

1K+ Views

The self-destructing code is a type of program that can delete itself. It automatically executes the program and then removes the executable file once the execution is done. In this article, we will learn how to write self-destructing code for a C program. Write a Self Destructing Code in C++ You can write a self-destructing program that deletes its own executable file after it finishes running by using the remove() function. The remove() function is a built-in function from the C standard library () that deletes the specified file from the file system. To delete the program's own executable file, ... Read More

Advertisements