Python Articles

Page 25 of 852

Program to find largest perimeter triangle using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 618 Views

Suppose we have an array nums of positive lengths, we have to find the largest perimeter of a triangle, by taking three values from that array. When it is impossible to form any triangle of non-zero area, then return 0.So, if the input is like [8,3,6,4,2,5], then the output will be 19.To solve this, we will follow these steps −sort the list numsa := delete last element from numsb := delete last element from numsc := delete last element from numswhile b+c

Read More

Program to find longest nice substring using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 641 Views

Suppose we have a string s. We have to find longest nice substring of s. For a string s, it will be said to nice when, for every letter of the alphabet in s, it appears in uppercase and lowercase both. If there are multiple such substring, then return the substring of the earliest occurrence.So, if the input is like s = "ZbybBbz", then the output will be "bBb" as this contains lowercase and uppercase B's.To solve this, we will follow these steps −cur_max:= -1res:= blank stringfor i in range 0 to size of s, doc := s[i]upper := a ...

Read More

Program to merge strings alternately using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 6K+ Views

Suppose we have two strings s and t. We have to merge them by adding letters in alternating fashion, starting from s. If s and t are not of same length, add the extra letters onto the end of the merged string.So, if the input is like s = "major" t = "general", then the output will be "mgaejnoerral", as t is larger than s, so we have added extra part "ral" at the end.To solve this, we will follow these steps −i := j := 0result := blank stringwhile i < size of s and j < size of ...

Read More

Program to count items matching a rule using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 528 Views

Suppose we have an array nums, where each nums[i] contains three elements [type_i, color_i, name_i]. These are describing the type, color, and name of the ith item. We also have a rule represented by two other strings, ruleKey and ruleValue. Now we can say the ith item is matched the rule if one of the following is true −ruleKey = "type" and ruleValue = type_i.ruleKey = "color" and ruleValue = color_i.ruleKey = "name" and ruleValue = name_i.We have to find number of matching we can find.So, if the input is likeBikeblueElecBCarsilverSumoBikeblueTVSAnd ruleKey = "color", ruleValue = "blue", then the output ...

Read More

Program to find nearest point that has the same x or y coordinate using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have a set of points given in an array called pts. We also have another point (x, y) which is our current location. We are defining a valid point as, a point which shares the same x-coordinate or the same y-coordinate as our current point. We have to return the index of the valid point with the smallest Manhattan distance from our current location (x, y). If there are more than one points, then return the valid point with the smallest index. (Note: the Manhattan distance between two points (a, b) and (p, q) is |a - p| ...

Read More

Program to check if binary string has at most one segment of ones or not using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 543 Views

Suppose we have a binary string s (without leading zeros), We have to check whether s contains at most one contiguous segment of ones or not.So, if the input is like s = "11100", then the output will be True as there is one segment of ones "111".To solve this, we will follow these steps −count := -1if size of s is same as 1, thenreturn Truefor each i in s, doif i is same as "1" and count > -1, thenreturn Falseotherwise when i is same as "0", thencount := count + 1return TrueLet us see the following implementation ...

Read More

Program to check whether one string swap can make strings equal or not using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 526 Views

Suppose we have two strings s and t of same length. Consider an operation where we choose two indices in a string (not necessarily different) and swap the characters at the selected indices. We have to check whether it is possible to make both strings same by performing at most one string swap on exactly one of the strings or not.So, if the input is like s = "hello" t = "hlelo", then the output will be True because we need to swap 'e' and 'l' at either s or t to make them equal.To solve this, we will follow ...

Read More

Program to find second largest digit in a string using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have an alphanumeric string s, we have to find the second largest numerical digit that appears in s, if there is no such string then return -1.So, if the input is like s = "p84t3ho1n", then the output will be 4 as the digits are [1,3,4,8], so second largest digit is 4.To solve this, we will follow these steps −lst := a new setfor each let in s, doif let is not alphabetic, theninsert let as integer in lstif size of lst

Read More

Program to find maximum ascending subarray sum using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 677 Views

Suppose we have an array of positive values called nums, we have to find the maximum possible sum of an ascending subarray in nums. We can say a subarray [nums_l, nums_l+1, ..., nums_r-1, nums_r] is ascending when for all i where l nums[i-1], thentotal := total + nums[i]otherwise, total:= nums[i]if total > max_total, thenmax_total:= totalreturn max_totalLet us see the following implementation to get better understanding −Exampledef solve(nums):    total=nums[0]    max_total=nums[0]    for i in range(1, len(nums)):       if nums[i] > nums[i-1]:          total+=nums[i]       else:          total=nums[i]   ...

Read More

Program to find number of different integers in a string using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 459 Views

Suppose we have a lowercase alphanumeric string s. We shave to replace every non-digit character with a space, but now we are left with some integers that are separated by at least one space. We have to find the number of different integers after performing the replacement operations on s. Here two numbers are considered as different if their decimal representations without any leading zeros are different.So, if the input is like s = "ab12fg012th5er67", then the output will be 3 because, there are few numbers ["12", "012", "5", "67"] now "12" and "012" are different in string but same ...

Read More
Showing 241–250 of 8,519 articles
« Prev 1 23 24 25 26 27 852 Next »
Advertisements