Change Character of a String Using Given Index in Python

Arnab Chakraborty
Updated on 11-Oct-2021 10:55:39

413 Views

Suppose we have a string s, an index i and a character c. We have to replace the ith character of s using c. Now in Python, strings are immutable in nature. We cannot write a statement like s[i] = c, it will raise an error [TypeError: 'str' object does not support item assignment]So, if the input is like s = "python", i = 3, c = 'P', then the output will be "pytPon"To solve this, we will follow these steps −left := s[from index 0 to i]right := s[from index i+1 to end]return left concatenate c concatenate rightExampleLet us ... Read More

Split a String and Join with Comma in Python

Arnab Chakraborty
Updated on 11-Oct-2021 10:31:58

1K+ Views

Suppose we have few words that are separated by spaces. We have to split these words to form a list, then join them into a string by placing comma in-between.So, if the input is like s = "Programming Python Language Easy Funny", then the output will be Programming, Python, Language, Easy, FunnyTo solve this, we will follow these steps −words := a list of words by applying split function on s with delimiter " " blank space.ret := join each items present in words and place ", " in between each pair of wordsreturn retExampleLet us see the following implementation ... Read More

Python Program to Swap Case of English Word

Arnab Chakraborty
Updated on 11-Oct-2021 10:29:28

322 Views

Suppose we have a string with English letters. We have to swap the case of the letters. So uppercase will be converted to lower and lowercase converted to upper.So, if the input is like s = "PrograMMinG", then the output will be pROGRAmmINgTo solve this, we will follow these steps −ret := blank stringfor each letter in s, doif letter is in uppercase, thenret := ret concatenate lower case equivalent of letterotherwise, ret := ret concatenate upper case equivalent of letterreturn retExampleLet us see the following implementation to get better understandingdef solve(s):    ret = '' ... Read More

Find Hash from a Given Tuple in Python

Arnab Chakraborty
Updated on 11-Oct-2021 10:26:56

5K+ Views

Suppose we have a tuple. There are few numbers are present. We have to find the hash value of this tuple by using hash() function. This is a built-in function. The hash() function can work on some datatypes like int, float, string, tuples etc, but some types like lists are not hashable. As lists are mutable in nature, we cannot hash it. This hash value is used to map other values when we use dictionary.So, if the input is like t = (2, 4, 5, 6, 7, 8), then the output will be -1970127882925375109To solve this, we will follow these ... Read More

Sort and Reverse a Given List in Python

Arnab Chakraborty
Updated on 11-Oct-2021 10:22:32

989 Views

Suppose we have a list of numbers in Python. We have to reverse and sort the lists using list operations but do not change the actual list. To reverse the list we have reverse() function for lists but if we use it, the list will be reversed in place. Similar for the sort() also. To maintain actual order we will use the reversed() function and sorted() function.So, if the input is like l = [2, 5, 8, 6, 3, 4, 7, 9], then the output will be [9, 7, 4, 3, 6, 8, 5, 2] [2, 3, 4, 5, 6, ... Read More

Find Average Score of Each Student from Dictionary in Python

Arnab Chakraborty
Updated on 11-Oct-2021 10:20:43

9K+ Views

Suppose we have a dictionary of students marks. The keys are names and the marks are list of numbers. We have to find the average of each students.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] so 38.25 is average score for Amal, 68.75 is average score for Bimal and so on.To solve this, we will follow these steps −avg_scores := a new mapfor each name in scores ... Read More

Find Expected Sum of Subarrays in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:30:51

388 Views

Program to find expected sum of subarrays of a given array by performing some operationsSuppose we have an array A whose size is n and two values p and q. We can perform these operations on A.Randomly select two indexes (l, r) where l < r, then exchange A[l] and A[r]Randomly select two indexes (l, r) where l < r, then reverse subarray of A form index l to r.After performing first operation p number of times and the second operation q times, we randomly select two indices l & r where l < r and calculate the S = ... Read More

Display All Second Lowest Grade Students from Nested List in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:28:02

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 an 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, Bimal both have second least score 37, they are placed in alphabetic order.To solve this, we will follow these steps −min_mark := minimum ... Read More

Python Program to Find Runner-Up Score

Arnab Chakraborty
Updated on 11-Oct-2021 09:21:47

2K+ Views

Suppose we have a list of scores for different number of participants. We have to find the runner-up score.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.To solve this, we will follow these steps −winner := -99999runner_up := -99999for each i in scores, doif i > winner, thenwinner := irunner_up := winnerotherwise when i < winner and i > runner_up, thenrunner_up := ireturn runner_upExampleLet us see the following implementation to get better understandingdef solve(scores): ... Read More

Find Expected Value of Maximum Occurred Frequency Values in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:20:44

165 Views

Suppose we have M different expressions, and the answers of these expressions are in range 1 to N (both inclusive) So consider x = max(f(i)) for each i in range 1 through N, we have to find the expected value of x.So, if the input is like M = 3, N = 3, then the output will be 2.2, becauseSequenceMaximum frequency1113112211321222123113312223223223323333$$E(x) = \sum P(x) * x = P(1) + 2P(2) + 3P(3) = \frac{1}{10} + 2 * \frac{6}{10} + 3 * \frac{3}{10} = \frac{22}{10}$$To solve this, we will follow these steps −combination := a new mapDefine a function nCr() . ... Read More

Advertisements