Python Articles

Page 324 of 855

Program to count number of 5-star reviews required to reach threshold percentage in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 883 Views

Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent. So, if the input is like reviews = [[3, 4], [1, 2], [4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% ...

Read More

Program to count number of similar substrings for each query in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 430 Views

Finding similar substrings based on character pattern matching is a common string processing problem. Two substrings are similar if they have the same length and maintain the same character relationship pattern. This means if characters at positions i and j are equal in one substring, they must be equal in the other substring too. Understanding Similar Substrings Two strings are similar if they follow these rules − They are of same length For each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] ...

Read More

Program to create a lexically minimal string from two strings in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 217 Views

Creating a lexically minimal string from two strings involves comparing suffixes and selecting characters that produce the smallest lexicographical result. We compare remaining portions of both strings and choose the character from the string with the lexicographically smaller suffix. So, if the input is like input_1 = 'TUTORIALS', input_2 = 'POINT', then the output will be POINTTUTORIALS. Algorithm Steps If we compare the two strings step-by-step ? TUTORIALS vs POINT TUTORIALS vs OINT → P (choose from input_2) TUTORIALS vs INT → O (choose from input_2) TUTORIALS vs NT → I (choose from input_2) ...

Read More

Program to find out the number of shifts required to sort an array using insertion sort in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 1K+ Views

Insertion sort moves elements one position at a time to place them in their correct sorted position. Each movement is called a shift. We need to count the total number of shifts required to sort an array using insertion sort. The key insight is that the number of shifts for each element equals the number of larger elements that appear before it in the array. This is called the inversion count. Example Walkthrough Given array [4, 5, 3, 1, 2], let's trace the insertion sort process ? Step 1: [4, 5, 3, 1, 2] → ...

Read More

When is plt.Show() required to show a plot and when is it not?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 3K+ Views

When working with Matplotlib plots, understanding when to use plt.show() is crucial. The requirement depends on your environment — whether you're in an interactive Python session, a Jupyter notebook, or running a script. Interactive vs Non-Interactive Environments In interactive environments like Jupyter notebooks or IPython, plots often display automatically without calling plt.show(). In non-interactive environments like regular Python scripts, you must call plt.show() to display the plot. Basic Example Without plt.show() In Jupyter notebooks, this code displays the plot automatically: import matplotlib.pyplot as plt import numpy as np x = np.linspace(-5, 5, 100) ...

Read More

How to remove the axis tick marks on a Seaborn heatmap?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 6K+ Views

To remove the axis tick marks on a Seaborn heatmap, you can use the tick_params() method to customize the appearance of ticks and tick labels. This is useful when you want a cleaner visualization without the small lines indicating tick positions. Basic Heatmap with Tick Marks First, let's create a basic heatmap to see the default tick marks − import numpy as np import seaborn as sns import matplotlib.pyplot as plt # Create sample data data = np.random.rand(4, 4) # Create heatmap plt.figure(figsize=(6, 4)) ax = sns.heatmap(data, annot=True, cmap='viridis') plt.title('Heatmap with Default Tick Marks') ...

Read More

Make logically shading region for a curve in matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 254 Views

To make logically shading region for a curve in matplotlib, we can use BrokenBarHCollection.span_where() to create conditional shading based on the curve's values. This technique is useful for highlighting regions where a function satisfies certain conditions. Steps Set the figure size and adjust the padding between and around the subplots. Create t, s1 and s2 data points using numpy. Create a figure and a set of subplots. Plot t and s1 data points; add a horizontal line across the axis. Create a collection of horizontal bars spanning yrange with a sequence of xranges. Add a Collection to ...

Read More

Saving a 3D-plot in a PDF 3D with Python

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 5K+ Views

To save a 3D plot in a PDF with Python, you can use matplotlib's savefig() method. This approach creates standard 2D PDF files containing the 3D visualization, which is suitable for most documentation and sharing purposes. Steps Set the figure size and adjust the padding between and around the subplots. Create a new figure or activate an existing figure. Add an 'ax' to the figure as part of a subplot arrangement with 3D projection. Create u, v, x, y and z data points using numpy. Plot a 3D wireframe or surface. Set the title and labels of ...

Read More

How to control the border of a bar patch in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 6K+ Views

In matplotlib, you can control the appearance of bar chart borders using several parameters in the bar() method. The main parameters are edgecolor for border color and linewidth for border thickness. Basic Border Control Use edgecolor to set the border color of bar patches ? import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True heights = [3, 12, 5, 18, 45] labels = ('P1', 'P2', 'P3', 'P4', 'P5') x_pos = np.arange(len(labels)) plt.bar(x_pos, heights, color=(0.9, 0.7, 0.1, 0.5), edgecolor='green') plt.xticks(x_pos, labels) plt.title('Bar Chart with Green Borders') plt.show() ...

Read More

How to increase the line thickness of a Seaborn Line?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 4K+ Views

To increase the line thickness of a Seaborn line plot, you can use the linewidth parameter (or its shorthand lw) in the lineplot() function. This parameter controls how thick the line appears in your visualization. Basic Example Here's how to create a line plot with increased thickness ? import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np # Create sample data df = pd.DataFrame({ 'time': list(pd.date_range("2021-01-01 12:00:00", periods=10)), 'speed': np.linspace(1, 10, 10) }) # Create line plot with thick ...

Read More
Showing 3231–3240 of 8,546 articles
« Prev 1 322 323 324 325 326 855 Next »
Advertisements