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
Articles by Arnab Chakraborty
Page 121 of 377
Program 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 MoreProgram to find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python
Suppose we have a string s with only "a" and "b". "a"s can stay "a" or turn into "b", but "b"s can not be changed. We have to find the number of unique strings that we can make. So, if the input is like s = "baab", then the output will be 4, as we can make these strings − ["baab", "babb", "bbab", "bbbb"] Approach The key insight is that only characters 'a' can be transformed. Each 'a' has 2 choices (remain 'a' or become 'b'), while 'b' characters remain unchanged. If we have n occurrences of ...
Read MoreProgram to check a number is ugly number or not in Python
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. In other words, an ugly number can be expressed as 2i × 3j × 5k where i, j, and k are non-negative integers. So, if the input is like n = 18, then the output will be True, as 18's prime factors are 2 and 3 (18 = 2 × 32). Algorithm To solve this, we will follow these steps − If n ≤ 0, then return False (ugly numbers are positive) Create a list of factors [2, ...
Read MoreProgram to create one triangle stair by using stars in Python
Suppose we have a number n, we have to create a string representing stairs with n steps. Each line in the string is separated by a newline separator, forming a triangle pattern with stars. So, if the input is like n = 5, then the output will be ? * ** *** **** ***** Algorithm To solve this, we will follow these steps ? Initialize an empty string For each step from 0 to n-1: Add (n-i-1) number of spaces for ...
Read MoreProgram to check whether given matrix is Toeplitz Matrix or not in Python
A Toeplitz matrix is a square matrix where every diagonal descending from left to right has the same value. In other words, all elements along each diagonal from top-left to bottom-right are identical. So, if the input matrix is like: 7 2 6 3 7 2 5 3 7 Then the output will be True because each diagonal has consistent values. Algorithm To check if a matrix is Toeplitz, we follow these steps: For each row i except the last one, do: For each ...
Read More