Found 10476 Articles for Python

Palindromic Substrings in Python

Arnab Chakraborty
Updated on 29-Apr-2020 06:12:53

6K+ Views

Suppose we have a string; we have to count how many palindromic substrings present in this string. The substrings with different start indices or end indices are counted as different substrings even they consist of same characters. So if the input is like “aaa”, then the output will be 6 as there are six palindromic substrings like “a”, “a”, “a”, “aa”, “aa”, “aaa”To solve this, we will follow these steps −count := 0for i in range 0 to length if stringfor j in range i + 1 to length of string + 1temp := substring from index i to jif ... Read More

Minimum Moves to Equal Array Elements II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:41:57

459 Views

Suppose we have a non-empty integer array, we have to find the minimum number of moves that are required to make all array elements equal, where a move is incrementing or decrementing a selected element by 1. So when the array is like [1, 2, 3], then the output will be 2, as 1 will be incremented to 2, and 3 will be decremented to 2.To solve this, we will follow these steps −sort the array numsset counter as 0for i in nums, docounter := counter + absolute of (i – nums[length of nums / 2])return counterExample(Python)Let us see the ... Read More

4Sum II in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:09:00

794 Views

Suppose we have four lists A, B, C, D of integer values, we have to compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. Consider all A, B, C, D have same length of N where 0 ≤ N ≤ 500. Remember all integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. So if the inputs are A = [1, 2], B = [-2, -1], C = [-1, 2], D = [0, 2], then ... Read More

Hexspeak in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:26:59

931 Views

Suppose a decimal number can be converted to its Hexspeak representation by converting it to an uppercase hexadecimal string at first, after that replacing all occurrences of the digit 0 with the letter O, and the digit 1 with the letter I.This kind of representation is valid if and only if it consists only of the letters in the set {"A", "B", "C", "D", "E", "F", "I", "O"}.So we have a string num representing a decimal integer N, we have to find the Hexspeak representation of N if it is correct, otherwise return "ERROR". So if num = “257”, then ... Read More

Diet Plan Performance in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:12:55

1K+ Views

Suppose a dieter consumes calories[i], this indicates the calories on the i-th day. If we have an integer k, for every consecutive sequence of k days that is (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 upper, so increase one point, lower = length of C, then come out from the looptemp := temp + C[right]return pointsExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object):    def dietPlanPerformance(self, c, k, l, u):       temp = 0       for i in range(k):          temp += c[i]       right ... Read More

Single-Row Keyboard in python

Arnab Chakraborty
Updated on 28-Apr-2020 08:59:34

411 Views

Suppose, there is a special keyboard with all keys in a single row. So if we have a string of length 26 indicating the layout of the keyboard (indexed from 0 to 25), initially our finger is at index 0. To type a character, we have to move your finger to the index of the next character. The time taken to move your finger from index i to index j is denoted as |i - j|. So if we want to type a string. we have to define a function to calculate how much time it takes to type it ... Read More

Check If a Number Is Majority Element in a Sorted Array in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:54:42

320 Views

Suppose we have an array called, nums and that is sorted in non-decreasing order, and a number target. We have to find if the target is a majority element. In an array a majority element is an element that appears more than N/2 times in an array of length N. So if the array is like − [2, 4, 5, 5, 5, 5, 5, 6, 6] and target is 5, then output is true.To solve this, we will follow these steps −There will be two helping modules, lower() and upper(). These are as follows.The lower() takes two arguments array arr ... Read More

Largest Unique Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:46:16

565 Views

Suppose we have a list of numbers, we have to return the number whose occurrence is 1, if no such element is present, then return -1. So if the list is like [5, 2, 3, 6, 5, 2, 9, 6, 3], then the output will be 9.To solve this, we will follow these steps −We will check each element, and put the elements inside the map, so if the element is not in map, then put a new entry, otherwise increase the valuethen go through the map, when the value is 1, return the key.Example(Python)Let us see the following implementation ... Read More

Remove Vowels from a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:41:54

2K+ Views

Suppose we have a string, we have to remove all vowels from that string. So if the string is like “iloveprogramming”, then after removing vowels, the result will be − "lvprgrmmng"To solve this, we will follow these steps −create one array vowel, that is holding [a, e, i, o, u]for v in a vowelreplace v using blank stringExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def removeVowels(self, s):       s = s.replace("a", "")       s = s.replace("e", "")       s = s.replace("i", "")       s ... Read More

Number of days in a month in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:35:20

7K+ Views

Suppose we have one year Y and a month M, we have to return the number of days of that month for the given year. So if the Y = 1992 and M = 7, then the result will be 31, if the year is 2020, and M = 2, then the result is 29.To solve this, we will follow these steps −if m = 2, thenif y is a leap year, return 29, otherwise 28make an array with elements [1, 3, 5, 7, 8, 10, 12]if m is in the list, then return 31, otherwise, return 30.Example(Python)Let us see ... Read More

Advertisements