Found 10476 Articles for Python

Program to find minimum cost to connect all points in Python

Arnab Chakraborty
Updated on 04-Oct-2021 09:32:53

478 Views

Suppose we have an array called points with some points in the form (x, y). Now the cost of connecting two points (xi, yi) and (xj, yj) is the Manhattan distance between them, the formula is |xi - xj| + |yi - yj|. We have to find the minimum cost to make all points connected.So, if the input is like points = [(0, 0), (3, 3), (2, 10), (6, 3), (8, 0)], then the output will be 22 becauseso here total distance is (6+5+3+8) = 22.To solve this, we will follow these steps −points_set := a new set holding numbers ... Read More

Program to count number of unhappy friends in Python

Arnab Chakraborty
Updated on 04-Oct-2021 09:18:51

260 Views

Suppose we have a list of preferences for n(even) different friends. For each person i, the preferences[i] holds a list of friends sorted in the order of preference. So, a friend earlier in the list is more preferred than a friend later in the list. The friends in each list are numbered by integers from 0 to n-1. All of the friends are divided into different pairs. where pairs[i] = [xi, yi] represents xi is paired with yi and/or yi is paired with xi. But a friend x is unhappy if x is paired with y and there exists a ... Read More

Program to find minimum deletion cost to avoid repeating letters in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:50:25

685 Views

Suppose we have a string s and another array of integers called cost where cost[i] represents the cost of deleting the ith character in s. We have to find the minimum cost of deletions such that there are no two same letters next to each other. We have to keep in mind that that we will delete the chosen characters at the same time. So after deleting a character, the costs of deleting other characters will not change.So, if the input is like s = "pptpp", cost = [2, 3, 4, 5, 2], then the output will be 4 because ... Read More

Program to find number of ways where square of number is equal to product of two numbers in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:39:40

204 Views

Suppose we have two arrays nums1 and nums2, we have to find of triplets formed (type 1 and type 2) following these two rules −Triplet (i, j, k) if nums1[i]^2 = nums2[j] * nums2[k] where [0

Program to find shortest subarray to be removed to make array sorted in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:12:57

270 Views

Suppose we have an array called arr, we have to remove a subarray of arr such that the remaining elements in arr are in non-decreasing order. We have to find the length of the shortest subarray to remove.So, if the input is like arr = [10, 20, 30, 100, 40, 20, 30, 50], then the output will be 3 because we can remove [100, 40, 20] which is smallest subarray of length 3, and by removing these all are in non-decreasing order [10, 20, 30, 30, 50].To solve this, we will follow these steps:n := size of arrarr := insert ... Read More

Program to find number of ways to split a string in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:02:11

406 Views

Suppose we have a binary string s we can split s into 3 non-empty strings s1, s2, s3 such that (s1 concatenate s2 concatenate s3 = s). We have to find the number of ways s can be split such that the number of characters '1' is the same in s1, s2, and s3. The answer may be very large so return answer mod 10^9+7.So, if the input is like s = "11101011", then the output will be 2 because we can split them like "11 | 1010 | 11" and "11 | 101 | 011".To solve this, we will ... Read More

Python - Density Plots with Pandas for a specific attribute

AmitDiwan
Updated on 04-Oct-2021 07:42:33

608 Views

We will use plot.density() to density plot on a Dataset in the form of a csv file. Let’s say the following is our dataset − Cricketers2.csvAt first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Plotting the density plot. Attribute considered is "Age" −dataFrame.Age.plot.density(color='green')ExampleFollowing is the complete code −import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") # plotting the density plot # attribute considered is "Age" dataFrame.Age.plot.density(color='green') plt.title('Density ... Read More

Python Pandas - Draw a vertical violinplot grouped by a categorical variable with Seaborn

AmitDiwan
Updated on 04-Oct-2021 07:39:55

561 Views

Violin Plot in Seaborn is used to draw a combination of boxplot and kernel density estimate. The seaborn.violinplot() is used for this. We will plotti violin plot with the columns grouped by a categorical variable.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting violin plot with Role and Age grouped by a categorical variable −sb.violinplot(x = 'Role', y = "Age", data = dataFrame)ExampleFollowing ... Read More

Python Pandas - Draw a bar plot and set a cap to the error bars with Seaborn

AmitDiwan
Updated on 04-Oct-2021 07:37:07

924 Views

Bar Plot in Seaborn is used to show point estimates and confidence intervals as rectangular bars. The seaborn.barplot() is used. Set caps to the error bars using the capsize parameter.Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Set caps to the error bars using the capsize parameter −sb.barplot(x=dataFrame["Role"], y=dataFrame["Matches"], capsize=.3)ExampleFollowing is the code −import seaborn as sb import pandas as pd import matplotlib.pyplot ... Read More

Python Pandas - Group the swarms by a categorical variable with Seaborn

AmitDiwan
Updated on 04-Oct-2021 07:35:05

138 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this. Group the swarms by a categorical variable by simply setting it as one of the x and y coordinates.Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Group the swarms by a categorical variable −sb.swarmplot(x = dataFrame["Role"], y = dataFrame["Age"])ExampleFollowing is the code −import ... Read More

Advertisements