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
Python Articles
Page 686 of 855
Fizz Buzz in Python
The Fizz Buzz problem is a classic programming challenge where we replace numbers with specific strings based on divisibility rules. For numbers from 1 to n, we print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both. Problem Rules If the number is divisible by 3, write "Fizz" instead of the number If the number is divisible by 5, write "Buzz" instead of the number If the number is divisible by both 3 and 5, write "FizzBuzz" instead ...
Read MoreMove Zeroes in Python
Sometimes we need to move all zero values in an array to the right while maintaining the relative order of non-zero elements. For example, transforming [0, 1, 5, 0, 3, 8, 0, 0, 9] into [1, 5, 3, 8, 9, 0, 0, 0, 0]. Algorithm Approach We use a two-pointer technique to solve this problem efficiently ? Use an insert_index pointer to track where to place the next non-zero element Iterate through the array and move non-zero elements to the front Fill remaining positions ...
Read MoreMissing Number in Python
Suppose we have a list of numbers from 0 to n. There is one number that is missing. We have to find the missing number in an efficient approach. So if A = [0, 1, 2, 3, 4, 5, 7, 8, 9], missing number is 6. Python provides multiple approaches to solve this problem efficiently. We'll explore three common methods: binary search, mathematical formula, and XOR operation. Method 1: Using Binary Search The binary search approach works by comparing array values with their indices after sorting ? Sort the list in ascending order Set high ...
Read MoreValid Anagram in Python
Anagrams are words or phrases formed by rearranging the letters of another word or phrase, using all the original letters exactly once. For example, "ANAGRAM" and "NAAGARM" are anagrams because they contain the same letters in different arrangements, but "cat" and "fat" are not anagrams because they have different letters. To check if two strings are valid anagrams, we can use several approaches. The most common method is to sort the characters of both strings and compare them. Method 1: Using Sorting Convert both strings to sorted character lists and compare them ? def is_anagram_sorting(s, ...
Read MoreDelete Node in a Linked List in Python
A linked list is a linear data structure where each element is a separate object, commonly referred to as a node. Each node contains two fields: the data and a pointer to the next node in the list. In this article, we'll learn how to delete a node from a singly linked list using Python. Suppose we have a linked list with a few elements. Our task is to delete a specific node, given only access to that node − not the head of the list ? Input: 1 → 3 → 5 → 7 → ...
Read MoreInvert Binary Tree in Python
Inverting a binary tree means swapping the left and right child nodes for every node in the tree. This creates a mirror image of the original tree structure. Original Tree 4 2 7 1 3 6 ...
Read MoreReverse Linked List in Python
A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a pointer to the next node. Reversing a linked list means changing the direction of pointers so that the last node becomes the first node. For example, if we have: 1 → 3 → 5 → 7, the reversed list will be: 7 → 5 → 3 → 1 Algorithm We can reverse a linked list using a recursive approach ? Define a recursive function solve(head, back) where back tracks the previous node If head ...
Read MoreHouse Robber in Python
The House Robber problem is a classic dynamic programming challenge. A robber wants to rob houses along a street, but cannot rob two adjacent houses as this would trigger an alarm. We need to find the maximum amount that can be robbed. Given an array where each element represents the amount of money in each house, we must determine the maximum sum possible without selecting adjacent elements. Problem Understanding For the array [2, 7, 10, 3, 1], the optimal strategy is to rob houses at indices 0, 2, and 4 (values 2, 10, 1) for a total ...
Read MoreBest Time to Buy and Sell Stock in Python
The Best Time to Buy and Sell Stock problem asks us to find the maximum profit from buying and selling a stock once. Given an array where each element represents the stock price on a specific day, we need to determine the optimal buy and sell days to maximize profit. For example, if prices = [7, 1, 5, 3, 6, 4], the maximum profit is 5 (buy at price 1 on day 2, sell at price 6 on day 5). Algorithm Approach We can solve this using two auxiliary arrays: leftMin: ...
Read MorePath Sum in Python
The Path Sum problem asks us to determine if there exists a root-to-leaf path in a binary tree where the sum of node values equals a given target sum. This is a classic tree traversal problem that can be solved using recursion. 0 -3 9 -10 5 ...
Read More