Found 10784 Articles for Python

Maximum Subarray in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:01:53

3K+ Views

Suppose we have an integer array A. We have to find the contiguous subarrays which length will be at least one, and that has the largest sum, and also return its sum. So if the array A is like A = [-2, 1, -3, 4, -1, 2, 1, -5, 4], then the sum will be 6. And the subarray will be [4, -1, 2, 1]To solve this we will try to use the Dynamic programming approach.define an array dp same as the size of A, and fill it with 0dp[0] := A[0]for i = 1 to the size of A ... Read More

Count and Say in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:00:49

1K+ Views

Here we will see the Count and Say sequence. This is a sequence whose few terms are like below −111211211111221The string will be read like1 (One)11 (One 1) So read the previous 1, and say “One 1”21 (Two 1) So read the previous 11, and say “Two 1”1211 (One 2 one 1) So read the previous 21, and say “One 2 one 1”111221 (One 1 one 2 two 1) So read the previous 1211, and say “One 1 one 2 two 1”Suppose we have a number n, 1

Implement strStr() in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:00:13

2K+ Views

Suppose we have two strings str and sub_str. We have to find the first occurrence of sub_str in the str. So if the string str is “helloworld”, and substring is “lo”, then the result will be 3.This can be done using the strstr() function in C. We have to design another function that is similar to the strstr() in C.To solve this, follow these steps −i := 0, j := 0, m := length of sub_str and n := length of strif m = 0, then return 0while i < n and n – i + 1 = m, doif ... Read More

Remove Duplicates from Sorted Array in Python

Arnab Chakraborty
Updated on 28-Apr-2020 15:59:22

4K+ Views

Suppose we have a sorted list A. We have to return the length of the array after removing all duplicate entries. We have to perform this in O(1) extra space. So we have to do the operation in-place.For an example, suppose A = [1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5, 6] Then the output will be 6, as there are six distinct elements.To solve this, follow these steps −If the list is empty, return 0otherwise, initially take prev = first element of A. And define length = 0for i := 1 to n-1, doif ... Read More

Merge Two Sorted Lists in Python

Arnab Chakraborty
Updated on 28-Apr-2020 15:58:23

1K+ Views

Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may different.For an example, suppose A = [1,2,4,7] and B = [1,3,4,5,6,8], then merged list C will be [1,1,2,3,4,4,5,6,7,8]We will solve this using recursion. So the function will work like below −Suppose the lists A and B of function merge()if A is empty, then return B, if B is empty, then return Aif value in A

Longest Common Prefix in Python

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

6K+ 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

30K+ 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

17K+ 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

5K+ 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

349 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

Advertisements