Found 33676 Articles for Programming

Program to find average salary excluding the minimum and maximum salary in Python

Arnab Chakraborty
Updated on 17-May-2021 12:14:34

2K+ Views

Suppoe we have an array with distinct elements called salary where salary[i] is the salary of ith employee. We have to find the average salary of employees excluding the minimum and maximum salary.So, if the input is like salary = [8000, 6000, 2000, 8500, 2500, 4000], then the output will be 5125.0, as the minimum and maximum salary values are 2000 and 8500, so excluding them the average salary values are [8000, 6000, 2500, 4000] so the average is (8000 + 6000 + 2500 + 4000)/4 = 5125.To solve this, we will follow these steps −delete minimum of salary from ... Read More

Program to perform XOR operation in an array using Python

Arnab Chakraborty
Updated on 17-May-2021 12:14:00

854 Views

Suppose we have an integer n and another integer start. We have to create an array called nums where nums[i] = start + 2*i (i start from 0) and n is the size of nums. Then find the bitwise XOR of all elements of nums.So, if the input is like n = 6, start = 2, then the output will be 14 because the array will be like [2+2*0, 2+2*1, ... 2+2*5] = [2, 4, 6, 8, 10, 12], then XOR of each element present in the array is 14.To solve this, we will follow these steps −count := startwhile ... Read More

Program to find running sum of 1d array in Python

Arnab Chakraborty
Updated on 17-May-2021 12:13:36

4K+ Views

Suppose we have an array nums. The running sum of an array as rs[i] is sum of all elements from nums[0] to nums[i]. Finally return the entire running sum of nums.So, if the input is like nums = [8, 3, 6, 2, 1, 4, 5], then the output will be [8, 11, 17, 19, 20, 24, 29], becausers[0] = nums[0] rs[1] = sum of nums[0..1] = 8 + 3 = 11 rs[2] = sum of nums[0..2] = 8 + 3 + 6 = 17 and so onTo solve this, we will follow these steps −n:= size of numsrs:= [nums[0]]for i ... Read More

Program to find Final Prices With a Special Discount in a Shop in Python

Arnab Chakraborty
Updated on 17-May-2021 12:13:13

1K+ Views

Suppose we have an array called prices where prices[i] represents price of the ith item in a shop. There is a special offer going on, if we buy the ith item, then we will get a discount equivalent to prices[j] where j is the minimum index such that j > i and price of jth item is less or same as price of ith item (i.e. prices[j] = prices[j], thenprices[i] := prices[i] - prices[j]come out from the loopotherwise, j := j + 1return pricesExample (Python)Let us see the following implementation to get better understanding − Live Demodef solve(prices):    for i ... Read More

Program to delete n nodes after m nodes from a linked list in Python

Arnab Chakraborty
Updated on 17-May-2021 12:12:32

309 Views

Suppose we are given a linked list that has the start node as "head", and two integer numbers m and n. We have to traverse the list and delete some nodes such as the first m nodes are kept in the list and the next n nodes after the first m nodes are deleted. We perform this until we encounter the end of the linked list. We start from the head node, and the modified linked list is to be returned.The linked list structure is given to us as −Node    value :    next : So, if the ... Read More

How to show multiple images in one figure in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:50:21

7K+ Views

To show multiple images in one figure in matplotlib, we can take the following steps −Create random data using numpy.Add a subplot to the current figure, nrows=1, ncols=4 and at index=1.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Blues_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=2.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Accent_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=3.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="terrain_r".Add a subplot to the current figure, nrows=1, ... Read More

How to remove gaps between bars in Matplotlib bar chart?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:47:45

10K+ Views

To remove gaps between bars, we can change the align value to center in the argument of bar() method.StepsCreate a dictionary called data with two keys, milk and water.Get the list of keys and values in the dictionay.Using subplots() method, create a figure and add a set of two subplots.On axis 2, use bar method to plot bars without gaps. Set the width attribute as 1.0. Set the title using set_title() method.Use tight_layout() to adjust the padding between and around the subplots.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = {'milk': 12, 'water': ... Read More

Rotate theta=0 on a Matplotlib polar plot

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:47:22

2K+ Views

To set theta=0 on a matplotlib polar plot, we can take the following steps −Create random theta in the range of 0 to 100; convert them into radian.Using set_theta_zero_location() method, we can set the location of theta to 0.Plot theta_in_rad and data_r using plot() method.Set the title of the plot using title() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import random plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True theta_in_rad = [float(i) * np.pi / 180.0 for i in range(0, 100, 10)] data_r = random.sample(range(70, 90), 10) ax = plt.subplot(111, polar=True) ax.set_theta_zero_location("W") ax.plot(theta_in_rad, data_r, color='r', linewidth=3) ax.set_title("Example", ... Read More

What is the difference between plt.close() and plt.clf() in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:47:02

1K+ Views

plt.figure() - Create a new figure or activate an existing figure.plt.figure().close() - Close a figure window.close() by itself closes the current figureclose(h), where h is a Figure instance, closes that figureclose(num) closes figure number numclose(name), where name is a string, closes the figure with that labelclose('all') closes all the figure windowsplt.figure().clear() - It is the same as clf.plt.cla() - Clear the current axes.plt.clf() - Clear the current figure.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 10) y = np.linspace(1, 2, 10) plt.plot(x, y, c='red') plt.title("First Plot") plt.show() ... Read More

How to add a variable to Python plt.title?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:46:22

12K+ Views

To add a varaible to Python plt.title(), we can take the following steps −Create data points for x and y using numpy and num (is a variable) to calculate y and set this in title.Plot x and y data points using plot() method with red color.Set the title of the curve with variable num.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 10) num = 2 y = num ** x plt.plot(x, y, c='red') plt.title(f"y=%d$^x$" % num) plt.show()OutputRead More

Advertisements