Found 10784 Articles for Python

Day of the Year in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:54:48

3K+ Views

Suppose, we have a date in the format “YYYY-MM-DD”. We have to return the day number of the year. So if the date is “2019-02-10”, then this is 41st day of the year.To solve this, we will follow these steps −Suppose D is an array of day count like [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]Convert the date into list of year, month and dayif the year is leap year then set date D[2] = 29Add up the day count up to the month mm – 1. and day count after that.ExampleLet us see ... Read More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:45

225 Views

Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0

Relative Sort Array in Python

Arnab Chakraborty
Updated on 18-May-2020 05:54:15

557 Views

Suppose we have two arrays arr1 and arr2, the elements of arr2 are unique, and all elements in arr2 are also present in arr1. We have to sort the elements of arr1 in such a way that the relative ordering of items in arr1 are the same as in arr2. If there are some elements that are not present in arr2, they should be placed at the end of arr1 in ascending order. So if the arr1 is like [2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19], and arr2 is like [2, 1, 4, 3, 9, 6], ... Read More

Defanging an IP Address in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:47:43

780 Views

Suppose we have a valid IPv4 IP address. We have to return the Defanged version of the IP address. A Defanged IP address is basically replace every period “.” by “[.]” So if the IP address is “192.168.4.1”, the output will be “192[.]168[.]4[.]1”To solve this, we will follow these steps −We will split the string using dot, then put each element separated by “[.]”ExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object):    def defangIPaddr(self, address):       address = address.split(".")       return "[.]".join(address) ob1 = Solution() print(ob1.defangIPaddr("192.168.4.1"))Input"192.168.4.1"Output"192[.]168[.]4[.]1"

Distribute Candies to People in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:45:56

1K+ Views

Suppose we want to distribute some number of candies to a row of n people in the following way −We then give 1 candy to the first people, 2 candies to the second people, and so on until we give n candies to the last people.After that, we go back to the start of the row again, give n + 1 candies to the first people, n + 2 candies to the second people, and so on until we give 2 * n candies to the last people.We will repeat this process until we run out of candies. The last ... Read More

Duplicate Zeros in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:43:45

875 Views

Suppose we have a fixed length array of integers, we have to duplicate each occurrence of zero, shifting the remaining elements to the right side.Note that elements beyond the length of the original array are not written.So suppose the array is like [1, 0, 2, 3, 0, 4, 5, 0], then after modification it will be [1, 0, 0, 2, 3, 0, 0, 4]To solve this, we will follow these steps −copy arr into another array arr2, set i and j as 0while i < size of arr −if arr2[i] is zero, thenarr[i] := 0increase i by 1if i < ... Read More

Occurrences After Bigram in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:45:32

102 Views

Suppose there are words given. These are first and second, consider occurrences in some text of the form "first second third", here second comes immediately after the first, and third comes immediately after the second.For each such cases, add "third" into the answer, and show the answer. So if the text is like “lina is a good girl she is a good singer”, first = “a”, second = “good”, the answer will be [girl, singer]To solve this, we will follow these steps −text := split the string by spacesres is an empty listfor i := 0 to size of text ... Read More

Greatest Common Divisor of Strings in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:42:47

2K+ Views

Suppose there are two strings A and B. We can say that A is divisible by B, when A is created by concatenating B one or more times. So if A = “abcabc”, and B = “abc”, then A is divisible by B. In this section, we will see what is the greatest common divisor of a String. So return the largest string that divides both of the strings. So if two strings are “ABABAB”, and “ABAB”, then GCD will be “AB”To solve this, we will follow these steps −temp := shorter string between A and Bm := length of ... Read More

Height Checker in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:40:34

838 Views

Suppose a set of students have to be arranged in non-decreasing order of their heights for a photograph. If we have an array of students, we have to return the minimum number of students that are not present in correct position. So if the array is like [1, 1, 4, 2, 1, 3], then output will be 3. So students with height 4, 3 and the last 1 are not standing in the correct position.To solve this, we will follow these steps −answer := 0let x := Array in sorted formley y := Arrayfor i := 0 to size of ... Read More

Remove All Adjacent Duplicates In String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:38:36

3K+ Views

Suppose we have a string S of lowercase letters; a duplicate removal operation will be performed. This will be done by choosing two adjacent and equal letters, and removing them.We will repeatedly remove duplicates from S until no duplicates are remaining.Return the string after all such duplicate removals have been completed. It is guaranteed that the answer is unique.Suppose the string is “abbacaca”, then answer will be “caca”. At first delete duplicates bb, then string is “aacaca”, then remove aa, then string is “caca”, then no such duplicates are there.To solve this, we will follow these steps −Define an array ... Read More

Advertisements