Found 26504 Articles for Server Side Programming

Create a stopwatch using python

Pradeep Elance
Updated on 19-Dec-2019 11:40:26

1K+ Views

A stopwatch is used to measure the time interval between two events usually in seconds to minutes. It has various usage like in sports or measuring the flow of heat, current etc in an industrial setup. Python can be used to creat a stopwatch by using its tkinter library.This library will have the GUI features to create a stopwatch showing the  Start, Stop and Reset  option. The key component of the program is using the lable.after()  module of tkinter.label.after(parent, ms, function = None) where parent: The object of the widget which is using this function. ms: Time in miliseconds. function: ... Read More

Count set bits using Python List comprehension

Pradeep Elance
Updated on 19-Dec-2019 11:34:21

184 Views

Set bits are the bits representing 1 in the binary form of a number. In this article we will see how to count the number of set bits in a given decimal number.#53 in binary is: 110101 The number of set bits is the number of ones. Here it is 4.In the below program we take the number and convert it to binary. As the binary conversion contains 0b as the first two characters, we remove it using string splitting technique. Then use a for loop to count each bit of the binary number if that value of the digit ... Read More

Find the K-th minimum element from an array concatenated M times in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:35:33

142 Views

Consider we have an array A, and another two integers K and M. We have to find Kth minimum element after concatenating the array to itself M number of times. Suppose the array is like A = [3, 1, 2], K = 4 and M = 3, so after concatenating A, 3 times, it will be [3, 1, 2, 3, 1, 2, 3, 1, 2], the 4th smallest element is 2 here.To solve this problem, we will sort the array A, then return the value, present at index ((K – 1)/M), of the array.Example Live Demo#include #include using namespace std; int ... Read More

Find the Diameter or Longest chord of a Circle in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:25:00

130 Views

Suppose we have radius r is given. We have to find the diameter or longest chord of the circle. If the radius is 9, and diameter will be 18. This task is extremely simple, we have to find the 2*r, that is the diameter of the circle.Example Live Demo#include using namespace std; int getDiameter(int r) {    return 2*r; } int main() {    int r = 9;    cout

Find the common nodes in two singly linked list in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:23:36

442 Views

Suppose we have two singly-linked lists. We have to find the total number of common nodes in both the singly linked list. So if two lists are like [15, 16, 10, 9, 7, 17], and [15, 16, 40, 6, 9], there are three common nodes.Traverse both lists up to end of the list using two nested loops, for every node in the list, check if it is matched with any node of the second list or not. If a match is found, then increase the counter, and finally return the count.Example Live Demo#include using namespace std; class Node {    public: ... Read More

Binary list to integer in Python

Pradeep Elance
Updated on 19-Dec-2019 11:14:51

2K+ Views

We can convert a list of 0s and 1s representing a binary number to a decimal number in python using various approaches. In the below examples we use the int() method as well as bitwise left shift operator.Using int()The int() method takes in two arguments and changes the base of the input as per the below syntax.int(x, base=10) Return an integer object constructed from a number or string x.In the below example we use the int() method to take each element of the list as a string and join them to form a final string which gets converted to integer ... Read More

Find the closest leaf in a Binary Tree in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:20:59

330 Views

Suppose, one binary tree is given. It has leaf nodes at different levels. Another pointer is given, that is pointing to a node. We have to find the distance to the nearest leaf node from the pointed node. Consider the tree is like below −Here leaf nodes are 2, -2 and 6. If the pointer is pointing to node -5, The nearest nodes from -5 will be at distance 1.To solve this, we will traverse the subtree rooted with the given node, and find the closest leaf in the subtree, then store the distance. Now traversing tree starting from the ... Read More

Find the altitude and area of an isosceles triangle in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:06:00

321 Views

Consider we have the side of the isosceles triangle, our task is to find the area of it and the altitude. In this type of triangle, two sides are equal. Suppose the sides of the triangle are 2, 2 and 3, then altitude is 1.32 and the area is 1.98.Altitude(h)=$$\sqrt{a^{2}-\frac{b^{2}}{2}}$$Area(A)=$\frac{1}{2}*b*h$Example Live Demo#include #include using namespace std; float getAltitude(float a, float b) {    return sqrt(pow(a, 2) - (pow(b, 2) / 4)); } float getArea(float b, float h) {    return (1 * b * h) / 2; } int main() {    float a = 2, b = 3;    cout

Program to print an inverse pyramid character pattern in C++

Ayush Gupta
Updated on 19-Dec-2019 10:50:43

276 Views

In this tutorial, we will be discussing a program to print an inverse pyramid character pattern.For this we will be provided with the number of rows containing in the inverted pyramid triangle. Our task is to print the alphabets in the given number of rows to develop the shape of an inverse pyramid.Example Live Demo#include using namespace std; //printing the inverse pyramid pattern void inv_pyramid(int n){    int i, j, num, gap;    for (i = n; i >= 1; i--) {       for (gap = n - 1; gap >= i; gap--) {          cout

Find Tangent at a given point on the curve in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:54:19

359 Views

Suppose we have a curve like y = x(A - x), we have to find the tangent at a given point (x, y) on that curve. Here A is an integer number, x and y are also integers.To solve this, we have the check that the given point is on the curve or not, if so, then find the differentiation of that curve, so it will be −$$\frac{\text{d}y}{\text{d}x}=A-2x$$Then put x and y into the dy/dx, then find the tangent using this equation −$$Y-y=-\lgroup\frac{\text{d}y}{\text{d}x}\rgroup*\lgroup X-x \rgroup$$Example Live Demo#include using namespace std; void getTangent(int A, int x, int y) {    int differentiation ... Read More

Advertisements