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 525 of 2109
Program to find the sum of elements that forms a Z shape on matrix in Python
Suppose we have one n x n matrix M, we have to find the sum of all elements that form a Z shape in the matrix. A Z-shape consists of three parts: the first row (top horizontal line), the diagonal from top-right to bottom-left (middle diagonal), and the last row (bottom horizontal line). Example Matrix So, if the input is like ? 4 3 2 9 1 8 2 5 6 Then the output will be 23, as elements are [4+3+2+1+2+5+6] = 23. Algorithm To solve this, we will follow these steps ? n := row count of matrix if n
Read MoreProgram to check we can spell out the target by a list of words or not in Python
Given a list of numbers and a starting index, we need to determine if we can reach the end of the list by jumping. At any index i, we can move left or right by exactly nums[i] steps. For example, with nums = [0, 0, 2, 1, 3, 3, 1, 1] and starting index k = 2, we can reach the end by jumping from index 2 to index 4, then to the last index 7. Algorithm We'll use a breadth-first search (BFS) approach to explore all possible positions: Initialize a visited array to track ...
Read MoreProgram to find the maximum profit we can get by buying on stock market multiple times in Python
Suppose we have a list of prices representing the stock prices of a company in chronological sequence, we have to find the maximum profit we could have made from buying and selling that stock any number of times. We have to keep in mind that we must buy before we can sell it. So, if the input is like prices = [10, 50, 30, 40, 60], then the output will be 70, as we can buy at 10, sell at 50, buy at 30, and sell at 60. Algorithm To solve this, we will follow these steps: ...
Read MoreProgram to check whether given graph is bipartite or not in Python
A bipartite graph is an undirected graph where vertices can be divided into two disjoint sets such that no two vertices within the same set are adjacent. In other words, every edge connects vertices from different sets. For example, if we have the following graph: 0 1 2 3 4 ...
Read MoreProgram to find the maximum profit we can get by buying on stock market once in Python
Finding the maximum profit from buying and selling stock once is a classic programming problem. Given a list of stock prices in chronological order, we need to find the maximum profit possible by buying once and selling once, where we must buy before selling. For example, if the input is prices = [10, 12, 9, 6, 8, 12], the output will be 6, as we can buy at price 6 and sell at price 12. Algorithm To solve this efficiently, we use a single pass approach ? Initialize max_profit to 0 Initialize min_price to infinity ...
Read MoreProgram to sort all vowels at beginning then the consonants, are in sorted order in Python
Suppose we have a lowercase alphabet string s, we have to find a string with all the vowels of s in sorted sequence followed by all consonants of s in sorted sequence. So, if the input is like "helloworld", then the output will be "eoodhlllrw", as vowels are "eooo" in sorted order and consonants are "dhlllrw" in sorted order. Algorithm To solve this, we will follow these steps − vowel_str := blank string, consonant_str := blank string for each character c in s, do if c is a vowel, then vowel_str := vowel_str ...
Read MoreProgram to encrypt a string using Vigenere cipher in Python
The Vigenère cipher is a classic encryption technique that shifts each character in a text by a different amount based on a key. Each character in the text is shifted by the corresponding character in the key, where A=0, B=1, C=2, and so on. For example, if we have text = "code" and key = "team", each letter gets shifted: 'c' + 't'(19) = 'v' 'o' + 'e'(4) = 's' 'd' + 'a'(0) = 'd' 'e' + 'm'(12) = 'q' Algorithm Steps To implement the Vigenère cipher, we follow these steps − Create an ...
Read MoreProgram to encrypt a string using Vertical Cipher in Python
The Vertical Cipher is an encryption technique that rearranges a string by distributing characters into columns and reading them vertically. Given a string and number of rows, we arrange the string into a grid and extract each column to form encrypted segments. For example, with string "ilovepythonprogramming" and n = 5 rows, we arrange characters into 5 rows and read each column from top to bottom, left to right. How Vertical Cipher Works The algorithm distributes characters across n rows in a round-robin fashion ? Original: "ilovepythonprogramming" Arranged ...
Read MoreProgram to find the resolved Unix style path in Python
In Unix systems, paths can contain special directory symbols: ".." represents the parent directory and "." represents the current directory. When given a path as a list of strings, we need to resolve these symbols to find the actual final path. For example, if we have ["usr", "..", "usr", ".", "local", "etc", "foo"], this represents the path /usr/../usr/./local/etc/foo which resolves to /usr/local/etc/foo. Algorithm The solution uses a stack-based approach ? Create an empty stack (list) to store resolved path components For each element in the path: If element is "..": pop from stack (go ...
Read MoreProgram to find the number of unique integers in a sorted list in Python
Finding the number of unique integers in a sorted list is a common programming task. Python provides several efficient approaches to solve this problem, from using built-in data structures to leveraging the sorted nature of the list. So, if the input is like nums = [3, 3, 3, 4, 5, 7, 7], then the output will be 4, as the unique numbers are [3, 4, 5, 7]. Using Set Data Structure The most straightforward approach is to use a set to track unique elements ? def count_unique_with_set(nums): unique_set = set() ...
Read More