Find Column Index of Left-Most 1 in Binary Matrix using Python

Arnab Chakraborty
Updated on 10-Nov-2020 07:37:30

241 Views

Suppose we have a 2D binary matrix. Here each row is sorted in ascending order with 0s coming before 1s, we have to find the leftmost column index with the value of 1. If there's no such result, return -1.So, if the input is like0001001100110010then the output will be 2, as the second column has left most 1 in entire matrix.To solve this, we will follow these steps:if matrix is empty, thenreturn -1N := row count of matrixM := column count of matrixi := 0, j := M - 1leftmost := -1while i < N and j >= 0, doif ... Read More

Find Maximum Profit by Buying and Selling Stocks in Python

Arnab Chakraborty
Updated on 10-Nov-2020 07:34:52

203 Views

Suppose we have a list of stock prices of a company in chronological order, we have to find the maximum profit we could have made from buying and selling the stock. We must buy before selling, and we must wait one day after selling the stock before buying again.So, if the input is like prices = [2, 6, 9, 4, 11], then the output will be 11, as we can buy at 2, then sell at 6, wait for a day, then buy at 4 and then sell at 11.To solve this, we will follow these steps:s := 0b := ... Read More

Convert Linked List by Alternating Nodes from Front and Back in Python

Arnab Chakraborty
Updated on 10-Nov-2020 07:31:39

165 Views

Suppose we have a singly linked list, we have to rearrange it such that we take: the last node, and then the first node, and then the second last node, and then the second node, and so on.So, if the input is like [1, 2, 3, 4, 5, 6, 7, 8, 9], then the output will be [9, 1, 8, 2, 7, 3, 6, 4, 5, ]To solve this, we will follow these steps:c := nodel := a new listwhile c is not-null, doinsert value of c at the end of lc := next of cc := nodewhile c is ... Read More

Minimum Characters to Delete to Make A Before B in Python

Arnab Chakraborty
Updated on 10-Nov-2020 07:26:04

893 Views

Suppose we have a string s consisting only two letters A and B, we have to find the minimum number of letters that need to be deleted from s to get all occurrences of As before all occurrences of Bs.So, if the input is like S = "AABAABB", then the output will be 1, as We can remove the last A to get AABBBTo solve this, we will follow these steps:a_right := number of occurrences of "A" in sb_left := 0ans := a_rightfor each index i and character c in s, doif c is same as "A", thena_right := a_right ... Read More

Find Number of Ways to Arrange Symbols to Get Target in Python

Arnab Chakraborty
Updated on 10-Nov-2020 07:23:28

168 Views

Suppose we have a list of non-negative numbers called nums and also have an integer target. We have to find the the number of ways to arrange + and - in nums such that the expression equals to target.So, if the input is like nums = [2, 3, 3, 3, 2] target = 9, then the output will be 2, as we can have -2 + 3 + 3 + 3 + 2 and 2 + 3 + 3 + 3 – 2.To solve this, we will follow these steps:s := sum of all numbers in numsif (s + target) ... Read More

Find Number of Arithmetic Subsequences in Python

Arnab Chakraborty
Updated on 10-Nov-2020 07:19:24

443 Views

Suppose we have a list of numbers called nums, we have to find the number of arithmetic subsequences of length ≥ 3. As we know an arithmetic sequence is a list of numbers where the difference between one number and the next is the same.So, if the input is like nums = [6, 12, 13, 8, 10, 14], then the output will be 3, as we have subsequences like: [6, 8, 10], [6, 10, 14], [12, 13, 14].To solve this, we will follow these steps:dp := a new mapn := size of numsres := 0for i in range 0 to ... Read More

Find Number of Arithmetic Sequences from a List of Numbers in Python

Arnab Chakraborty
Updated on 10-Nov-2020 07:16:50

2K+ Views

Suppose we have a list of numbers called nums, we have to find the number of contiguous arithmetic sequences of length ≥ 3. As we know an arithmetic sequence is a list of numbers where the difference between one number and the next number is the same.So, if the input is like nums = [6, 8, 10, 12, 13, 14], then the output will be 4, as we have the arithmetic sequences like: [6, 8, 10] [8, 10, 12] [6, 8, 10, 12] [12, 13, 14]To solve this, we will follow these steps −count := 0, ans := 0for i ... Read More

Select Subsets of Data in SQL Query Style in Pandas

Kiran P
Updated on 10-Nov-2020 06:52:12

358 Views

IntroductionIn this post, I will show you how to perform Data Analysis with SQL style filtering with Pandas. Most of the corporate company’s data are stored in databases that require SQL to retrieve and manipulate it. For instance, there are companies like Oracle, IBM, Microsoft having their own databases with their own SQL implementations.Data scientists have to deal with SQL at some stage of their career as the data is not always stored in CSV files. I personally prefer to use Oracle, as the majority of my company’s data is stored in Oracle.Scenario – 1 Suppose we are given a ... Read More

Select Subset of Data with Index Labels in Python Pandas

Kiran P
Updated on 10-Nov-2020 06:32:47

1K+ Views

IntroductionPandas have a dual selection capability to select the subset of data using the Index position or by using the Index labels. Inthis post, I will show you how to “Select a Subset Of Data Using Index Labels” using the index label.Remember, Python dictionaries and lists are built-in data structures that select their data either by using the index label or byindex position. A dictionary’s key must be a string, integer, or tuple while a List must either use integers (the position) or sliceobjects for selection.Pandas have .loc and.iloc attributes available to perform index operations in their own unique ways. ... Read More

Use Same Positional Arguments in Python

Kiran P
Updated on 10-Nov-2020 06:22:40

853 Views

Introduction..If we were writing a program that performs arithematic operations on two numbers, we could define them as two positional arguments. But since they are the same kinds/python data types of arguments, it might make more sense to use the nargs option to tell argparse that you want exactly two of same types.How to do it..1. Let's write a program to substract two numbers (both the arguments are of same type).Exampleimport argparse def get_args(): """ Function : get_args parameters used in .add_argument 1. metavar - Provide a hint to the user about the data type. - By default, all ... Read More

Advertisements