Server Side Programming Articles - Page 1988 of 2650

Python Program to Count set bits in an integer

Pavitra
Updated on 20-Dec-2019 07:22:44

462 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an integer n, we need to count the number of 1’s in the binary representation of the numberNow let’s observe the solution in the implementation below −#naive approachExample Live Demo# count the bits def count(n):    count = 0    while (n):       count += n & 1       n >>= 1    return count # main n = 15 print("The number of bits :", count(n))OutputThe number of bits : 4#recursive approachExample Live Demo# recursive way def count( ... Read More

Python Program to Count number of binary strings without consecutive 1’

Pavitra
Updated on 20-Dec-2019 07:20:06

299 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a positive integer N, we need to count all possible distinct binary strings available with length N such that no consecutive 1’s exist in the string.Now let’s observe the solution in the implementation below −Example Live Demo# count the number of strings def countStrings(n):    a=[0 for i in range(n)]    b=[0 for i in range(n)]    a[0] = b[0] = 1    for i in range(1, n):       a[i] = a[i-1] + b[i-1]       b[i] = ... Read More

Find frequency of each word in a string in Python

Pradeep Elance
Updated on 20-Dec-2019 07:19:17

11K+ Views

As a part of text analytics, we frequently need to count words and assign weightage to them for processing in various algorithms, so in this article we will see how we can find the frequency of each word in a given sentence. We can do it with three approaches as shown below.Using CounterWe can use the Counter() from collections module to get the frequency of the words. Here we first apply the split() to generate the words from the line and then apply the most_common ().Example Live Demofrom collections import Counter line_text = "Learn and practice and learn to practice" freq ... Read More

Python Program to Count Inversions in an array

Pavitra
Updated on 20-Dec-2019 07:17:40

276 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to count the inversion required and display it.Inversion count is obtained by counting how many steps are needed for the array to be sorted.Now let’s observe the solution in the implementation below −Example Live Demo# count def InvCount(arr, n):    inv_count = 0    for i in range(n):       for j in range(i + 1, n):          if (arr[i] > arr[j]):             inv_count += 1    return ... Read More

Find Circles in an Image using OpenCV in Python

SaiKrishna Tavva
Updated on 09-Oct-2024 12:16:01

7K+ Views

The OpenCV platform provides a cv2 library for Python. This can be used for various shape analyses which is useful in Computer Vision. To identify the shape of a circle using OpenCV we can use the cv2.HoughCircles() function. It finds circles in a grayscale image using the Hough transform. Common Approaches Some of the common methods to find circles in an image using OpenCV are as follows - Circle detection using Hough transform OpenCV ... Read More

Python Program for Triangular Matchstick Number

Pavitra
Updated on 20-Dec-2019 07:15:18

168 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number X which represents the floor of a matchstick pyramid, we need to display the total number of matchstick required to form a pyramid of matchsticks with x floors.Now let’s observe the solution in the implementation below −Example Live Demo#function def numberOfSticks(x):    return (3 * x * (x + 1)) / 2 # main() n=21 a=numberOfSticks(n) print(int(a))Output693All the variables are declared in the local scope and their references are seen in the figure above.ConclusionIn this article, we have learned ... Read More

Find and Draw Contours using OpenCV in Python

Pradeep Elance
Updated on 20-Dec-2019 07:13:13

3K+ Views

For the purpose of image analysis we use the Opencv (Open Source Computer Vision Library) python library. The library name that has to be imported after installing opencv is cv2.In the below example we find the contours present in an image files. Contours help us identify the shapes present in an image. Contours are defined as the line joining all the points along the boundary of an image that are having the same intensity. The findContours function in OPenCV helps us identify the contours. Similarly the drawContours function help us draw the contours. Below is the syntax of both of ... Read More

Facebook Login using Python

Pradeep Elance
Updated on 20-Dec-2019 07:02:58

1K+ Views

We can use the python package called selenium to automate the interaction with webdrivers. In this article we will see the interaction between python’s selenium package and logging in to Facebook.ApproachSelenium package is used to automate and controls web browsers activity. Out python code will need the selenium package to be installed and also a driver software known as geckodriver to be available for the program. Below are the steps to achieve this.Step-1Install selenium in you python environmentpip install seleniumStep-2Download the geckodriver from this link. Place it in the same directory where we are going to have this python script.Next we ... Read More

Python Program for Subset Sum Problem

Pavitra
Updated on 20-Dec-2019 06:59:44

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a set of non-negative integers in an array, and a value sum, we need to determine if there exists a subset of the given set with a sum equal to a given sum.Now let’s observe the solution in the implementation below −# Naive approachExampledef SubsetSum(set, n, sum) :    # Base Cases    if (sum == 0) :       return True    if (n == 0 and sum != 0) :       return False    # ... Read More

Dictionary Methods in Python (cmp(), len(), items()…)

Pradeep Elance
Updated on 20-Dec-2019 06:58:57

2K+ Views

Dictionary in python is one of the most frequently used collection data type. It is represented by hey value pairs. Keys are indexed but values may not be. There are many python-built in functions that make using the dictionary very easy in various python programs. In this topic we will see the three in-built methods namely cmp(), len() and items().cmp()The method cmp() compares two dictionaries based on key and values. It is helpful in identifying duplicate dictionaries as well as doing a relational comparison among the dictionaries. It is a feature on only python2 and not available in python 3.Syntaxcmp(dict1, ... Read More

Advertisements