Found 33676 Articles for Programming

Program to check three consecutive odds are present or not in Python

Arnab Chakraborty
Updated on 17-May-2021 12:29:21

650 Views

Suppose we have an array called nums, we have to check whether there are three consecutive odd numbers in nums or not.So, if the input is like nums = [18, 15, 2, 19, 3, 11, 17, 25, 20], then the output will be True as there are three consecutive odds [3, 11, 17].To solve this, we will follow these steps −length:= size of numsif length is same as 1 or length is same as 2, thenreturn Falseotherwise, for i in range 0 to size of nums - 3, doif nums[i], nums[i+1] and nums[i+2] all are odds, thenreturn Truereturn FalseExample (Python)Let ... Read More

Program to find a good string from a given string in Python

Arnab Chakraborty
Updated on 17-May-2021 12:30:33

1K+ Views

Suppose we have a string s with lower and upper case English letters. We shall consider a string is a good string which does not have any two adjacent characters s[i] and s[i + 1] where −0

Program to find kth missing positive number in an array in Python

Arnab Chakraborty
Updated on 17-May-2021 12:30:13

617 Views

Suppose we have an array called nums with positive sorted strictly increasing values, and also have an integer k. We have to find the kth positive integer that is missing from this array.So, if the input is like nums = [1, 2, 4, 8, 12], k = 6, then the output will be 10 because the missing numbers are [3, 5, 6, 7, 9, 10, 11], here the 6th term is 10.To solve this, we will follow these steps −nums := a new set from the elements present in numscount := 0num := 1while count < k, doif num is ... Read More

Program to find number of good triplets in Python

Arnab Chakraborty
Updated on 17-May-2021 12:29:49

2K+ Views

Suppose we have an array nums, and three different integers a, b and c. We have to find the number of good triplets. A triplet (nums[i], nums[j], nums[k]) is said to be a good triplet if the following conditions are true −0

Program to shuffle string with given indices in Python

Arnab Chakraborty
Updated on 17-May-2021 12:28:54

1K+ Views

Suppose we have a string s and a list of indices ind, they are of same length. The string s will be shuffled such that the character at the position i, moves to indices[i] in the final string. We have to find the final string.So, if the input is like s = "ktoalak" ind = [0, 5, 1, 6, 2, 4, 3], then the output will be "kolkata"To solve this, we will follow these steps −fin_str := a list whose size is same as s and fill with 0for each index i and character v in s, dofin_str[ind[i]] := vjoin ... Read More

Program to count odd numbers in an interval range using Python

Arnab Chakraborty
Updated on 17-May-2021 12:23:37

1K+ Views

Suppose we have two non-negative numbers left and right. We have to find the number of odd numbers between left and right (inclusive).So, if the input is like left = 3, right = 15, then the output will be 7 because there are 7 odd numbers in range, these are [3, 5, 7, 9, 11, 13, 15], there are 7 elements.To solve this, we will follow these steps −if left is odd or right is odd, thenreturn 1 + quotient of (right-left) / 2otherwise, return quotient of (right-left) / 2Example (Python)Let us see the following implementation to get better understanding ... Read More

Program to find maximum how many water bottles we can drink in Python

Arnab Chakraborty
Updated on 17-May-2021 12:23:17

702 Views

Suppose there are n number of full water bottles, we can exchange m empty water bottles for only one full water bottle. Now drinking a full water bottle makes it an empty bottle. We have to find the maximum number of water bottles we can drink.So, if the input is like n = 9, m = 3, then the output will be 13 because initially we have 9 bottles, so after drinking all bottles, we can get 9/3 = 3 full bottles, after drinking them all we have three empty bottles and using them we can buy one and drink ... Read More

Program to find number of good pairs in Python

Arnab Chakraborty
Updated on 17-May-2021 12:22:53

4K+ Views

Suppose we have an array nums. Here a pair (i, j) is said to be a good pair if nums[i] is same as nums[j] and i < j. We have to count the number of good pairs.So, if the input is like nums = [5, 6, 7, 5, 5, 7], then the output will be 4 as there are 4 good pairs the indices are (0, 3), (0, 4) (3, 4), (2, 5)To solve this, we will follow these steps −count:= 0n:= size of numsfor i in range 0 to n - 1, dofor j in range i+1 to n ... Read More

Program to reformat date in YYYY-MM-DD format using Python

Arnab Chakraborty
Updated on 17-May-2021 12:22:29

2K+ Views

Suppose we have a date string in the format "Day Month Year" format where day's are like [1st, 2nd, ..., 30th, 31st], months are in [Jan, Feb, ... Nov, Dec] format and year is a four-digit numeric value in range 1900 to 2100, we have to convert this date into "YYYY-MM-DD" format.So, if the input is like date = "23rd Jan 2021", then the output will be 2021-01-23To solve this, we will follow these steps −Months:= ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]string:= split the date and form a list like [day, month, year] formatyear ... Read More

Program to check we can make arithmetic progression from sequence in Python

Arnab Chakraborty
Updated on 17-May-2021 12:22:01

2K+ Views

Suppose we have a list of numbers called nums. We have to check whether the elements present in nums are forming AP series or not. As we know in AP (Arithmetic Progression) series the common difference between any two consecutive elements is the same.So, if the input is like nums = [9, 1, 17, 5, 13], then the output will be True because if we sort them, it will be [1, 5, 9, 13, 17] and here common difference is 4 for each pair of elements.To solve this, we will follow these steps −nums := sort the list numsif number ... Read More

Advertisements