Found 33676 Articles for Programming

Golang program to check if k’th bit is set for a given number or not.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:58:33

2K+ Views

ExamplesConsider n = 20(00010100), k = 3So, the result after turning off the 3rd bit => 00010000 & (1

Golang Program to turn on the k’th bit in a number.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:56:25

187 Views

ExampleFor example consider n = 20(00010100), k = 4. So result after turning on 4th bit => 00010000 | (1

Golang program to turn off the k’th bit in a number.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:54:15

166 Views

ExampleConsider n = 20(00010100), k = 3 The result after turning off the 3rd bit => 00010000 & ^(1 16sApproach to solve this problemStep 1 − Define a method, where n and k would be the arguments, return type is int.Step 2 − Perform AND operation with n & ^(1

Golang program to define a binary tree

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:52:12

643 Views

Example − In the given tree, the root node is 1, the root of its left sub tree is 2, and the root of its right sub tree is 3, ... so on.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − First, we’ll define the node structure.Step 2 − In the main method, we would create the above tree.Step 3 − Finally, we will perform the Preorder Tree Traversal.Example Live Demopackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PreOrderTraversal(){    if ... Read More

Rotating an image using Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:45:51

826 Views

In this program, we will rotate an image using the pillow library. The rotate() function in the Image class takes in angle of rotation.Original ImageAlgorithmStep1: Import Image class from Pillow. Step 2: Open the image. Step 3: Rotate the image. Step 4: Display the output.Example Codefrom PIL import Image im = Image.open('testimage.jpg') im.rotate(45).show()Output

Cropping an image using the Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:46:10

286 Views

In this program, we will crop an image using the Pillow library. We will use the crop() function for the same. The function takes left, top, right, bottom pixel coordinates to crop the image.Original ImageAlgorithmStep 1: Import Image from Pillow. Step 2: Read the image. Step 3: Crop the image using the crop function. Step 4: Display the output.Example Codefrom PIL import Image im = Image.open('testimage.jpg') width, height = im.size left = 5 top = height / 2 right = 164 bottom = 3 * height / 2 im1 = im.crop((left, top, right, bottom)) im1.show()Output

Loading and displaying an image using the Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:46:35

216 Views

In this program, we will read or load an image using the pillow library. The pillow library consists of a method called Image.open(). This function takes the file path or the name of the file as a string. To display the image, we use another function show(). It does not require any parameter.Example Codefrom PIL import Image im = Image.open('testimage.jpg') im.show()Output

How do I format the axis number format to thousands with a comma in Matplotlib?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:47:21

6K+ Views

First, we can make two lists of x and y, where the values will be more than 1000. Then, we can use the ax.yaxis.set_major_formatter method where can pass StrMethodFormatter('{x:, }') method with {x:, } formatter that helps to separate out the 1000 figures from the given set of numbers.StepsMake two lists having numbers greater than 2000.Create fig and ax variables using subplots method, where default nrows and ncols are 1, using subplot() method.Plot line using x and y (from step 1).Set the formatter of the major ticker, using ax.yaxis.set_major_formatter() method, where StrMethodFormatter helps to make 1000 with common, i.e., expression ... Read More

How to overplot a line on a scatter plot in Python?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:48:17

14K+ Views

First, we can create a scatter for different data points using the scatter method, and then, we can plot the lines using the plot method.StepsCreate a new figure, or activate an existing figure with figure size(4, 3), using figure() method.Add an axis to the current figure and make it the current axes, create x using plt.axes().Draw scatter points using scatter() method.Draw line using ax.plot() method.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.To show the plot, use plt.show() method.Exampleimport random import matplotlib.pyplot as plt plt.figure(figsize=(4, 3)) ax = plt.axes() ax.scatter([random.randint(1, 1000) % 50 for i ... Read More

Plot mean and standard deviation in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:49:54

10K+ Views

First, we can calculate the mean and standard deviation of the input data using Pandas dataframe.Then, we could plot the data using Matplotlib.StepsCreate a list and store it in data.Using Pandas, create a data frame with data (step 1), mean, std.Plot using a dataframe.To show the figure, use plt.show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt data = [-5, 1, 8, 7, 2] df = pd.DataFrame({       'data': data,       'mean': [2.6 for i in range(len(data))],       'std': [4.673328578 for i in range(len(data))]}) df.plot() plt.show()Output

Advertisements