Server Side Programming Articles - Page 877 of 2650

Program to add one to a number that is shown as a digit list in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:21:37

728 Views

Suppose we have an array called nums, containing decimal digits of a number. For example, [2, 5, 6] is for 256. We have to add 1 with this number and return the list in same format as before.So, if the input is like nums = [2, 6, 9], then the output will be [2, 7, 0].To solve this, we will follow these steps −i := size of nums - 1while i >= 0, doif nums[i] + 1 = 0:       if nums[i] + 1

Program to count number of 5-star reviews required to reach threshold percentage in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:19:05

803 Views

Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent.So, if the input is like reviews = [[3, 4], [1, 2], [4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% 5-star reviews, ... Read More

Program to find number of different substrings of a string for different queries in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:26:59

310 Views

Suppose we have a string s whose length is n. We also have a list of queries Q, where Q[i] contains a pair (l, r). For each query we have to count number of different substrings of s in the inclusive range between l and r.So, if the input is like s = "ppqpp" Q = [(1, 1), (1, 4), (1, 1), (0, 2)], then the output will be [1, 8, 1, 5] becauseFor query (1, 1) the only substring is 'p' so output is 1For query (1, 4) the substrings are 'p', 'q', 'pq', 'qp', 'pp', 'pqp', 'qpp' and ... Read More

Program to count number of similar substrings for each query in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:15:35

378 Views

Suppose we have two strings s and a set of query Q. Where Q[i] contains pair (l, r), for each substring of s from l to r, we have to find number of substrings s from x to y where they are similar. Two strings s and t are similar if they follow these rules −They are of same lengthFor each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] is not same as s[j], then t[i] and t[j] must be different.So, if the input is like ... Read More

Program to create a lexically minimal string from two strings in python

Arnab Chakraborty
Updated on 09-Oct-2021 11:29:37

166 Views

Suppose, we have two strings. We want to make a lexically minimum string from those strings. To make the string we compare the first letter of the two strings and extract the lexically smaller letter from one of the strings. In the case of a tie i.e, the letters are the same; we extract the letter from the first string. We repeat this process until both the strings are empty. The minimal string constructed has to be returned.So, if the input is like input_1 = 'TUTORIALS', input_2 = 'POINT', then the output will be POINTTUTORIALSIf we compare the two strings, ... Read More

Program to find out the number of shifts required to sort an array using insertion sort in python

Arnab Chakraborty
Updated on 09-Oct-2021 11:07:25

1K+ Views

Suppose we are given an array and asked to perform insertion sort on it. In insertion sort, each element in an array is shifted to its correct position in the array. We have to find out the total number of shifts required to sort an array. The total number of shifts is an integer number and if the array is already sorted, we return 0.So, if the input is like input_array = [4, 5, 3, 1, 2], then the output will be 8[4, 5, 3, 1, 2] = 0 shifts [4, 5, 3, 1, 2] = 0 shifts ... Read More

C program to sort triangles based on area

Arnab Chakraborty
Updated on 08-Oct-2021 11:26:27

830 Views

Suppose we have an array of different triangles where triangles[i] = [ai, bi, ci] these are the sides of ith triangle. We shall have to sort the triangles based on their area. The area of a triangle by using sides is: square root of p*(p-a)*(p-b)*(p-c) where p = (a+b+c)/2.So, if the input is like (7, 24, 25), (5, 12, 13), (3, 4, 5), then the output will be (3, 4, 5), (5, 12, 13), (7, 24, 25)To solve this, we will follow these steps −Define triangle object with sides a, b and cDefine a function square(), this will take Triangle ... Read More

C program to find sum, max and min with Variadic functions

Arnab Chakraborty
Updated on 08-Oct-2021 11:23:16

2K+ Views

Suppose we want to make some functions that can take multiple arguments, there are no fixed number of arguments. We want to make three functions sum(), max() and min(), they can calculate sum of the numbers, maximum of numbers and minimum of given numbers respectively. Each of these functions will take number of arguments count as their first argument. To define this type of functions we need to use ellipsis (...) three dots into the function argument. To use it we shall have to include stdarg.h header file. This type of function is called variadict functions. To access variable arguments ... Read More

C program to find permutations of given strings

Arnab Chakraborty
Updated on 08-Oct-2021 11:19:50

6K+ Views

Suppose we have few strings in an array. We shall have to find all permutations of them in different line.So, if the input is like strings = ["abc", "def", "ghi"], then the output will beabc def ghi abc ghi def def abc ghi def ghi abc ghi abc def ghi def abcTo solve this, we will follow these steps −Define a function next_permutation(), this will take n, string array s, for initialize i := n - 1, when i > 0, update (decrease i by 1), do:if s[i] > s[i - 1]), then:j := i + 1for j < n, ... Read More

C program to find frequency of each digit in a string

Arnab Chakraborty
Updated on 08-Oct-2021 11:18:19

5K+ Views

Suppose we have a string s. The s contains letters and digits both. We shall have to find frequencies of each digit and display them. To do this we can make an array of size 10 for each digits (0 through 9), initially all elements are 0 inside the array, then when we encounter a digit simply increase the value of that index and finally print them all.So, if the input is like s = "we85abc586wow236h69", then the output will be (Number 2, Freq 1) (Number 3, Freq 1) (Number 5, Freq 2) (Number 6, Freq 3) (Number 8, Freq ... Read More

Advertisements