Found 33676 Articles for Programming

Swap Consecutive Even Elements in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:33:11

603 Views

Suppose we have a list of numbers called nums, we have to exchange every consecutive even integer with each other.So, if the input is like nums = [4, 5, 6, 8, 10], then the output will be [6, 5, 4, 10, 8]To solve this, we will follow these steps −temp := nullfor i in range 0 to size of nums, doif nums[i] mod 2 is same as 0, thenif temp is not null, thenexchange nums[i], nums[temp]temp := nullotherwise, temp := ireturn numsLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):     ... Read More

Remove Substrings in One Iteration in python

Arnab Chakraborty
Updated on 22-Sep-2020 11:31:32

168 Views

Suppose we have a string s, we have to remove all “y” and “xz” in the string in one iteration.So, if the input is like s = "xyxxzyyxxzx", then the output will be xxxxTo solve this, we will follow these steps −To solve this, we will follow these steps −temp := string after removing xzreturn temp after removing yLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       return s.replace("xz","").replace("y","") ob = Solution() print(ob.solve("xyxxzyyxxzx"))Input"xyxxzyyxxzx"Outputxxxx

Pattern After Double, Reverse, and Swap in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:30:12

187 Views

Suppose we have a number n, we have ot find the nth value from the sequence. The sequence is like below −xxyxxyxxyyxxyxxxyyxyyxyyxyyxyyxyy...To generate the next value, we have to follow these rules, starting with xxy as the first term −When we are at the start of the pattern, double it(concatenate the string with itself).When the last operation was doubling, reverse it.When the last operation was reversing, exchange all xs with ys and vice versa.Repeat these steps.So, if the input is like n = 5, then the output will be "yyxyyxyyxyyx"To solve this, we will follow these steps −i := 0ret ... Read More

Domino Covering Board in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:26:36

241 Views

Suppose we have two numbers n and m representing a board of size n x m. We also have an unlimited number of 1 x 2 dominos. We have to find the maximum number of dominos that can be placed on the board such that they don't overlap and every domino lies completely within the board.So, if the input is like n = 5, m = 3, then the output will be 7To solve this, we will follow these steps −t := n * mreturn quotient of (t / 2)Let us see the following implementation to get better understanding −Example Live ... Read More

Detect Voter Fraud in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:21:02

274 Views

Suppose we have a list of votes, where each element in the list has two elements [c_id, v_id], the c_id is the candidate id and v_id is the voter id. We have to check whether any voter has voted more than once or not.So, if the input is like [[5, 1], [5, 0], [5, 4], [5, 3], [5, 0]], then the output will be True as [5, 0] is present twiceTo solve this, we will follow these steps −make a new set named allfor each vote in votes, doinsert (vote[1]) into allreturn true when size of all is not same ... Read More

Counting Number of Dinosaurs in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:14:18

246 Views

Suppose we have a string called animals and another string called dinosaurs. Every letter in animals represents a different type of animal and every unique character in dinosaurs string represents a different dinosaur. We have to find the total number of dinosaurs in animals.So, if the input is like animals = "xyxzxyZ" dinosaurs = "yZ", then the output will be 3, as there are two types of dinosaurs y and Z, in the animal string there are two y type animal and one Z type animal.To solve this, we will follow these steps −res := 0dinosaurs := a new set ... Read More

Count Elements x and x+1 Present in List in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:12:50

174 Views

Suppose we have a list of numbers called nums, we have to find the number of elements x there are such that x + 1 exists as well.So, if the input is like [2, 3, 3, 4, 8], then the output will be 3To solve this, we will follow these steps −s := make a set by inserting elements present in numscount := 0for each i in nums, doif i+1 in s, thencount := count + 1return countLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):       s = set(nums) ... Read More

Cell Count After Removing Corner Diagonals in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:11:25

132 Views

Suppose we have a number n representing the length of an n x n board. We have to delete all cells that are diagonal to one of the four corners and return the number of empty cells.So, if the input is like n = 4,XOOXOXXOOXXOXOOXThen the output will be 8.To solve this, we will follow this formula −n*n - 2 * n +(n mod 2)Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       return n*n - 2 * n + (n%2) ob = Solution() print(ob.solve(4))Input4Output8

Connell sequence in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:06:14

540 Views

Suppose we have a number n, we have to find the nth term of Connell sequence. The Connell sequence is as follows: 1. Take first odd integer: 1 2. Take next two even integers 2, 4 3. Then take the next three odd integers 5, 7, 9 4. After that take the next four even integers 10, 12, 14, 16 And so on.So, if the input is like 12, then the output will be 21To solve this, we will follow these steps −i := 1while quotient of (i *(i + 1) / 2) < n + 1, doi := i ... Read More

Common Words in Two Strings in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:04:19

6K+ Views

Suppose we have two strings s0 and s1, they are representing a sentence, we have to find the number of unique words that are shared between these two sentences. We have to keep in mind that, the words are case-insensitive so "tom" and "ToM" are the same word.So, if the input is like s0 = "i love python coding", s1 = "coding in python is easy", then the output will be 2 as there are 2 common words, ['python', 'coding']To solve this, we will follow these steps −convert s0 and s1 into lowercases0List := a list of words in s0s1List ... Read More

Advertisements