Found 33676 Articles for Programming

Last Substring in Lexicographical Order in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:24:58

302 Views

Suppose we have s as string, we have to find the last substring of s in lexicographic order.So, if the input is like "abbbcabbc", then the output will be "cabbc"To solve this, we will follow these steps −i := 0, j := 1, k := 0while j + k < size of s, do &minsu;if s[i + k] is same as s[j + k], then −(increase k by 1)Ignore following part, skip to the next iterationif s[i + k] < s[j + k], then −i := j(increase j by 1)Otherwisej := j + k + 1k := 0return substring of ... Read More

Count occurrence of all elements of list in a tuple in Python

Pradeep Elance
Updated on 04-Jun-2020 11:25:26

452 Views

We have a list and tuple. We match the elements of the list with the elements of the tuple and account the number of elements in the table matching with the elements of the list.With CounterWe use the counter function from collections to get the count of every element in the tuple. Again design a for and in condition find those elements which are present in the the list and part of the counting result from the tuple.Example Live Demofrom collections import Counter Atup = ('Mon', 'Wed', 'Mon', 'Tue', 'Thu') Alist = ['Mon', 'Thu'] # Given Tuple and list print("Given tuple ... Read More

Longest Chunked Palindrome Decomposition in python

Arnab Chakraborty
Updated on 04-Jun-2020 11:23:32

293 Views

Suppose we have a text. We have to find the largest possible k such that there exists a[1], a[2], ..., a[k] such that: Each a[i] is a non-empty string; Their concatenation a[1] + a[2] + ... + a[k] is equal to the given text; For all i in range 1 to k, a[i] = a[{k+1 - i}].So, if the input is like "antaprezatepzapreanta", then the output will be 11, because we can split it like "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)".To solve this, we will follow these steps −start := 0, end := length of text - 1initialize temp1 and temp2 with empty stringsans = ... Read More

Count number of items in a dictionary value that is a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:23:15

1K+ Views

We are given a Dictionary in which the values from the key value pair itself is a list. In this article we will see a how to count the number of items in this list which are present as values in the dictionary.With isinstanceHindi suppose we use isinstance function to find out if the value of the dictionary is a list. Then we increment a count variable whenever isinstance returns true.Example Live Demo# defining the dictionary Adict = {'Days': ["Mon", "Tue", "wed", "Thu"],    'time': "2 pm",    'Subjects':["Phy", "Chem", "Maths", "Bio"]    } print("Given dictionary:", Adict) count = 0 # ... Read More

Smallest Sufficient Team in Pyhton

Arnab Chakraborty
Updated on 04-Jun-2020 11:20:34

335 Views

Suppose for a project we have a list of required skills called req_skills, and a list of people. Here i-th people people[i] contains a list of skills that person has.Now suppose a sufficient team is defined as a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: As an example suppose team is [0, 1, 3] this represents the people with skills people[0], people[1], and people[3].We have to find the team of the smallest ... Read More

Count and display vowels in a string in Python

Pradeep Elance
Updated on 04-Jun-2020 11:19:57

1K+ Views

Given a string of characters let's analyse how many of the characters are vowels.With setWe first find out all the individual and unique characters and then test if they are present in the string representing the vowels.Example Live DemostringA = "Tutorialspoint is best" print("Given String: ", stringA) vowels = "AaEeIiOoUu" # Get vowels res = set([each for each in stringA if each in vowels]) print("The vlowels present in the string: ", res)OutputRunning the above code gives us the following result −Given String: Tutorialspoint is best The vlowels present in the string: {'e', 'i', 'a', 'o', 'u'}with fromkeysThis function enables to extract ... Read More

Converting list string to dictionary in Python

Pradeep Elance
Updated on 04-Jun-2020 11:18:14

1K+ Views

Here we have a scenario where if string is presented which has elements in it making it a list. But those elements can also represent a key-value pair making it dictionary. In this article we will see how to take such a list string and make it a dictionary.With split and slicingIn this approach we use the split function to separate the elements as key value pair and also use slicing to convert the key value pairs into a dictionary format.Example Live DemostringA = '[Mon:3, Tue:5, Fri:11]' # Given string print("Given string : ", stringA) # Type check print(type(stringA)) # using ... Read More

Converting all strings in list to integers in Python

Pradeep Elance
Updated on 04-Jun-2020 11:16:45

2K+ Views

Sometimes we can have a list containing strings but the strings themselves are numbers and closing quotes. In such a list we want to convert the string elements into actual integers.With int()The int function takes in parameters and converts it to integers if it is already a number. So we design a for loop to go through each element of the list and apply the in function. We store the final result into a new list.Example Live DemolistA = ['5', '2', '-43', '23'] # Given list print("Given list with strings : ", listA) # using int res = [int(i) for i ... Read More

Parsing A Boolean Expression in Python

Arnab Chakraborty
Updated on 04-Jun-2020 11:18:09

1K+ Views

Suppose we have a boolean expression, we have to find the result after evaluating that expression.An expression can either be −"t", evaluating to True;"f", evaluating to False;"!(expression)", evaluating to the logical NOT of the inner expression;"&(expr1, expr2, ...)", evaluating to the logical AND of 2 or more inner expressions;"|(expr1, expr2, ...)", evaluating to the logical OR of 2 or more inner expressions;So, if the input is like "|(!(t), &(t, f, t))", then the output will be fasle, this is because !(t) is false, then &(t, f, t) is also false, so the OR of all false values will be false.To ... Read More

Convert two lists into a dictionary in Python

Pradeep Elance
Updated on 04-Jun-2020 11:15:54

5K+ Views

While a Python list contains a series of values a dictionary on the other hand contains a pair of values which are called key-value pairs. In this article we will take two lists and mark them together to create a Python dictionary.With for and removeWe create two nested for loops. In the inner loop will assign one of the list as key for the dictionary while keep removing the values from the list which is outer for loop.Example Live DemolistK = ["Mon", "Tue", "Wed"] listV = [3, 6, 5] # Given lists print("List of K : ", listK) print("list of V ... Read More

Advertisements