Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 295 of 377

Program to find ith element by rotating k times to right

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 175 Views

Suppose we have an array nums, and a value k and another value i. We have to find the element at index i after rotating elements of nums, k number of times to the right.So, if the input is like nums = [2, 7, 9, 8, 10] k = 3 i = 2, then the output will be 10 because after 3rd rotation array will be [9, 8, 10, 2, 7], so now the ith element will be nums[2] = 10.To solve this, we will follow these steps −for r in range 0 to k, dodelete last element from nums ...

Read More

Python program to validate postal address format

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 1K+ Views

Suppose we have a postal code we have to check whether it is valid or not. A valid postal code has following criteriaIt must be a number in the range from 100000 to 999999 (both inclusive).It must not contain more than one alternating repetitive digit pair.So, if the input is like s = "700035", then the output will be True as this is in range 100000 to 999999 and there are no consecutive digits either.To solve this, we will follow these steps −n := size of snb := 0ok := Truefor i in range 0 to n - 1, dook ...

Read More

Python program to find ways to get n rupees with given coins

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 573 Views

Suppose we have given a coins of denominations (1, 2, 5 and 10). We have to find in how many ways can we can arrange n using these dominations. We have an array called count with 4 elements, where count[0] indicates number of coins of 1, count[1] indicates number of coins for 2 and so on.So, if the input is like n = 27 count = [8, 4, 3, 2], then the output will be 18 so there are 18 possible combinations some of them are10*2 + 5*1 + 2*1 = 2710*2 + 2*3 + 1*1 = 2710*1 + 5*3 ...

Read More

Python program to find word score from list of words

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 762 Views

Suppose we have few words in an array. These words are in lowercase letters. We have to find the total score of these set of words based on following rules −Consider vowels are [a, e, i, o, u and y]The score of an individual word is 2 when the word contains an even number of vowels.Otherwise, the score of that word is 1.The score for the whole set of words is the sum of scores of all words in the set.So, if the input is like words = ["programming", "science", "python", "website", "sky"], then the output will be 6 because ...

Read More

Program to remove duplicate characters from a given string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 8K+ Views

Suppose we have a string s. We have to remove all duplicate characters that have already appeared before. The final string will have same ordering of characters like the actual one.We can solve this by using ordered dictionary to maintain the insertion order of the characters. The value will be the frequency of those characters, however the frequency values are not important here. After forming the dictionary, we can simply take the keys and join them to get the string.So, if the input is like s = "bbabcaaccdbaabababc", then the output will be "bacd".d := a dictionary where keys are ...

Read More

Python program to check credit card number is valid or not

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 4K+ Views

Suppose we have a credit card number. We have to check whether the card number is valid or not. The card numbers have certain properties −It will start with 4, 5 and 6It will be 16 digits’ longNumbers must contain only digitsIt may have digits in four groups separated by '-'It must not use any other separator like space or underscoreIt must not have 4 or more consecutive same digitsSo, if the input is like s = "5423-2578-8632-6589", then the output will be TrueTo solve this, we will follow these steps −if number of '-' in s is greater than ...

Read More

Program to rotate a string of size n, n times to left in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 744 Views

Suppose we have a string s of size n. We have to find all rotated strings by rotating them 1 place, 2 places ... n places.So, if the input is like s = "hello", then the output will be ['elloh', 'llohe', 'lohel', 'ohell', 'hello']To solve this, we will follow these steps −res := a new listn := size of sfor i in range 0 to n, dos := (substring of s from index 1 to n-1) concatenate s[0]insert s at the end of resreturn resExampleLet us see the following implementation to get better understanding −def solve(s):    res = [] ...

Read More

Program to find elements from list which have occurred at least k times in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 370 Views

Suppose we have a list of elements called nums, and a value k. We have to find those elements which have occurred at least k number of times.So, if the input is like nums = [2, 5, 6, 2, 6, 1, 3, 6, 3, 8, 2, 5, 9, 3, 5, 1] k = 3, then the output will be [2, 5, 6, 3]To solve this, we will follow these steps −c := a list containing frequencies of each elements present in numsres := a new listfor each key n in c, doif c[n] >= k, theninsert n at the end ...

Read More

Python program to find product of rational numbers using reduce function

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 539 Views

Suppose we have a list of rational numbers. We have to find their product using reduce function. The reduce() function applies a function with two arguments cumulatively on a list of objects from left to right.So, if the input is like fractions = [(5, 3), (2, 8), (6, 9), (5, 12), (7, 2)], then the output will be (175, 432) because 5/3 * 2/8 * 6/9 * 5/12 * 7/2 = (5*2*6*5*7)/(3*8*9*12*2) = 2100/5184 = 175/432.To solve this, we will follow these steps −fracs := a new listfor each f in frac, doinsert a new fraction object from (numerator, denominator) ...

Read More

Python program to validate email address

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 15K+ Views

Suppose we have an email address as string. We have to check whether this is valid or not based on the following conditions −The format must be username@company.domain formatUsername can only contain upper and lowercase letters, numbers, dashes and underscoresCompany name can only contain upper and lowercase letters and numbersDomain can only contain upper and lowercase lettersMaximum length of the extension is 3.We can use regular expression to validate the mail addresses. Regular expressions can be used by importing re library. To match a pattern we shall use match() function under re library.So, if the input is like s = ...

Read More
Showing 2941–2950 of 3,768 articles
« Prev 1 293 294 295 296 297 377 Next »
Advertisements