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 27 of 377
Python program to validate string has few selected type of characters or not
When working with strings in Python, you may need to validate whether a string contains specific types of characters. This article demonstrates how to check if a string contains numbers, lowercase letters, and uppercase letters. Problem Statement Given a string, we need to check whether it contains all three of the following character types ? Numbers (digits) Lowercase letters Uppercase letters Note: The string may contain other symbols, but all three character types above must be present. For example, if the input string is "p25KDs", the output should be True because it contains ...
Read MorePython Program to calculate function from indicator random variable from given condition
This problem involves calculating the expected value of a function from indicator random variables based on local extrema in random permutations. Given values k and n, we work with a random permutation of first n natural numbers and calculate F = (X₂+...+Xₙ₋₁)ᵏ, where Xᵢ is 1 when pᵢ₋₁ < pᵢ > pᵢ₊₁ or pᵢ₋₁ > pᵢ < pᵢ₊₁ (local maximum or minimum), and 0 otherwise. Understanding the Problem The indicator random variable Xᵢ equals 1 when position i is a local extremum (peak or valley) in the permutation. The expected value calculation uses precomputed formulas for different values ...
Read MorePython program to change character of a string using given index
Strings in Python are immutable, meaning you cannot modify individual characters directly. When you need to change a character at a specific index, you must create a new string using slicing and concatenation. For example, if we have s = "python", i = 3, and c = 'P', attempting s[i] = c will raise a TypeError: 'str' object does not support item assignment. Algorithm To replace a character at index i with character c ? Extract the left part: s[0:i] Extract the right part: s[i+1:] Concatenate: left + c + right Using String ...
Read MorePython program to split a string and join with comma
Sometimes we need to split a string into individual words and then join them back together with commas instead of spaces. Python provides simple built-in methods to accomplish this task efficiently. So, if the input is like s = "Programming Python Language Easy Funny", then the output will be Programming, Python, Language, Easy, Funny Algorithm To solve this, we will follow these steps − words − Create a list of words by applying split() function on the string with delimiter " " (space) result − Join each ...
Read MorePython program to find hash from a given tuple
The hash() function in Python computes a hash value for hashable objects like tuples. Hash values are integers used internally by Python for dictionary keys and set membership tests. Tuples are hashable because they are immutable, unlike lists which cannot be hashed due to their mutable nature. Syntax hash(object) Where object must be a hashable type (int, float, string, tuple, etc.). Example Let's find the hash value of a tuple containing integers − def solve(t): return hash(t) t = (2, 4, 5, 6, 7, 8) result ...
Read MorePython program to sort and reverse a given list
When working with lists in Python, you often need to sort or reverse them without modifying the original list. Python provides sorted() and reversed() functions that return new objects, unlike sort() and reverse() which modify the list in-place. Problem Statement Given a list of numbers, create a reversed version and a sorted version without changing the original list. Example Input and Output If the input is l = [2, 5, 8, 6, 3, 4, 7, 9], the output should be ? Reversed: [9, 7, 4, 3, 6, 8, 5, 2] Sorted: [2, 3, 4, ...
Read MorePython program to find average score of each students from dictionary of scores
When working with student scores stored in a dictionary, calculating the average score for each student is a common task. In Python, we can achieve this by iterating through the dictionary and computing the mean of each student's scores. So, if the input is like scores = {'Amal' : [25, 36, 47, 45], 'Bimal' : [85, 74, 69, 47], 'Tarun' : [65, 35, 87, 14], 'Akash' : [74, 12, 36, 75]}, then the output will be [38.25, 68.75, 50.25, 49.25] where 38.25 is average score for Amal, 68.75 is average score for Bimal and so on. Algorithm ...
Read MoreProgram to find expected sum of subarrays of a given array by performing some operations in Python
Given an array A of size n and two values p and q, we need to find the expected sum of subarrays after performing specific operations. This problem involves probability calculations and matrix operations to determine the expected value. Problem Understanding We can perform two types of operations on array A: Randomly select two indices (l, r) where l < r, then swap A[l] and A[r] Randomly select two indices (l, r) where l < r, then reverse subarray A[l..r] After performing the first operation p times and second operation q times, we randomly ...
Read MorePython program to display all second lowest grade student name from nested list
Suppose we have the names and grades for each student in a nested list, we have to display the names of any students having the second lowest grade. If there are more than one students with the second lowest grade, reorder these in alphabetical order and print each name on a new line. So, if the input is like students = [['Amal', 37], ['Bimal', 37], ['Tarun', 36], ['Akash', 41], ['Himadri', 39]], then the output will be Amal and Bimal, both having the second lowest score of 37, displayed in alphabetical order. Algorithm Steps To solve this, we ...
Read MorePython program to find runner-up score
Finding the runner-up score means identifying the second highest score in a list of participants. This is a common problem in competitive programming and data analysis. So, if the input is like scores = [5, 8, 2, 6, 8, 5, 8, 7], then the output will be 7 because the winner score is 8 and second largest score is 7. Algorithm To solve this, we will follow these steps − Initialize winner := -99999 Initialize runner_up := -99999 For each score in the ...
Read More