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
Programming Articles
Page 574 of 2547
Regular Expression Examples in Python
Regular expressions (regex) are powerful tools for pattern matching and text processing in Python. The re module provides functions to work with regular expressions, allowing you to search, match, and manipulate text based on specific patterns. Literal Characters Literal characters in regex match themselves exactly. Here's how to use basic literal matching ? import re text = "python is awesome" pattern = "python" match = re.search(pattern, text) if match: print(f"Found: '{match.group()}'") else: print("Not found") Found: 'python' Character Classes Character classes ...
Read MoreLargest Number in Python
Finding the largest number from a list of non-negative integers requires arranging them in a specific order. For example, given [10, 2], the largest number would be 210, not 102. The key insight is to sort numbers based on which arrangement produces a larger concatenated result. We compare two numbers by checking if x + y is greater than y + x as strings. Algorithm Steps Convert all numbers to strings Sort using a custom comparator that checks concatenated results Join the sorted strings and handle edge cases Implementation Let us see the ...
Read MoreBinary Tree Inorder Traversal in Python
Binary tree inorder traversal visits nodes in the order: left subtree, root, right subtree. This article demonstrates how to perform inorder traversal iteratively using a stack instead of recursion. 10 5 15 2 7 20 ...
Read MoreDecode Ways in Python
Suppose we have a message containing letters from A to Z that is being encoded to numbers using the following mapping − 'A' → 1, 'B' → 2 ... 'Z' → 26. Given a non-empty string containing only digits, we need to find in how many ways that string can be decoded. For example, if the string is "12", it can be decoded as "AB" (1, 2) or "L" (12), so there are two possible ways. Algorithm Approach We will solve this using dynamic programming with the following steps ? Create a DP array where ...
Read MoreCombinations in C++
Generating all possible combinations of k numbers from a range 1 to n is a classic problem that can be solved efficiently using backtracking. For example, if n = 4 and k = 2, the combinations would be [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]. In Python, we can implement this using recursion with backtracking to explore all possible combinations systematically. Using Recursive Backtracking The backtracking approach builds combinations incrementally and backtracks when a complete combination is found ? def combine(n, k): result = [] ...
Read MoreSort Colors in Python
The Sort Colors problem involves sorting an array containing only three values: 0 (red), 1 (white), and 2 (blue). We need to sort them in-place so objects of the same color are adjacent, in the order red, white, blue. For example, if the input array is [2,0,2,1,1,0], the sorted output should be [0,0,1,1,2,2]. Algorithm: Dutch National Flag This problem is efficiently solved using the Dutch National Flag algorithm with three pointers ? Set low = 0, mid = 0, and high = length of array − 1 While mid
Read MorePow(x, n) in Python
Calculating x to the power n efficiently without using built-in functions like pow() or ** is a common programming problem. We can implement this using binary exponentiation, which reduces time complexity from O(n) to O(log n). Problem Statement Given two inputs x and n, where x is a number in range -100.0 to 100.0 and n is a 32-bit signed integer, we need to calculate xn efficiently. Example If x = 12.1 and n = -2, then xn = 12.1-2 = 1/(12.1)2 = 0.00683 Algorithm We use binary exponentiation which works by breaking down ...
Read MoreMultiply Strings in C++
We are given two strings consisting of integers that can have lengths up to 200. Both strings do not contain any leading zeros, but 0 as a number itself can be present. We need to multiply these integer strings and return the result as a string. For example, if we have two numbers as strings "26" and "12", the result will be "312". Following are various ways to multiply strings in C++: Using Built−in stoll() method Digit−by−Digit String Multiplication Algorithm Using Karatsuba Multiplication Algorithm ...
Read MoreValid Sudoku in Python
A valid Sudoku puzzle must follow three key rules for all filled cells: no duplicate digits in any row, column, or 3x3 sub-box. We need to validate only the filled cells, not solve the entire puzzle. Validation Rules A 9x9 Sudoku board is valid when ? Each row contains digits 1-9 without repetition Each column contains digits 1-9 without repetition Each of the 9 (3x3) sub-boxes contains digits 1-9 without repetition Algorithm Approach We'll use three dictionaries to track seen digits in rows, columns, and 3x3 blocks. For each cell, we calculate which ...
Read MoreFind First and Last Position of Element in Sorted Array in Python
Finding the first and last position of an element in a sorted array is a classic problem that can be efficiently solved using binary search. Given a sorted array and a target value, we need to return the starting and ending indices of the target. If the target is not found, we return [-1, -1]. For example, if the array is [2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6] and target is 4, the output should be [4, 7] since 4 appears from index 4 to index 7. Algorithm We use two binary searches ...
Read More