Programming Articles - Page 955 of 3363

Program to decode a given message in C++

Arnab Chakraborty
Updated on 19-Oct-2021 10:59:55

1K+ Views

Suppose we are given an encoded message that is a string of integer numbers. Now, these integer numbers can be mapped to a specific letter in the alphabet. a is mapped to 1, b is mapped to 2, c is mapped to 3, and so on. There is also a character '*' that can be in the message and that can be mapped to any of the numbers from 1 to 9. So given a message 'input', we have to find out how many ways it can be decoded.So, if the input is like input = "18", then the output ... Read More

Python Pandas - Extract the Number of seconds for each element from TimeDeltaIndex

AmitDiwan
Updated on 19-Oct-2021 10:54:01

194 Views

To extract the Number of seconds for each element from TimeDeltaIndex object, use the TimedeltaIndex.seconds propertyAt first, import the required libraries −import pandas as pdCreate a TimeDeltaIndex object. We have set the timedelta-like data using the 'data' parameter as well −tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 35s 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])Display TimedeltaIndex −print("TimedeltaIndex...", tdIndex)Display the number of seconds from each element of TimeDeltaIndex −print("The number of seconds from the TimeDeltaIndex object...", tdIndex.seconds)ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have set the timedelta-like data using the ... Read More

Program to find out the cost to merge an array of integers into a single value in C++

Arnab Chakraborty
Updated on 19-Oct-2021 10:59:29

168 Views

Suppose we are given an array arr that contains n positive integer numbers. We are also given an integer number j. The task we have to perform is to merge j numbers into a single number by adding them. The cost of merging is equal to the addition of the j numbers we have selected. We have to find out the minimum possible cost for this merging operation.So, if the input is like arr = [2, 5, 6, 2, 3, 1, 3], j = 4, then the output will be 31.Cost to merge 2, 3, 1, 3 is equal to ... Read More

Program to find length of longest repeating substring in a string in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:46:49

3K+ Views

Suppose we have a lowercase string s, we have to find the length of the longest substring that occurs at least twice in s. If we cannot find such string, return 0.So, if the input is like s = "abdgoalputabdtypeabd", then the output will be 3, because the longest substring that occurs more than once is "abd".To solve this, we will follow these steps −Define a function lcs() . This will take s1, s2n := minimum of size of s1 and size of s2for i in range 0 to n - 1, doif s1[i] is not same as s2[i], thenreturn ... Read More

Program to find length longest prefix sequence of a word array in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:43:22

253 Views

Suppose we have a list of words called w, with lowercase strings. We have to find the length of the longest sequence of w where each previous word is the prefix of the next word and the next word has just one new character appended.So, if the input is like w = ["pqr", "pq", "m", "mn", "pqrs"], then the output will be 3 because we can get the sequence: ["pq", "pqr", "pqrs"], whose length is 3.To solve this, we will follow these steps −sort the list wdp := a map, where default value for a key is 0res := 0for ... Read More

Program to find length of longest matrix path length in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:40:59

316 Views

Suppose we have a binary matrix, where 0 indicates empty cell and 1 indicates wall. We can start at any empty cell on first row and want to end up on any empty cell on the last row. We can move left, right, or down, we have to find the longest such path where we can visit each cell at most once. If this is not possible, then return 0.So, if the input is like000000010000then the output will be 10, as We can move (0, 3), (0, 2), (0, 1), (0, 0), (1, 0), (1, 1), (1, 2), (2, 2), ... Read More

Program to find length of longest contiguously strictly increasing sublist after removal in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:37:33

557 Views

Suppose we have a list of numbers called nums, we have to find the maximum length of a contiguous strictly increasing sublist. We are allowed to remove at most single element from the list.So, if the input is like nums = [35, 5, 6, 7, 8, 9, 12, 11, 26], then the output will be 7, because if we remove 12 from nums, the list will be [5, 6, 7, 8, 9, 11, 26], the length is 7, this is the longest, contiguous, strictly increasing sub-list.To solve this, we will follow these steps −if nums is empty, thenreturn 0end := ... Read More

Program to find length of longest consecutively increasing substring in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:34:44

258 Views

Suppose we have a lowercase string s. This contains English letters as well as "?" Symbol. For each "?" we must either remove it or replace it with any lowercase letter. We have to find the length of the longest consecutively increasing substring that starts with letter "a".So, if the input is like s = "vta???defke", then the output will be 6, as we can turn s into "vtabcdefke" and "abcdef" is the longest consecutively increasing substring, and this is also starting with "a".To solve this, we will follow these steps −maxlen := 0length := 0qmarks := 0for each c ... Read More

Program to find length of longest consecutive sublist with unique elements in Python

Arnab Chakraborty
Updated on 19-Oct-2021 13:43:10

388 Views

Suppose we have a list of numbers called nums, where all elements are unique. We have to find the length of the longest sublist that contains consecutive elements.So, if the input is like nums = [3, 6, 7, 5, 4, 9], then the output will be 5, because the sublist is [3, 6, 7, 5, 4] this contains all consecutive elements from 3 to 7.To solve this, we will follow these steps −ret := 0for i in range 0 to size of nums - 1, dolhs := nums[i]rhs := nums[i]for j in range i to size of nums - 1, ... Read More

Program to find longest consecutive run of 1s in binary form of n in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:29:27

752 Views

Suppose we have a non-negative value n, we have to find the length of the longest consecutive run of 1s in its binary representation.So, if the input is like n = 1469, then the output will be 4, because binary representation of 156 is "10110111101", so there are four consecutive 1sTo solve this, we will follow these steps −count := 0while n is not same as 0, don := n AND (n after shifting one bit to the left)count := count + 1return countExampleLet us see the following implementation to get better understanding −def solve(n):    count = 0    while n != 0:       n = n & (n

Advertisements