Python Articles

Page 318 of 855

Python program to find hash from a given tuple

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 6K+ Views

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 More

Python program to sort and reverse a given list

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 1K+ Views

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 More

Python program to find average score of each students from dictionary of scores

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 9K+ Views

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 More

Program to find expected sum of subarrays of a given array by performing some operations in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 436 Views

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 More

Python program to display all second lowest grade student name from nested list

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

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 More

Python program to find runner-up score

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

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

Program to find out the palindromic borders in a string in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 356 Views

A palindromic border is a substring that appears as both a prefix and suffix of a string, and the substring itself is a palindrome. For example, in the string 'ababab', the substring 'ab' is a border, but not palindromic. However, in 'pqpqp', the substring 'p' is both a border and palindromic. Given a string, we need to find the sum of palindromic borders across all possible substrings. The result should be returned modulo 10^9 + 7. Example For the string 'pqpqp', there are 15 total substrings, but only 4 have palindromic borders ? pqp : ...

Read More

Program to find expected value of maximum occurred frequency values of expression results in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 196 Views

When analyzing M expressions with results ranging from 1 to N, we need to find the expected value of the maximum frequency among all possible outcomes. This problem involves calculating probabilities using combinatorics and the inclusion-exclusion principle. Problem Understanding Given M expressions with results in range [1, N], we want to find the expected value of the maximum frequency. For each possible sequence of expression results, we count the frequency of the most common value. For example, with M = 3, N = 3: Sequence Maximum Frequency 1113 1122 1132 1212 1222 1231 ...

Read More

Program to find out the substrings of given strings at given positions in a set of all possible substrings in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 231 Views

When working with multiple strings, we often need to find specific substrings from the union of all possible substrings. This involves generating all substrings from each string, creating a sorted union set, and retrieving elements at specified positions. So, if the input strings are ['pqr', 'pqt'] and queries are [4, 7, 9], then the output will be ['pqt', 'qt', 't']. How It Works The substrings from the first string are: {p, pq, pqr, q, qr, r} The substrings from the second string are: {p, pq, pqt, q, qt, t} The union of these sets is: {p, ...

Read More

Program to count number of isosceles triangle from colored vertex regular polygon in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 426 Views

In a regular polygon with n sides, we need to count isosceles triangles formed by vertices of the same color. The vertices are colored either blue (0) or red (1) represented as a binary string. An isosceles triangle has at least two sides of equal length. So, if the input is like polygon = "111010", then the output will be 2 because there are two triangles ACE and AFE as shown in the diagram ? A(1) B(1) ...

Read More
Showing 3171–3180 of 8,546 articles
« Prev 1 316 317 318 319 320 855 Next »
Advertisements