Found 26504 Articles for Server Side Programming

Valid Parentheses in C++

Arnab Chakraborty
Updated on 28-Apr-2020 08:40:23

6K+ Views

Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.The task is simple; we will use the stack to do this. We should follow these steps to get the solution −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from ... Read More

Longest Common Prefix in Python

Arnab Chakraborty
Updated on 28-Apr-2020 15:50:24

7K+ Views

Suppose we have a set of strings in an array. We have to find the Longest Common Prefix amongst the string in the array. Here we will assume that all strings are lower case strings. And if there is no common prefix, then return “”.So if the array of a string is like ["school", "schedule", "Scotland"], then the Longest Common Prefix is “sc” as this is present in all of these string.To solve this, we will take the first string as curr, now take each string from the array and read them character by character, and check the characters between ... Read More

Roman to Integer in Python

Arnab Chakraborty
Updated on 14-Sep-2023 01:09:07

37K+ Views

Suppose we have Roman literals; we have to convert them into an integer. As we know the Roman numerals represent in some different symbols as below −NumeralValueI1V5X10L50C100D500M1000If we see the roman numbers closely, it is like suppose the numeral is 'II', so this is 2, there are two 'I's are added together. For XII, it is 12, so this is actually X + II = 10 + 2 = 12. The roman numerals of 4 are not IIII, it is IV. This is a little tricky.I can be used before V(5) and X(10) to make it 4 and 9 respectivelyX ... Read More

Two Sum in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:04:05

19K+ Views

Suppose we have an array of integers. We have to return the indices of two integers, such that if we add them up, we will reach to a specific target that is also given. Here we will take one assumption, that is always there will be one unique solution in the array, so no two set of indices for same target will be there.For an example, suppose the array is like A = [2, 8, 12, 15], and the target sum is 20. Then it will return indices 1 and 2, as A[1] + A[2] = 20.To solve this, we ... Read More

Windows registry access using Python (winreg)

Pradeep Elance
Updated on 07-Jan-2020 06:53:49

6K+ Views

As a versatile language and also availability of very large number of user supported modules, we find that python is also good at OS level programming. In this article we will see how python can access the registry of a windows operating system.We need to import the module named winreg into the python environment.In the below example we use the winreg module to first connect to the registry using the ConnectRegistry function and then access the registry using OpenKey function. Finally we design a for loop to print the result of the keys accessed.Exampleimport winreg #connecting to key in registry ... Read More

Python - Filter the negative values from given dictionary

Pradeep Elance
Updated on 07-Jan-2020 06:48:05

532 Views

As part of data analysis, we will come across scenarios to remove the negative values form a dictionary. For this we have to loop through each of the elements in the dictionary and use a condition to check the value. Below two approaches can be implemented to achieve this.Using for loopW simply loop through the elements of the list using a for loop. In every iteration we use the items function to compare the value of the element with the 0 for checking negative value.Example Live Demodict_1 = {'x':10, 'y':20, 'z':-30, 'p':-0.5, 'q':50} print ("Given Dictionary :", str(dict_1)) final_res_1 ... Read More

Python - Filter even values from a list

Pradeep Elance
Updated on 07-Jan-2020 06:46:30

1K+ Views

As part of data analysis require to filter out values from a list meeting certain criteria. In this article we'll see how to filter out only the even values from a list.We have to go through each element of the list and divide it with 2 to check for the remainder. If the remainder is zero then we consider it as an even number. After fetching these even numbers from a list we will put a condition to create a new list which excludes this even numbers. That new list is the result of the filtering condition we applied.Using for ... Read More

Multiplication of two Matrices in Single line using Numpy in Python

Pradeep Elance
Updated on 07-Jan-2020 06:41:16

729 Views

Matrix multiplication is a lengthy process where each element from each row and column of the matrixes are to be multiplied and added in a certain way. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result matrix has the number of rows of the first and the number of columns of the second matrix.For smaller matrices we may design nested for loops and find the result. For bigger matrices we need some built in functionality in python to tackle this. We will see both ... Read More

All possible permutations of N lists in Python

Pradeep Elance
Updated on 07-Jan-2020 06:34:45

1K+ Views

If we have two lists and we need to combine each element of the first element with each element of the second list, then we have the below approaches.Using For LoopIn this straight forward approach we create a list of lists containing the permutation of elements from each list. we design a for loop within another for loop. The inner for loop refers to the second list and Outer follow refers to the first list.Example Live DemoA = [5, 8] B = [10, 15, 20] print ("The given lists : ", A, B) permutations = [[m, n] for m in ... Read More

Add leading Zeros to Python string

Pradeep Elance
Updated on 18-Feb-2020 11:22:50

643 Views

We may sometimes need to append zeros as string to various data elements in python. There may the reason for formatting and nice representation or there may be the reason for some calculations where these values will act as input. Below are the methods which we will use for this purpose.Using format()Here we take a DataFrame and apply the format function to the column wher we need to append the zeros as strings. The lambda method is used to apply the function repeatedly.Example Live Demoimport pandas as pd string = {'Column' : ['HOPE', 'FOR', 'THE', 'BEST']} dataframe=pd.DataFrame(string) print("given column is ") ... Read More

Advertisements