Merge Two Dictionaries in Python

Hafeezul Kareem
Updated on 27-Aug-2019 12:48:32

708 Views

In this tutorial, we are going to learn how to combine two dictionaries in Python. Let's see some ways to merge two dictionaries.update() methodFirst, we will see the inbuilt method of dictionary update() to merge. The update() method returns None object and combines two dictionaries into one. Let's see the program.Example## initializing the dictionaries fruits = {"apple": 2, "orange" : 3, "tangerine": 5} dry_fruits = {"cashew": 3, "almond": 4, "pistachio": 6} ## updating the fruits dictionary fruits.update(dry_fruits) ## printing the fruits dictionary ## it contains both the key: value pairs print(fruits)If you run the above program, Output{'apple': 2, 'orange': 3, ... Read More

Binomial Distribution in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:41:00

470 Views

The Binomial Distribution is a discrete probability distribution Pp(n | N) of obtaining n successes out of N Bernoulli trails (having two possible outcomes labeled by x = 0 and x = 1. The x = 1 is success, and x = 0 is failure. Success occurs with probability p, and failure occurs with probability q as q = 1 – p.) So the binomial distribution can be written as$$P_{p}\lgroup n\:\arrowvert\ N\rgroup=\left(\begin{array}{c}N\ n\end{array}\right) p^{n}\lgroup1-p\rgroup^{N-n}$$Example Live Demo#include #include using namespace std; int main(){    const int nrolls = 10000; // number of rolls    const int nstars = 100; // ... Read More

Bernoulli Distribution in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:33:29

455 Views

The Bernoulli Distribution is a discrete distribution having two possible outcomes labeled by x = 0 and x = 1. The x = 1 is success, and x = 0 is failure. Success occurs with probability p, and failure occurs with probability q as q = 1 – p. So$$P\lgroup x\rgroup=\begin{cases}1-p\:for & x = 0\p\:for & x = 0\end{cases}$$This can also be written as −$$P\lgroup x\rgroup=p^{n}\lgroup1-p\rgroup^{1-n}$$Example Live Demo#include #include using namespace std; int main(){    const int nrolls=10000;    default_random_engine generator;    bernoulli_distribution distribution(0.7);    int count=0; // count number of trues    for (int i=0; i

Minimum Spanning Tree in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:29:59

17K+ Views

A spanning tree is a subset of an undirected Graph that has all the vertices connected by minimum number of edges.If all the vertices are connected in a graph, then there exists at least one spanning tree. In a graph, there may exist more than one spanning tree.Minimum Spanning TreeA Minimum Spanning Tree (MST) is a subset of edges of a connected weighted undirected graph that connects all the vertices together with the minimum possible total edge weight. To derive an MST, Prim’s algorithm or Kruskal’s algorithm can be used. Hence, we will discuss Prim’s algorithm in this chapter.As we ... Read More

Applications of DFS and BFS in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:25:37

20K+ Views

Here we will see what are the different applications of DFS and BFS algorithms of a graph?The DFS or Depth First Search is used in different places. Some common uses are −If we perform DFS on unweighted graph, then it will create minimum spanning tree for all pair shortest path treeWe can detect cycles in a graph using DFS. If we get one back-edge during BFS, then there must be one cycle.Using DFS we can find path between two given vertices u and v.We can perform topological sorting is used to scheduling jobs from given dependencies among jobs. Topological sorting ... Read More

Find All Close Matches of Input String from a List in Python

Hafeezul Kareem
Updated on 27-Aug-2019 12:23:17

672 Views

In this tutorial, we are going to find a solution to a problem. Let's see what the problem is. We have a list of strings and an element. We have to find strings from a list in which they must closely match to the given element. See the example.Inputs strings = ["Lion", "Li", "Tiger", "Tig"] element = "Lion" Ouput Lion LiWe can achieve this by using the startswith built-in method. See the steps to find the strings.Initialize string list and a string.Loop through the list.If string from list startswith element or element startswith the string from the listPrint the stringExample## initializing ... Read More

Binary Search Trees in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:13:24

856 Views

The binary search trees are binary tree which has some properties. These properties are like below −Every Binary Search Tree is a binary treeEvery left child will hold lesser value than rootEvery right child will hold greater value than rootIdeal binary search tree will not hold same value twice.Suppose we have one tree like this −This tree is one binary search tree. It follows all of the mentioned properties. If we traverse elements into inorder traversal mode, we can get 5, 8, 10, 15, 16, 20, 23. Let us see one code to understand how we can implement this in ... Read More

Check If All Values in a List Are Greater Than a Given Value in Python

Hafeezul Kareem
Updated on 27-Aug-2019 12:12:11

1K+ Views

In this tutorial, we will check whether all the elements in a list are greater than a number or not. For example, we have a list [1, 2, 3, 4, 5] and a number 0. If every value in the list is greater than the given value then, we return True else False.It's a simple program. We write it in less than 3 minutes. Try it yourself first. If you are not able to find the solution, then, follow the below steps to write the program.Initialise a list and any numberLoop through the list.If yes, return **False**Return True.Example## initializing the list    values ... Read More

Preorder Tree Traversal in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:07:28

532 Views

In this section we will see the pre-order traversal technique (recursive) for binary search tree.Suppose we have one tree like this −The traversal sequence will be like: 10, 5, 8, 16, 15, 20, 23AlgorithmpreorderTraverse(root): Begin    if root is not empty, then       print the value of root       preorderTraversal(left of root)       preorderTraversal(right of root)    end if EndExample Live Demo#include using namespace std; class node{    public:       int h_left, h_right, bf, value;       node *left, *right; }; class tree{    private:       node *get_node(int key);   ... Read More

Level Order Tree Traversal in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 11:34:58

548 Views

In this section we will see the level-order traversal technique for binary search tree.Suppose we have one tree like this −The traversal sequence will be like: 10, 5, 16, 8, 15, 20, 23AlgorithmlevelOrderTraverse(root): Begin    define queue que to store nodes    insert root into the que.    while que is not empty, do       item := item present at front position of queue       print the value of item       if left of the item is not null, then          insert left of item into que       end ... Read More

Advertisements