Found 26504 Articles for Server Side Programming

Program to find sum of digits in base K using Python

Arnab Chakraborty
Updated on 29-May-2021 14:15:44

714 Views

Suppose we have a number n in decimal number system (base 10) have another value k, we have to find the sum of the digits of n after converting given number n from base 10 to base k. When we calculate digit sum, we will consider each digit as decimal (base 10) number.So, if the input is like n = 985 k = 8, then the output will be 12 because the number 985 in octal is 1731, so the digit sum is 1+7+3+1 = 12.To solve this, we will follow these steps −ans := 0while n >= k, doans ... Read More

Program to find minimum operations to make the array increasing using Python

Arnab Chakraborty
Updated on 29-May-2021 14:16:17

494 Views

Suppose we have an array nums. In one operation, we can select one element of the array and increase it by 1. For example, if we have [4, 5, 6], we can select element at index 1 to make the array [4, 5, 5]. Then we have to find the minimum number of operations required to make nums strictly increasing.So, if the input is like nums = [8, 5, 7], then the output will be 7, because we need to increase like [8, 6, 7], [8, 7, 7], [8, 8, 7], [8, 9, 7], [8, 9, 8], [8, 9, 9], ... Read More

Program to find sign of the product of an array using Python

Arnab Chakraborty
Updated on 29-May-2021 14:16:31

232 Views

Suppose we have an array called nums. We have to find sign of the multiplication result of all elements present in the array.So, if the input is like nums = [-2, 3, 6, -9, 2, -4], then the output will be Negative, as the multiplication result is -2592To solve this, we will follow these steps −zeroes := 0, negatives := 0for each i in nums, doif i is same as 0, thenzeroes := zeroes + 1if i < 0, thennegatives := negatives + 1if zeroes > 0 , thenreturn "Zero"otherwise when negatives mod 2 is same as 0, thenreturn "Positive"otherwise, ... Read More

Program to find k partitions after truncating sentence using Python

Arnab Chakraborty
Updated on 29-May-2021 14:16:46

114 Views

Suppose we have sentence s where some English words are present, that are separated by a single space with no leading or trailing spaces. We also have another value k. We have to find only the first k words after truncating.So, if the input is like s = "Coding challenges are really helpful for students" k = 5, then the output will be True (See the image)To solve this, we will follow these steps −words := split s by spacesjoin first k letters from words array by separating spaces and returnLet us see the following implementation to get better understanding ... Read More

Program to determine color of a chessboard square using Python

Arnab Chakraborty
Updated on 29-May-2021 14:17:01

2K+ Views

Suppose we have a chessboard coordinate, that is a string that represents the coordinates of row and column of the chessboard. Below is a chessboard for your reference.We have to check whether given cell is white or not, if white return true, otherwise return false.So, if the input is like coordinate = "f5", then the output will be True (See the image)To solve this, we will follow these steps −if ASCII of coordinate[0] mod 2 is same coordinate[1]) mod 2, thenreturn Falseotherwise, return TrueLet us see the following implementation to get better understanding −Example Live Demodef solve(coordinate):    if (ord(coordinate[0]))%2 == ... Read More

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

Arnab Chakraborty
Updated on 29-May-2021 14:17:19

402 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

Program to find maximum ascending subarray sum using Python

Arnab Chakraborty
Updated on 29-May-2021 14:30:50

626 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 −Example Live Demodef 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 second largest digit in a string using Python

Arnab Chakraborty
Updated on 29-May-2021 14:31:05

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

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

Arnab Chakraborty
Updated on 29-May-2021 14:31:18

478 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 check if binary string has at most one segment of ones or not using Python

Arnab Chakraborty
Updated on 29-May-2021 14:31:34

490 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

Advertisements