Found 26504 Articles for Server Side Programming

Count number of edges in an undirected graph in C++

Sunidhi Bansal
Updated on 06-Jun-2020 11:23:54

1K+ Views

Given the task is to count the number of edges in an undirected graph. An undirected graph is a set of vertices which are connected together to form a graph, whose all the edges are bidirectional. Undirected graphs can travel in any direction from one node to another connected node.Below is a visual representation of the undirected graph.Now, according to the problem we have to find the number of edges in the undirected graph.Edges in a graph are the lines to which two vertices are joined.Input −insert(graph_list, 0, 1); insert(graph_list, 0, 2); insert(graph_list, 1, 2); insert(graph_list, 1, 4); insert(graph_list, 2, ... Read More

Total Hamming Distance in C++

Arnab Chakraborty
Updated on 05-Jun-2020 07:08:20

825 Views

Suppose we have a list of numbers. We have to find the Hamming distance of all pair of given numbers. We know that the Hamming distance between two integers is the number of positions at which the corresponding bits are different.So, if the input is like [4, 14, 17, 2], then the output will be 17.To solve this, we will follow these steps −m := 1^9 + 7Define a function add(), this will take a, b, return ((a mod m) + (b mod m))Define a function mul(), this will take a, b, return ((a mod m) * (b mod m))Define ... Read More

Minimum Genetic Mutation in C++

Arnab Chakraborty
Updated on 05-Jun-2020 07:01:54

318 Views

Suppose we have a gene string. That can be represented by a string whose length is 8, This string is consists of these letters [A, C, G, T]. Now consider we want to investigate about a mutation, where ONE mutation is actually ONE single character changed in the gene string. As an example, "AACCGTTT" is changed like "AACCGTTA" is 1 mutation.We also have a given gene "bank", where all the valid gene mutations are present. A gene must be in the bank to make it a valid gene string.Now suppose we have given 3 things - start, end, bank, our ... Read More

Get match indices in Python

Pradeep Elance
Updated on 04-Jun-2020 12:27:24

589 Views

Two lists are given. We need to find the index of the elements from the first list whose values match with the elements in the second list.With indexWe simply design follow to get the value of the element in the second list and extract the corresponding index from the first list.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = ['Tue', 'Fri'] # Given lists print("The given list: ", listA) print("The list of values: ", listB) # using indices res = [listA.index(i) for i in listB] # Result print("The Match indices list is : ", res)OutputRunning the above code gives ... Read More

Get last N elements from given list in Python

Pradeep Elance
Updated on 04-Jun-2020 12:25:14

1K+ Views

Given a Python list we want to find out only e e the last few elements.With slicingThe number of positions to be extracted is given. Based on that we design is slicing technique to slice elements from the end of the list by using a negative sign.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Given list print("Given list : ", listA) # initializing N n = 4 # using list slicing res = listA[-n:] # print result print("The last 4 elements of the list are : ", res)OutputRunning the above code gives us the following result −Given list ... Read More

Get key with maximum value in Dictionary in Python

Pradeep Elance
Updated on 04-Jun-2020 12:22:34

772 Views

A Python dictionary contains key value pairs. In this article we will see how to get the key of the element whose value is maximum in the given Python dictionary.With max and getWe use the get function and the max function to get the key.Example Live DemodictA = {"Mon": 3, "Tue": 11, "Wed": 8} print("Given Dictionary:", dictA) # Using max and get MaxKey = max(dictA, key=dictA.get) print("The Key with max value:", MaxKey)OutputRunning the above code gives us the following result −Given Dictionary: {'Mon': 3, 'Tue': 11, 'Wed': 8} The Key with max value: TueWith itemgetter and maxWith itemgetter function we get ... Read More

Get key from value in Dictionary in Python

Pradeep Elance
Updated on 04-Jun-2020 12:19:41

3K+ Views

Python dictionary contains key value pairs. In this article we aim to get the value of the key when we know the value of the element. Ideally the values extracted from the key but here we are doing the reverse.With index and valuesWe use the index and values functions of dictionary collection to achieve this. We design a list to first get the values and then the keys from it.Example Live DemodictA = {"Mon": 3, "Tue": 11, "Wed": 8} # list of keys and values keys = list(dictA.keys()) vals = list(dictA.values()) print(keys[vals.index(11)]) print(keys[vals.index(8)]) # in one-line print(list(dictA.keys())[list(dictA.values()).index(3)])OutputRunning the above code gives ... Read More

Get indices of True values in a binary list in Python

Pradeep Elance
Updated on 04-Jun-2020 12:17:42

785 Views

When a Python list contains values like true or false and 0 or 1 it is called binary list. In this article we will take a binary list and find out the index of the positions where the list element is true.With enumerateThe enumerate function extracts all the elements form the list. We apply a in condition to check of the extracted value is true or not.Example Live DemolistA = [True, False, 1, False, 0, True] # printing original list print("The original list is : ", listA) # using enumerate() res = [i for i, val in enumerate(listA) if val] # ... Read More

Get first index values in tuple of strings in Python

Pradeep Elance
Updated on 04-Jun-2020 12:15:28

356 Views

We have a tuple of strings. We are required to create a list of elements which are the first character of these strings in the tuple.With indexWe design a for loop to take each element and extract the first character by applying the index condition as 0. Then the list function converts it to a list.Example Live DemotupA = ('Mon', 'Tue', 'Wed', 'Fri') # Given tuple print("Given list : " ,tupA) # using index with for loop res = list(sub[0] for sub in tupA) # printing result print("First index charaters:", res)OutputRunning the above code gives us the following result −Given list ... Read More

Get first element with maximum value in list of tuples in Python

Pradeep Elance
Updated on 04-Jun-2020 12:13:20

1K+ Views

We have a list of tuple. We are required to find out that tuple which was maximum value in it. But in case more than one tuple has same value we need the first tuple which has maximum value.With itemgetter and maxWith itemgetter(1) we get all the value from index position 1 and then apply a max function to get the item with max value. But in case more than one result is returned, we apply index zero to get the first tuple with maximum element in it.Example Live Demofrom operator import itemgetter # initializing list listA = [('Mon', 3), ('Tue', ... Read More

Advertisements