Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 386 of 2109
Check order of character in string using OrderedDict( ) in Python
When checking if characters in a string appear in the same order as a given pattern, the OrderedDict from Python's collections module provides an effective solution. This method preserves the order of characters as they first appear in the string. How OrderedDict Helps An OrderedDict maintains the insertion order of keys. Using OrderedDict.fromkeys() creates a dictionary with unique characters from the string in their first appearance order, which we can then compare against our pattern. Example from collections import OrderedDict def check_order(my_input, my_pattern): my_dict = OrderedDict.fromkeys(my_input) ...
Read MoreInsertion at the beginning in OrderedDict using Python
When working with OrderedDict in Python, you may need to insert elements at the beginning rather than the end. Python's OrderedDict doesn't have a direct method for insertion at the beginning, but you can achieve this using the update() method combined with move_to_end(). What is OrderedDict? OrderedDict is a dictionary subclass from the collections module that remembers the order in which items were inserted. This makes it useful when the sequence of key-value pairs matters. Method: Using update() and move_to_end() To insert at the beginning, first add the new item using update(), then move it to ...
Read MoreExtract Unique dictionary values in Python Program
When it is required to extract unique values from a dictionary, there are several approaches available. The most effective method combines set comprehension with sorted() to eliminate duplicates and arrange values in order. Using Set Comprehension with Sorted This approach flattens all dictionary values into a set (removing duplicates) and then sorts them ? my_dict = {'hi': [5, 3, 8, 0], 'there': [22, 51, 63, 77], 'how': [7, 0, 22], ...
Read MoreCount Number of Lowercase Characters in a String in Python Program
When you need to count the number of lowercase characters in a string, Python provides the islower() method which can be combined with a simple for loop to achieve this task. Using islower() Method with for Loop The islower() method returns True if the character is a lowercase letter, and False otherwise ? my_string = "Hi there how are you" print("The string is") print(my_string) my_counter = 0 for i in my_string: if(i.islower()): my_counter = my_counter + 1 print("The number of lowercase characters ...
Read MoreTake in Two Strings and Display the Larger String without Using Built-in Functions in Python Program
When it is required to take two strings and display the larger string without using any built-in function, a counter can be used to get the length of the strings, and if condition can be used to compare their lengths. Below is the demonstration of the same − Example string_1 = "Hi there" string_2 = "Hi how are ya" print("The first string is :") print(string_1) print("The second string is :") print(string_2) count_1 = 0 count_2 = 0 for i in string_1: count_1 = count_1 + 1 for j in ...
Read MorePython Program to Find All Connected Components using DFS in an Undirected Graph
When working with undirected graphs, connected components are subsets of vertices where each vertex is reachable from every other vertex within the same component. We can find all connected components using Depth-First Search (DFS) traversal. Below is a demonstration of finding connected components using a graph class ? Graph Class Implementation First, let's create a graph class with methods for DFS traversal and finding connected components ? class Graph_struct: def __init__(self, V): self.V = V ...
Read MorePython Program to Calculate the Length of a String Without Using a Library Function
When it is required to calculate the length of a string without using library methods, a counter is used to increment every time an element of the string is encountered. Below is the demonstration of the same ? Example my_string = "Hi Will" print("The string is :") print(my_string) my_counter = 0 for i in my_string: my_counter = my_counter + 1 print("The length of the string is") print(my_counter) Output The string is : Hi Will The length of the string is 7 Using While Loop ...
Read MorePython Program to Find the Sum of All Nodes in a Binary Tree
When working with binary trees, finding the sum of all nodes is a common operation. We can implement this using a tree class that contains methods to build the tree and calculate the sum recursively. Each node stores data and maintains references to its children. Below is a demonstration of the same − Tree Class Implementation First, let's create a tree structure class with methods for adding nodes and calculating the sum ? class Tree_struct: def __init__(self, data=None): self.key = data ...
Read MorePython Program to solve Maximum Subarray Problem using Kadane's Algorithm
The Maximum Subarray Problem finds the contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm solves this problem efficiently in O(n) time complexity using dynamic programming principles. Understanding Kadane's Algorithm Kadane's algorithm maintains two variables: the maximum sum ending at the current position and the overall maximum sum seen so far. At each position, it decides whether to extend the existing subarray or start a new one. Basic Implementation Here's a simple version that returns only the maximum sum ? def kadane_simple(arr): max_ending_here = ...
Read MorePython Program to Find if Undirected Graph contains Cycle using BFS
A cycle in an undirected graph occurs when you can start at a vertex and return to it by following edges without retracing the same edge. We can detect cycles using Breadth-First Search (BFS) by tracking parent nodes during traversal. How BFS Cycle Detection Works The algorithm uses a queue to traverse the graph level by level. For each visited vertex, we track its parent. If we encounter a visited vertex that is not the current vertex's parent, we've found a cycle. Implementation Helper Functions First, let's define a function to add edges to our ...
Read More