Find Duplicate Element from N+1 Numbers in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:31:30

284 Views

Suppose we have a list of numbers called nums of length n + 1. These numbers are picked from range 1, 2, ..., n. As we know, using the pigeonhole principle, there must be a duplicate. We have to find that and return it.So, if the input is like [2, 1, 4, 3, 3], then the output will be 3To solve this, we will follow these steps −l := size of numstemp := l*(l-1) /2temp_sum := sum of all elements in numsreturn (temp_sum - temp)Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, ... Read More

Find All Words Sharing Same First Letters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:29:41

420 Views

Suppose we have a list of words in lowercase letters, we have to find the length of the longest contiguous sublist where all words have the same first letter.So, if the input is like ["she", "sells", "seashells", "on", "the", "seashore"], then the output will be 3 as three contiguous words are "she", "sells", "seashells", all have same first letter 's'.To solve this, we will follow these steps −maxlength := 0curr_letter := Null, curr_length := 0for each word in words, doif curr_letter is null or curr_letter is not same as word[0], thenmaxlength := maximum of maxlength, curr_lengthcurr_letter := word[0], curr_length := ... Read More

Check Programmers Convention Arrangements in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:28:09

116 Views

Suppose we have a number n, this is representing programmers looking to enter a convention, and we also have a list of number, convention 1 represents a programmer and 0 represents empty space. Now the condition is no two programmers can sit next to each other, we have to check whether all n programmers can enter the convention or not.So, if the input is like n = 2, convention = [0, 0, 1, 0, 0, 0, 1], then the output will be TrueTo solve this, we will follow these steps −for i in range 0 to size of conv, doa:= ... Read More

Find Formatted Amount of Cents in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:26:17

405 Views

Suppose we have a positive number n, where n is representing the amount of cents we have, we have to find the formatted currency amount.So, if the input is like n = 123456, then the output will be "1, 234.56".To solve this, we will follow these steps −cents := n as stringif size of cents < 2, thenreturn '0.0' concatenate centsif size of cents is same as 2, thenreturn '0.' concatenate centscurrency := substring of cents except last two digitscents := '.' concatenate last two digitwhile size of currency > 3, docents := ', ' concatenate last three digit of ... Read More

Check Password Criteria in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:24:05

630 Views

Suppose we have a string s, representing a password, we have to check the password criteria. There are few rules, that we have to follow −Password length will be At least 8 characters and at most 20 characters long.Password contains at least one digitPassword contains at least one lowercase character and one uppercase characterPassword contains at least one special character like !"#$%&\'()*+, -./:;?@[\]^_`{|}~Password does not contain any other character like tabs or new lines.So, if the input is like "@bCd12#4", then the output will be True.To solve this, we will follow these steps −a:= 0, b:= 0, c:= 0, d:= ... Read More

Find the Nth Row of Pascal's Triangle in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:21:42

970 Views

Suppose we have a number n, we have to find the nth (0-indexed) row of Pascal's triangle. As we know the Pascal's triangle can be created as follows −In the top row, there is an array of 1.Subsequent row is made by adding the number above and to the left with the number above and to the right.So few rows are as follows −So, if the input is like 4, then the output will be [1, 4, 6, 4, 1]To solve this, we will follow these steps −if n is same as 0, thenreturn [1]if n is same as 1, ... Read More

Find Maximum Number of Balanced Groups of Parentheses in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:16:27

450 Views

Suppose we have a string s that contains balanced parentheses "(" and ")", we have to split them into the maximum number of balanced groups.So, if the input is like "(()())()(())", then the output will be ['(()())', '()', '(())']To solve this, we will follow these steps −temp := blank stringgroups := a new listcount := 0for each character b in s, doif count is same as 0 and size of temp > 0, theninsert temp at the end of groupstemp := blank stringtemp := temp concatenate bif b is same as '(', thencount := count + 1otherwise, count := count ... Read More

Count Palindromes of Size K from Given String Characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:05:34

412 Views

Suppose we have a string s that is representing alphabet characters and a number k. We have to find the number of palindromes where we can construct of length k using only letters in s. And we can use these letters more than once if we want.So, if the input is like s = "xy", k = 4, then the output will be 4 as the palindromes are [xxxx, yyyy, xyyx, yxxy].To solve this, we will follow these steps −n := quotient of k/2x := number of unique characters in sreturn x^(n + k mod 2)Let us see the following ... Read More

Count Valid Pairs with Odd Sum in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:04:00

375 Views

Suppose we have a list of positive numbers nums, we have to find the number of valid pairs of indices (i, j), where i < j, and nums[i] + nums[j] is an odd number.So, if the input is like [5, 4, 6], then the output will be 2, as two pairs are [5, 4] and [5, 6], whose sum are odd.To solve this, we will follow these steps −e := a list by taking only the even numbers in numsreturn (size of nums - size of e) * size of eLet us see the following implementation to get better understanding ... Read More

Check Old and New Version Numbering in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:02:42

176 Views

Suppose we have a strings older and another string newer. These two are representing software package versions in the format "major.minor.patch", we have to check whether the newer version is actually newer than the older one.So, if the input is like older = "7.2.2", newer = "7.3.1", then the output will be TrueTo solve this, we will follow these steps −older := a list of major, minor, patch code of oldernewer:= a list of major, minor, patch code of newerfor i in range the size of list older, do:= older[i], n := newer[i]if n > o, thenreturn Trueotherwise when n ... Read More

Advertisements